import { useEffect, useReducer } from 'react'; import { useForm, Controller, FormProvider } from 'react-hook-form'; import { initialState, reducer } from './reducer'; import { ProfileFormProps } from './ProfileFormProps.interface'; import { FormMode } from '../../enums/formMode'; import httpClient from '../../httpClient/httpClient'; import { AxiosError, AxiosInstance, AxiosResponse } from 'axios'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faSave, faXmark } from '@fortawesome/free-solid-svg-icons'; import { ScreenSize } from '../../enums/screenSize'; import { useBreakpoints } from '../../hooks/breakpoints/UseBreakpoints'; import { useAppContext } from '../../hooks/appContext/UseAppContext'; import { Skill } from '../skills/Skill.interface'; import { useSkills } from '../../hooks/skills/UseSkills'; import MultiSelectDropdown from '../multiSelectDropdown/MultiSelectDropdown'; const ProfileForm = ({ isDrawerOpen, mode, onOpenClose, profileId }: ProfileFormProps) => { const [state, dispatch] = useReducer(reducer, initialState); const appContext = useAppContext(); const { skills } = useSkills(); const projectSkills: Skill[] = [] const defaultValues = { name: '', headline: '', summary: '', userId: '', skills: projectSkills, statusId: 1 } const methods = useForm({ defaultValues: defaultValues }); // const watchRepoName = methods.watch(['repoName']) const { screenSize } = useBreakpoints(); const onSetSelectedSkills = (selectedSkill: Skill) => { const selectedSkillExists = state.selectedSkills.find((skill) => skill.id === selectedSkill.id) let newSelectedSkills: Skill[]; if (selectedSkillExists) { newSelectedSkills = state.selectedSkills.filter((skill) => skill.id !== selectedSkillExists.id) } else { newSelectedSkills = [...state.selectedSkills, selectedSkill] } dispatch({ type: 'SET_SELECTED_SKILLS', payload: newSelectedSkills }) methods.setValue('skills', newSelectedSkills) } const onCancel = () => { methods.reset(defaultValues); dispatch({ type: 'SET_IS_DISABLED', payload: false }); onOpenClose(FormMode.CANCEL); }; const onSubmit = async (data: any) => { try { dispatch({ type: 'SET_IS_LOADING', payload: true }); console.log(data) if (!profileId) { const newData = { ...data, createdBy: appContext.state.userProfile.userPrincipalName, userId: appContext.state.userProfile.userPrincipalName } console.log(newData) await httpClient.post(`api/profiles`, newData); } else { const newData = { ...data, updatedBy: appContext.state.userProfile.userPrincipalName } console.log(newData) await httpClient.put(`api/profiles/${profileId}`, newData); } methods.reset(defaultValues); dispatch({ type: 'SET_IS_DISABLED', payload: false }); onOpenClose(FormMode.CANCEL); } catch (error) { const axiosError = error as AxiosError; console.log(axiosError) dispatch({ type: 'SET_ERROR', payload: axiosError.message }); } finally { dispatch({ type: 'SET_IS_LOADING', payload: false }); } }; useEffect(() => { if (mode === FormMode.VIEW) { dispatch({ type: 'SET_IS_DISABLED', payload: true }); } }, [mode]); useEffect(() => { const getProfile = async () => { try { dispatch({ type: 'SET_IS_LOADING', payload: true }); const response: AxiosResponse = await httpClient.get( `api/profiles/${profileId}` ); const profile = response.data; if (profile.skills) { dispatch({ type: 'SET_SELECTED_SKILLS', payload: profile.skills }) } methods.reset(profile); } catch (error) { const axiosError = error as AxiosError; console.log(axiosError) dispatch({ type: 'SET_ERROR', payload: axiosError.message }); } finally { dispatch({ type: 'SET_IS_LOADING', payload: false }); } }; if (profileId && isDrawerOpen) { getProfile(); } }, [profileId]); useEffect(() => { if (skills) { dispatch({ type: 'SET_SKILL_OPTIONS', payload: skills }) } }, [skills]) return (