import { joiResolver } from '@hookform/resolvers/joi'; import { Add } from '@mui/icons-material'; import { Button, FormControlLabel, FormGroup, Switch, TextField } from '@mui/material'; import { Resume } from '@reactive-resume/schema'; import Joi from 'joi'; import { useTranslation } from 'next-i18next'; import { useEffect } from 'react'; import { Controller, useForm } from 'react-hook-form'; import { useMutation } from 'react-query'; import BaseModal from '@/components/shared/BaseModal'; import { RESUMES_QUERY } from '@/constants/index'; import { ServerError } from '@/services/axios'; import queryClient from '@/services/react-query'; import { createResume, CreateResumeParams } from '@/services/resume'; import { useAppDispatch, useAppSelector } from '@/store/hooks'; import { setModalState } from '@/store/modal/modalSlice'; type FormData = { name: string; slug: string; isPublic: boolean; }; const defaultState: FormData = { name: '', slug: '', isPublic: true, }; const schema = Joi.object({ name: Joi.string().required(), slug: Joi.string() .lowercase() .min(3) .regex(/^[a-z0-9-]+$/, 'only lowercase characters, numbers and hyphens') .required(), isPublic: Joi.boolean().default(true).required(), }); const CreateResumeModal: React.FC = () => { const { t } = useTranslation(); const dispatch = useAppDispatch(); const { open: isOpen } = useAppSelector((state) => state.modal['dashboard.create-resume']); const { mutateAsync, isLoading } = useMutation(createResume); const { reset, watch, control, setValue, handleSubmit } = useForm({ defaultValues: defaultState, resolver: joiResolver(schema), }); const name = watch('name'); useEffect(() => { const slug = name ? name .toLowerCase() .replace(/[^\w\s]/gi, '') .replace(/[ ]/gi, '-') : ''; setValue('slug', slug); }, [name, setValue]); const onSubmit = async ({ name, slug, isPublic }: FormData) => { await mutateAsync({ name, slug, public: isPublic }); await queryClient.invalidateQueries(RESUMES_QUERY); handleClose(); }; const handleClose = () => { dispatch(setModalState({ modal: 'dashboard.create-resume', state: { open: false } })); reset(); }; return ( } heading={t('modals.dashboard.create-resume.heading')} handleClose={handleClose} footerChildren={ } >

{t('modals.dashboard.create-resume.body')}

( ('modals.dashboard.create-resume.form.name.label')} error={!!fieldState.error} helperText={fieldState.error?.message} {...field} /> )} /> ( ('modals.dashboard.create-resume.form.slug.label')} error={!!fieldState.error} helperText={fieldState.error?.message} {...field} /> )} /> ('modals.dashboard.create-resume.form.public.label')} control={ } /> } />
); }; export default CreateResumeModal;