Skip to content

A self-hosted media server and file management system

3.5 KB
Raw Blame
<script setup lang="ts">
import type { ChangeEmailRequest } from '@/types/requests';
import type { FormField } from '@/types/types';

import { FormInput, FormLabel, FormErrorList } from '@/components/cedar-ui/form';
import { changeEmail } from '@/service/authAPI';
import { ButtonForm } from '@/components/cedar-ui/button';
import { cn, toast } from '@aminnausin/cedar-ui';
import { reactive } from 'vue';

import SettingsHeader from '@/components/settings/SettingsHeader.vue';
import SettingsCard from '@/components/cards/layout/SettingsCard.vue';
import FormFooter from '@/components/forms/FormFooter.vue';
import useForm from '@/composables/useForm';

const fields = reactive<FormField[]>([
    {
        name: 'email',
        text: `New Email`,
        autocomplete: 'email',
        ariaAutocomplete: 'inline',
        type: 'text',
        required: true,
        placeholder: `New Email`,
        max: 255,
    },
    {
        name: 'password',
        text: 'Confirm Password',
        placeholder: 'Confirm Password',
        autocomplete: 'current-password',
        ariaAutocomplete: 'inline',
        type: 'password',
        required: true,
        max: 255,
    },
]);

const form = useForm<ChangeEmailRequest>({
    email: '',
    password: '',
});

const handleSubmit = async () => {
    form.submit(
        async (fields) => {
            return changeEmail(fields);
        },
        {
            onSuccess: () => {
                toast.add('Success', { type: 'success', description: 'Email Changed', life: 3000 });
                form.reset(...Object.keys(form.fields));
            },
            onError: () => {
                form.reset('password');

                if (form.errors.email) {
                    form.reset('email');
                    return;
                }
                toast.add('Error', { type: 'danger', description: 'Unable change email.', life: 3000 });
            },
        },
    );
};
</script>

<template>
    <SettingsCard>
        <template #content>
            <SettingsHeader>
                <h3 class="text-base font-medium">Update Email</h3>
                <p class="text-foreground-1">Ensure you have secure access to this email</p>
            </SettingsHeader>
            <form class="flex w-full max-w-xl flex-col flex-wrap gap-4 sm:flex-row sm:justify-between" @submit.prevent="handleSubmit">
                <div v-for="(field, index) in fields.filter((field) => !field.disabled)" :key="index" class="w-full" :class="field.class">
                    <FormLabel :for="field.name" :text="field.text" :subtext="field.subtext" />
                    <FormInput v-model="form.fields[field.name]" :field="field" />
                    <FormErrorList :errors="form.errors" :field-name="field.name" />
                </div>

                <FormFooter class="w-full">
                    <ButtonForm
                        type="button"
                        variant="reset"
                        :disabled="form.processing || !form.dirty"
                        :class="cn('transition-reveal overflow-hidden', form.dirty ? 'mx-0 w-18 px-4 opacity-100' : '-me-2 w-0 border-0 px-0 opacity-0')"
                        @click="form.reset(...Object.keys(form.fields))"
                    >
                        Clear
                    </ButtonForm>
                    <ButtonForm @click="handleSubmit" type="button" variant="submit" :disabled="form.processing"> Save </ButtonForm>
                </FormFooter>
            </form>
        </template>
    </SettingsCard>
</template>