-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Profil - Uprava udajov #160
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8b0f351
Add edit profile buttons
Matushl 27c1cf5
Move School subform into own component
Matushl 17bfe13
`yarn add usehooks-ts`
Matushl babcd31
Add ProfileForm and improve SchoolSubForm
Matushl 90ad1c3
Fix typescript errror and cyclic imports
Matushl aa57008
remove async/await where wasn't neccesary
Matushl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import {useMutation, useQuery} from '@tanstack/react-query' | ||
import axios from 'axios' | ||
import {useRouter} from 'next/router' | ||
import {FC} from 'react' | ||
import {SubmitHandler, useForm} from 'react-hook-form' | ||
|
||
import styles from '@/components/FormItems/Form.module.scss' | ||
import {FormInput} from '@/components/FormItems/FormInput/FormInput' | ||
import {SelectOption} from '@/components/FormItems/FormSelect/FormSelect' | ||
import {IGeneralPostResponse} from '@/types/api/general' | ||
import {Profile} from '@/types/api/personal' | ||
import {AuthContainer} from '@/utils/AuthContainer' | ||
import {useSeminarInfo} from '@/utils/useSeminarInfo' | ||
|
||
import {Button} from '../Clickable/Clickable' | ||
import {SchoolSubForm} from '../SchoolSubForm/SchoolSubForm' | ||
|
||
export type ProfileFormValues = { | ||
first_name?: string | ||
last_name?: string | ||
phone?: string | ||
parent_phone?: string | ||
new_school_description?: string | ||
without_school: boolean | ||
school?: SelectOption | null | ||
school_not_found: boolean | ||
grade: number | '' | ||
} | ||
|
||
const defaultValues: ProfileFormValues = { | ||
first_name: '', | ||
last_name: '', | ||
phone: '', | ||
parent_phone: '', | ||
new_school_description: '', | ||
without_school: false, | ||
school: null, | ||
school_not_found: false, | ||
grade: '', | ||
} | ||
|
||
export const ProfileForm: FC = () => { | ||
const {isAuthed} = AuthContainer.useContainer() | ||
|
||
const {data} = useQuery({ | ||
queryKey: ['personal', 'profiles', 'myprofile'], | ||
queryFn: () => axios.get<Profile>(`/api/personal/profiles/myprofile`), | ||
enabled: isAuthed, | ||
}) | ||
const profile = data?.data | ||
const profileValues: ProfileFormValues = { | ||
first_name: profile?.first_name, | ||
last_name: profile?.last_name, | ||
phone: profile?.phone ?? '', | ||
parent_phone: profile?.parent_phone ?? '', | ||
new_school_description: '', | ||
without_school: profile?.school_id === 1, | ||
school: ({id: profile?.school.code, label: profile?.school.verbose_name} as SelectOption) ?? null, | ||
school_not_found: profile?.school_id === 0, | ||
grade: profile?.grade ?? '', | ||
} | ||
|
||
const {handleSubmit, control, watch, setValue} = useForm<ProfileFormValues>({ | ||
defaultValues, | ||
values: profileValues, | ||
}) | ||
|
||
const scrollToTop = () => { | ||
window.scrollTo({ | ||
top: 0, | ||
behavior: 'smooth', | ||
}) | ||
} | ||
|
||
const router = useRouter() | ||
|
||
const {seminar} = useSeminarInfo() | ||
|
||
const transformFormData = (data: ProfileFormValues) => ({ | ||
profile: { | ||
first_name: data.first_name, | ||
last_name: data.last_name, | ||
school: data.school?.id, | ||
phone: data.phone, | ||
parent_phone: data.parent_phone, | ||
grade: data.grade, | ||
}, | ||
new_school_description: data.new_school_description || '', | ||
}) | ||
|
||
const {mutate: submitFormData} = useMutation({ | ||
mutationFn: (data: ProfileFormValues) => { | ||
return axios.put<IGeneralPostResponse>(`/api/user/user`, transformFormData(data)) | ||
}, | ||
onSuccess: () => router.push(`/${seminar}/profil`), | ||
}) | ||
|
||
const onSubmit: SubmitHandler<ProfileFormValues> = async (data) => { | ||
submitFormData(data) | ||
} | ||
|
||
const requiredRule = {required: '* Toto pole nemôže byť prázdne.'} | ||
const phoneRule = { | ||
validate: (val?: string) => { | ||
if (val && !/^(\+\d{10,12})$/u.test(val.replaceAll(/\s+/gu, ''))) | ||
return '* Zadaj telefónne číslo vo formáte validnom formáte +421 123 456 789 alebo +421123456789.' | ||
}, | ||
} | ||
return ( | ||
<div> | ||
<form className={styles.form} onSubmit={handleSubmit(onSubmit)}> | ||
<FormInput control={control} name="first_name" label="krstné meno*" rules={requiredRule} /> | ||
<FormInput control={control} name="last_name" label="priezvisko*" rules={requiredRule} /> | ||
<SchoolSubForm control={control} watch={watch} setValue={setValue} /> | ||
<FormInput control={control} name="phone" label="telefónne číslo" rules={phoneRule} /> | ||
<FormInput control={control} name="parent_phone" label="telefónne číslo na rodiča" rules={phoneRule} /> | ||
<p style={{fontWeight: 'bold'}}>* takto označéné polia sú povinné</p> | ||
<Button type="submit" onClick={scrollToTop}> | ||
Uložiť údaje | ||
</Button> | ||
</form> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {NextPage} from 'next' | ||
|
||
import {PageLayout} from '@/components/PageLayout/PageLayout' | ||
import {ProfileForm} from '@/components/Profile/ProfileForm' | ||
|
||
const Profil: NextPage = () => ( | ||
<PageLayout contentWidth={2} title="Profil"> | ||
<ProfileForm /> | ||
</PageLayout> | ||
) | ||
|
||
export default Profil |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asi raz pridame daky eslint rule co trochu zjednoti/da bacha na async funkcie... tu deklarujeme fuknciu ako async ale nevolame v nej await, co nejakemu rulu vadi, ale v podstate je to tu v pohode