aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/src/views/PublicAuthPrompt.vue
blob: 28955a871545049c55b19442c949f8269c20a5ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!--
  - SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  - SPDX-License-Identifier: AGPL-3.0-or-later
-->

<template>
	<NcDialog :buttons="dialogButtons"
		class="public-auth-prompt"
		data-cy-public-auth-prompt-dialog
		is-form
		:can-close="false"
		:name="dialogName"
		@submit="$emit('close', name)">
		<p v-if="owner" class="public-auth-prompt__subtitle">
			{{ t('files_sharing', '{ownerDisplayName} shared a folder with you.', { ownerDisplayName }) }}
		</p>

		<!-- Header -->
		<NcNoteCard class="public-auth-prompt__header"
			:text="t('files_sharing', 'To upload files, you need to provide your name first.')"
			type="info" />

		<!-- Form -->
		<NcTextField ref="input"
			class="public-auth-prompt__input"
			data-cy-public-auth-prompt-dialog-name
			:label="t('files_sharing', 'Name')"
			:placeholder="t('files_sharing', 'Enter your name')"
			minlength="2"
			name="name"
			required
			:value.sync="name" />
	</NcDialog>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { loadState } from '@nextcloud/initial-state'
import { t } from '@nextcloud/l10n'

import NcDialog from '@nextcloud/vue/components/NcDialog'
import NcNoteCard from '@nextcloud/vue/components/NcNoteCard'
import NcTextField from '@nextcloud/vue/components/NcTextField'

import { getGuestNameValidity } from '../services/GuestNameValidity'

export default defineComponent({
	name: 'PublicAuthPrompt',

	components: {
		NcDialog,
		NcNoteCard,
		NcTextField,
	},

	props: {
		/**
		 * Preselected nickname
		 * @default '' No name preselected by default
		 */
		nickname: {
			type: String,
			default: '',
		},
	},

	setup() {
		return {
			t,

			owner: loadState('files_sharing', 'owner', ''),
			ownerDisplayName: loadState('files_sharing', 'ownerDisplayName', ''),
			label: loadState('files_sharing', 'label', ''),
			note: loadState('files_sharing', 'note', ''),
			filename: loadState('files_sharing', 'filename', ''),
		}
	},

	data() {
		return {
			name: '',
		}
	},

	computed: {
		dialogName() {
			return this.t('files_sharing', 'Upload files to {folder}', { folder: this.label || this.filename })
		},
		dialogButtons() {
			return [{
				label: t('files_sharing', 'Submit name'),
				type: 'primary',
				nativeType: 'submit',
			}]
		},
	},

	watch: {
		/** Reset name to pre-selected nickname (e.g. Talk / Collabora ) */
		nickname: {
			handler() {
				this.name = this.nickname
			},
			immediate: true,
		},

		name() {
			// Check validity of the new name
			const newName = this.name.trim?.() || ''
			const input = (this.$refs.input as Vue|undefined)?.$el.querySelector('input')
			if (!input) {
				return
			}

			const validity = getGuestNameValidity(newName)
			input.setCustomValidity(validity)
			input.reportValidity()
		},
	},
})
</script>
<style scoped lang="scss">
.public-auth-prompt {
	&__subtitle {
		// Smaller than dialog title
		font-size: 1.25em;
		margin-block: 0 calc(3 * var(--default-grid-baseline));
	}

	&__header {
		margin-block: 0 calc(3 * var(--default-grid-baseline));
	}

	&__input {
		margin-block: calc(4 * var(--default-grid-baseline)) calc(2 * var(--default-grid-baseline));
	}
}
</style>