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