funkwhale/front/src/components/auth/SignupForm.vue

224 wiersze
6.3 KiB
Vue
Czysty Zwykły widok Historia

2022-07-30 18:44:49 +00:00
<script setup lang="ts">
import type { RouteLocationRaw } from 'vue-router'
import type { BackendError, Form } from '~/types'
2022-11-16 07:18:10 +00:00
import { computed, reactive, ref, watchEffect } from 'vue'
2022-09-08 14:32:45 +00:00
import { useI18n } from 'vue-i18n'
2022-07-30 18:44:49 +00:00
import { useStore } from '~/store'
import axios from 'axios'
import LoginForm from '~/components/auth/LoginForm.vue'
import PasswordInput from '~/components/forms/PasswordInput.vue'
import useLogger from '~/composables/useLogger'
interface Props {
defaultInvitation?: string | null
next?: RouteLocationRaw
buttonClasses?: string
customization?: Form | null
fetchDescriptionHtml?: boolean
signupApprovalEnabled?: boolean
}
const props = withDefaults(defineProps<Props>(), {
defaultInvitation: null,
next: '/',
buttonClasses: 'success',
customization: null,
fetchDescriptionHtml: false,
2022-11-16 07:18:10 +00:00
signupApprovalEnabled: undefined
2022-07-30 18:44:49 +00:00
})
2022-09-08 14:32:45 +00:00
const { t } = useI18n()
2022-07-30 18:44:49 +00:00
const logger = useLogger()
const store = useStore()
const labels = computed(() => ({
2022-09-22 23:26:59 +00:00
placeholder: t('components.auth.SignupForm.placeholder.invitation'),
usernamePlaceholder: t('components.auth.SignupForm.placeholder.username'),
emailPlaceholder: t('components.auth.SignupForm.placeholder.email')
2022-07-30 18:44:49 +00:00
}))
const signupRequiresApproval = computed(() => props.signupApprovalEnabled ?? store.state.instance.settings.moderation.signup_approval_enabled.value)
const formCustomization = computed(() => props.customization ?? store.state.instance.settings.moderation.signup_form_customization.value)
watchEffect(() => console.log(store.state.instance.settings.moderation.signup_approval_enabled.value))
2022-07-30 18:44:49 +00:00
const payload = reactive({
username: '',
password1: '',
email: '',
invitation: props.defaultInvitation,
2022-08-31 16:39:24 +00:00
request_fields: {} as Record<string, string | number | string[]>
2022-07-30 18:44:49 +00:00
})
const submitted = ref(false)
const isLoading = ref(false)
const errors = ref([] as string[])
const submit = async () => {
isLoading.value = true
errors.value = []
try {
await axios.post('auth/registration/', {
...payload,
password2: payload.password1
})
logger.info('Successfully created account')
submitted.value = true
} catch (error) {
errors.value = (error as BackendError).backendErrors
}
isLoading.value = false
}
const isLoadingInstanceSetting = ref(false)
const fetchInstanceSettings = async () => {
isLoadingInstanceSetting.value = true
await store.dispatch('instance/fetchSettings')
isLoadingInstanceSetting.value = false
}
fetchInstanceSettings()
</script>
2019-09-23 09:14:54 +00:00
<template>
2020-03-18 10:57:33 +00:00
<div v-if="submitted">
<div class="ui success message">
<p v-if="signupRequiresApproval">
2022-09-22 23:26:59 +00:00
{{ $t('components.auth.SignupForm.message.awaitingReview') }}
2020-03-18 10:57:33 +00:00
</p>
<p v-else>
2022-09-22 23:26:59 +00:00
{{ $t('components.auth.SignupForm.message.accountCreated') }}
2020-03-18 10:57:33 +00:00
</p>
</div>
2021-12-06 10:35:20 +00:00
<h2>
2022-09-22 23:26:59 +00:00
{{ $t('components.auth.SignupForm.header.login') }}
2021-12-06 10:35:20 +00:00
</h2>
<login-form
button-classes="basic success"
:show-signup="false"
/>
2020-03-18 10:57:33 +00:00
</div>
2019-09-23 09:14:54 +00:00
<form
2020-03-18 10:57:33 +00:00
v-else
2019-09-23 09:14:54 +00:00
:class="['ui', {'loading': isLoadingInstanceSetting}, 'form']"
2021-12-06 10:35:20 +00:00
@submit.prevent="submit()"
>
<p
v-if="!$store.state.instance.settings.users.registration_enabled.value"
class="ui message"
>
2022-09-22 23:26:59 +00:00
{{ $t('components.auth.SignupForm.message.registrationClosed') }}
2019-09-23 09:14:54 +00:00
</p>
2021-12-06 10:35:20 +00:00
<p
v-else-if="signupRequiresApproval"
class="ui message"
>
2022-09-22 23:26:59 +00:00
{{ $t('components.auth.SignupForm.message.requiresReview') }}
2020-03-18 10:57:33 +00:00
</p>
2022-11-16 07:18:10 +00:00
<template v-if="formCustomization?.help_text">
2021-12-06 10:35:20 +00:00
<rendered-description
:content="formCustomization.help_text"
:fetch-html="fetchDescriptionHtml"
:permissive="true"
/>
<div class="ui hidden divider" />
2020-03-18 10:57:33 +00:00
</template>
2021-12-06 10:35:20 +00:00
<div
v-if="errors.length > 0"
role="alert"
class="ui negative message"
>
<h4 class="header">
2022-09-22 23:26:59 +00:00
{{ $t('components.auth.SignupForm.header.signupFailure') }}
2021-12-06 10:35:20 +00:00
</h4>
2019-09-23 09:14:54 +00:00
<ul class="list">
2021-12-06 10:35:20 +00:00
<li
v-for="(error, key) in errors"
:key="key"
>
{{ error }}
</li>
2019-09-23 09:14:54 +00:00
</ul>
</div>
2020-03-18 10:57:33 +00:00
<div class="required field">
2022-09-22 23:26:59 +00:00
<label for="username-field">{{ $t('components.auth.SignupForm.label.username') }}</label>
2019-09-23 09:14:54 +00:00
<input
2021-12-06 10:35:20 +00:00
id="username-field"
ref="username"
2022-07-30 18:44:49 +00:00
v-model="payload.username"
2021-12-06 10:35:20 +00:00
name="username"
required
type="text"
autofocus
:placeholder="labels.usernamePlaceholder"
>
2019-09-23 09:14:54 +00:00
</div>
2020-03-18 10:57:33 +00:00
<div class="required field">
2022-09-22 23:26:59 +00:00
<label for="email-field">{{ $t('components.auth.SignupForm.label.email') }}</label>
2019-09-23 09:14:54 +00:00
<input
2021-12-06 10:35:20 +00:00
id="email-field"
ref="email"
2022-07-30 18:44:49 +00:00
v-model="payload.email"
2021-12-06 10:35:20 +00:00
name="email"
required
type="email"
:placeholder="labels.emailPlaceholder"
>
2019-09-23 09:14:54 +00:00
</div>
2020-03-18 10:57:33 +00:00
<div class="required field">
2022-09-22 23:26:59 +00:00
<label for="password-field">{{ $t('components.auth.SignupForm.label.password') }}</label>
2021-12-06 10:35:20 +00:00
<password-input
2022-07-30 18:44:49 +00:00
v-model="payload.password1"
2021-12-06 10:35:20 +00:00
field-id="password-field"
/>
2019-09-23 09:14:54 +00:00
</div>
2021-12-06 10:35:20 +00:00
<div
v-if="!$store.state.instance.settings.users.registration_enabled.value"
class="required field"
>
2022-09-22 23:26:59 +00:00
<label for="invitation-code">{{ $t('components.auth.SignupForm.label.invitation') }}</label>
2019-09-23 09:14:54 +00:00
<input
2021-12-06 10:35:20 +00:00
id="invitation-code"
2022-07-30 18:44:49 +00:00
v-model="payload.invitation"
2021-12-06 10:35:20 +00:00
required
type="text"
name="invitation"
:placeholder="labels.placeholder"
>
2019-09-23 09:14:54 +00:00
</div>
2022-11-16 07:18:10 +00:00
<template v-if="signupRequiresApproval && (formCustomization?.fields.length ?? 0) > 0">
2021-12-06 10:35:20 +00:00
<div
2022-11-16 07:18:10 +00:00
v-for="(field, idx) in formCustomization?.fields"
2021-12-06 10:35:20 +00:00
:key="idx"
:class="[{required: field.required}, 'field']"
>
2020-03-18 10:57:33 +00:00
<label :for="`custom-field-${idx}`">{{ field.label }}</label>
<textarea
v-if="field.input_type === 'long_text'"
2020-08-01 09:11:51 +00:00
:id="`custom-field-${idx}`"
2022-07-30 18:44:49 +00:00
v-model="payload.request_fields[field.label]"
:required="field.required"
2021-12-06 10:35:20 +00:00
rows="5"
/>
<input
v-else
:id="`custom-field-${idx}`"
2022-07-30 18:44:49 +00:00
v-model="payload.request_fields[field.label]"
2021-12-06 10:35:20 +00:00
type="text"
2022-07-30 18:44:49 +00:00
:required="field.required"
2021-12-06 10:35:20 +00:00
>
2020-03-18 10:57:33 +00:00
</div>
</template>
2021-12-06 10:35:20 +00:00
<button
:class="['ui', buttonClasses, {'loading': isLoading}, ' right floated button']"
type="submit"
>
2022-09-22 23:26:59 +00:00
{{ $t('components.auth.SignupForm.button.create') }}
2019-09-23 09:14:54 +00:00
</button>
</form>
</template>