|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 3 | + - SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +--> |
| 5 | + |
| 6 | +<template> |
| 7 | + <NcDialog :buttons="dialogButtons" |
| 8 | + class="public-auth-prompt" |
| 9 | + data-cy-public-auth-prompt-dialog |
| 10 | + is-form |
| 11 | + :can-close="false" |
| 12 | + :name="title" |
| 13 | + @submit="onSubmit"> |
| 14 | + <p v-if="subtitle" class="public-auth-prompt__subtitle"> |
| 15 | + {{ subtitle }} |
| 16 | + </p> |
| 17 | + |
| 18 | + <!-- Header --> |
| 19 | + <NcNoteCard class="public-auth-prompt__header" |
| 20 | + :text="notice" |
| 21 | + type="info" /> |
| 22 | + |
| 23 | + <!-- Form --> |
| 24 | + <NcTextField ref="input" |
| 25 | + class="public-auth-prompt__input" |
| 26 | + data-cy-public-auth-prompt-dialog-name |
| 27 | + :label="t('Name')" |
| 28 | + :placeholder="t('Enter your name')" |
| 29 | + :required="!cancellable" |
| 30 | + :value.sync="name" |
| 31 | + minlength="2" |
| 32 | + name="name" /> |
| 33 | + </NcDialog> |
| 34 | +</template> |
| 35 | + |
| 36 | +<script lang="ts"> |
| 37 | +import { defineComponent } from 'vue' |
| 38 | +import { getBuilder } from '@nextcloud/browser-storage' |
| 39 | +import { setGuestNickname } from '@nextcloud/auth' |
| 40 | +import { showError } from '@nextcloud/dialogs' |
| 41 | +
|
| 42 | +import NcButton from '@nextcloud/vue/components/NcButton' |
| 43 | +import NcDialog from '@nextcloud/vue/components/NcDialog' |
| 44 | +import NcNoteCard from '@nextcloud/vue/components/NcNoteCard' |
| 45 | +import NcTextField from '@nextcloud/vue/components/NcTextField' |
| 46 | +
|
| 47 | +import { t } from '../utils/l10n.ts' |
| 48 | +
|
| 49 | +const storage = getBuilder('public').build() |
| 50 | +
|
| 51 | +// TODO: Remove this when the type export is fixed in the vue components library |
| 52 | +type ButtonType = InstanceType<typeof NcButton>['$props']['type'] |
| 53 | +
|
| 54 | +export default defineComponent({ |
| 55 | + name: 'PublicAuthPrompt', |
| 56 | +
|
| 57 | + components: { |
| 58 | + NcDialog, |
| 59 | + NcNoteCard, |
| 60 | + NcTextField, |
| 61 | + }, |
| 62 | +
|
| 63 | + props: { |
| 64 | + /** |
| 65 | + * Preselected nickname |
| 66 | + * @default '' No name preselected by default |
| 67 | + */ |
| 68 | + nickname: { |
| 69 | + type: String, |
| 70 | + default: '', |
| 71 | + }, |
| 72 | +
|
| 73 | + /** |
| 74 | + * Dialog title |
| 75 | + */ |
| 76 | + title: { |
| 77 | + type: String, |
| 78 | + default: t('Guest identification'), |
| 79 | + }, |
| 80 | +
|
| 81 | + /** |
| 82 | + * Dialog subtitle |
| 83 | + * @default 'Enter your name to access the file' |
| 84 | + */ |
| 85 | + subtitle: { |
| 86 | + type: String, |
| 87 | + default: '', |
| 88 | + }, |
| 89 | +
|
| 90 | + /** |
| 91 | + * Dialog notice |
| 92 | + * @default 'You are currently not identified.' |
| 93 | + */ |
| 94 | + notice: { |
| 95 | + type: String, |
| 96 | + default: t('You are currently not identified.'), |
| 97 | + }, |
| 98 | +
|
| 99 | + /** |
| 100 | + * Dialog submit button label |
| 101 | + * @default 'Submit name' |
| 102 | + */ |
| 103 | + submitLabel: { |
| 104 | + type: String, |
| 105 | + default: t('Submit name'), |
| 106 | + }, |
| 107 | +
|
| 108 | + /** |
| 109 | + * Whether the dialog is cancellable |
| 110 | + * @default false |
| 111 | + */ |
| 112 | + cancellable: { |
| 113 | + type: Boolean, |
| 114 | + default: false, |
| 115 | + }, |
| 116 | + }, |
| 117 | +
|
| 118 | + emits: ['close'], |
| 119 | +
|
| 120 | + setup() { |
| 121 | + return { |
| 122 | + t, |
| 123 | + } |
| 124 | + }, |
| 125 | +
|
| 126 | + data() { |
| 127 | + return { |
| 128 | + name: '', |
| 129 | + } |
| 130 | + }, |
| 131 | +
|
| 132 | + computed: { |
| 133 | + dialogButtons() { |
| 134 | + const cancelButton = { |
| 135 | + label: t('Cancel'), |
| 136 | + type: 'tertiary' as ButtonType, |
| 137 | + callback: () => this.$emit('close'), |
| 138 | + } |
| 139 | +
|
| 140 | + const submitButton = { |
| 141 | + label: this.submitLabel, |
| 142 | + type: 'primary' as ButtonType, |
| 143 | + nativeType: 'submit', |
| 144 | + } |
| 145 | +
|
| 146 | + // If the dialog is cancellable, add a cancel button |
| 147 | + if (this.cancellable) { |
| 148 | + return [cancelButton, submitButton] |
| 149 | + } |
| 150 | +
|
| 151 | + return [submitButton] |
| 152 | + }, |
| 153 | + }, |
| 154 | +
|
| 155 | + watch: { |
| 156 | + /** Reset name to pre-selected nickname (e.g. Talk / Collabora ) */ |
| 157 | + nickname: { |
| 158 | + handler() { |
| 159 | + this.name = this.nickname |
| 160 | + }, |
| 161 | + immediate: true, |
| 162 | + }, |
| 163 | + }, |
| 164 | +
|
| 165 | + methods: { |
| 166 | + onSubmit() { |
| 167 | + const input = this.$refs.input as HTMLInputElement |
| 168 | + const nickname = this.name.trim() |
| 169 | +
|
| 170 | + if (nickname === '') { |
| 171 | + // Show error if the nickname is empty |
| 172 | + input.setCustomValidity(t('You cannot leave the name empty.')) |
| 173 | + input.reportValidity() |
| 174 | + input.focus() |
| 175 | + return |
| 176 | + } |
| 177 | +
|
| 178 | + if (nickname.length < 2) { |
| 179 | + // Show error if the nickname is too short |
| 180 | + input.setCustomValidity(t('Please enter a name with at least 2 characters.')) |
| 181 | + input.reportValidity() |
| 182 | + input.focus() |
| 183 | + return |
| 184 | + } |
| 185 | +
|
| 186 | + try { |
| 187 | + // Set the nickname |
| 188 | + setGuestNickname(nickname) |
| 189 | + } catch (e) { |
| 190 | + showError(t('Failed to set nickname.')) |
| 191 | + console.error('Failed to set nickname', e) |
| 192 | + input.focus() |
| 193 | + return |
| 194 | + } |
| 195 | +
|
| 196 | + // Set the dialog as shown |
| 197 | + storage.setItem('public-auth-prompt-shown', 'true') |
| 198 | +
|
| 199 | + // Close the dialog |
| 200 | + this.$emit('close', this.name) |
| 201 | + }, |
| 202 | + }, |
| 203 | +}) |
| 204 | +</script> |
| 205 | +<style scoped lang="scss"> |
| 206 | +.public-auth-prompt { |
| 207 | + &__subtitle { |
| 208 | + // Smaller than dialog title |
| 209 | + font-size: 1.25em; |
| 210 | + margin-block: 0 calc(3 * var(--default-grid-baseline)); |
| 211 | + } |
| 212 | +
|
| 213 | + &__header { |
| 214 | + margin-block: 0 calc(3 * var(--default-grid-baseline)); |
| 215 | + // No extra top margin for the first child |
| 216 | + &:first-child { |
| 217 | + margin-top: 0; |
| 218 | + } |
| 219 | + } |
| 220 | +
|
| 221 | + &__input { |
| 222 | + margin-block: calc(4 * var(--default-grid-baseline)) calc(2 * var(--default-grid-baseline)); |
| 223 | + } |
| 224 | +} |
| 225 | +</style> |
0 commit comments