import { Button, Divider, Form, Input, message, Select } from 'antd' import { useForm } from 'antd/lib/form/Form' import { NextPage } from 'next' import { useRouter } from 'next/router' import React, { useState } from 'react' import { useTranslation } from 'react-i18next' import Structure from '../../components/structure' import { withAuth } from '../../components/with.auth' import { useProfileUpdateMutation } from '../../graphql/mutation/profile.update.mutation' import { useProfileQuery } from '../../graphql/query/admin.profile.query' import { languages } from '../../i18n' interface FormData { user: { id: string username: string email: string language: string firstName: string lastName: string } password: string confirm: string } const Profile: NextPage = () => { const { t } = useTranslation() const [form] = useForm() const [saving, setSaving] = useState(false) const router = useRouter() const { loading } = useProfileQuery({ onCompleted: (next) => { form.setFieldsValue(next) }, onError(e) { void router.push('/') }, }) const [update] = useProfileUpdateMutation() const save = async (data: FormData) => { setSaving(true) try { const next = ( await update({ variables: { user: { ...data.user, password: data.password && data.password === data.confirm ? data.password : undefined, }, }, }) ).data form.setFieldsValue(next) await message.success(t('profile:updated')) } catch (e) { console.error('failed to save', e) await message.error(t('profile:updateError')) } setSaving(false) } return ( {t('profile:updateNow')} , ]} >
{ // TODO process errors await message.error(t('validation:mandatoryFieldsMissing')) }} labelCol={{ xs: { span: 24 }, sm: { span: 6 }, }} wrapperCol={{ xs: { span: 24 }, sm: { span: 18 }, }} > {() => ( ({ validator(_, value) { if (!value || getFieldValue('password') === value) { return Promise.resolve() } return Promise.reject(new Error(t('validation:passwordConfirmMismatch'))) }, }), ]} > )}
) } export default withAuth(Profile)