-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from dump-hr/speakers-admin
Speakers admin
- Loading branch information
Showing
23 changed files
with
630 additions
and
19 deletions.
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
.turbo | ||
.env.local | ||
dist | ||
dist | ||
apps/api/db/migrations/meta/_journal.json |
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,23 @@ | ||
import { SpeakerDto, SpeakerModifyDto } from '@ddays-app/types'; | ||
import toast from 'react-hot-toast'; | ||
import { useMutation, useQueryClient } from 'react-query'; | ||
|
||
import { api } from '..'; | ||
|
||
const speakerCreate = async (dto: SpeakerModifyDto) => { | ||
return await api.post<SpeakerModifyDto, SpeakerDto>('/speaker', dto); | ||
}; | ||
|
||
export const useSpeakerCreate = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation(speakerCreate, { | ||
onSuccess: () => { | ||
queryClient.invalidateQueries(['speaker']); | ||
toast.success('Speaker uspješno dodan!'); | ||
}, | ||
onError: (error: string) => { | ||
toast.error(error); | ||
}, | ||
}); | ||
}; |
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 { SpeakerDto } from '@ddays-app/types'; | ||
import { QueryOptions, useQuery } from 'react-query'; | ||
|
||
import { api } from '..'; | ||
|
||
const speakerGetAll = () => { | ||
return api.get<never, SpeakerDto[]>('speaker'); | ||
}; | ||
|
||
export const useSpeakerGetAll = (options?: QueryOptions<SpeakerDto[]>) => { | ||
return useQuery(['speaker'], speakerGetAll, options); | ||
}; |
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,18 @@ | ||
import { SpeakerDto } from '@ddays-app/types'; | ||
import { QueryOptions, useQuery } from 'react-query'; | ||
|
||
import { api } from '..'; | ||
|
||
const speakerGetOne = async (id: number) => { | ||
return await api.get<never, SpeakerDto>(`/speaker/${id}`); | ||
}; | ||
|
||
export const useSpeakerGetOne = ( | ||
id?: number, | ||
options?: QueryOptions<SpeakerDto>, | ||
) => { | ||
return useQuery(['speaker', id], () => speakerGetOne(id!), { | ||
enabled: !!id, | ||
...options, | ||
}); | ||
}; |
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,23 @@ | ||
import { SpeakerDto } from '@ddays-app/types'; | ||
import toast from 'react-hot-toast'; | ||
import { useMutation, useQueryClient } from 'react-query'; | ||
|
||
import { api } from '..'; | ||
|
||
const speakerRemove = async (id: number) => { | ||
return await api.delete<never, SpeakerDto>(`/speaker/${id}`); | ||
}; | ||
|
||
export const useSpeakerRemove = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation(speakerRemove, { | ||
onSuccess: () => { | ||
queryClient.invalidateQueries(['speaker']); | ||
toast.success('Speaker uspješno uklonjen!'); | ||
}, | ||
onError: (error: string) => { | ||
toast.error(error); | ||
}, | ||
}); | ||
}; |
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,22 @@ | ||
import toast from 'react-hot-toast'; | ||
import { useMutation, useQueryClient } from 'react-query'; | ||
|
||
import { api } from '..'; | ||
|
||
const speakerRemovePhoto = async (id: number | undefined) => { | ||
return await api.delete(`/speaker/photo/${id}`); | ||
}; | ||
|
||
export const useSpeakerRemovePhoto = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation(speakerRemovePhoto, { | ||
onSuccess: () => { | ||
queryClient.invalidateQueries(['speaker']); | ||
toast.success('Slika uspješno izbrisana'); | ||
}, | ||
onError: (error: string) => { | ||
toast.error(error); | ||
}, | ||
}); | ||
}; |
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,27 @@ | ||
import { SpeakerDto, SpeakerModifyDto } from '@ddays-app/types'; | ||
import toast from 'react-hot-toast'; | ||
import { useMutation, useQueryClient } from 'react-query'; | ||
|
||
import { api } from '..'; | ||
|
||
const speakerUpdate = async (dto: SpeakerModifyDto & { id: number }) => { | ||
return await api.patch<SpeakerModifyDto, SpeakerDto>(`/speaker/${dto.id}`, { | ||
...dto, | ||
id: undefined, | ||
}); | ||
}; | ||
|
||
export const useSpeakerUpdate = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation(speakerUpdate, { | ||
onSuccess: (updatedSpeaker) => { | ||
queryClient.invalidateQueries(['speaker']); | ||
queryClient.invalidateQueries(['speaker', updatedSpeaker.id]); | ||
toast.success('Uređivanje speakera uspješno izvršeno!'); | ||
}, | ||
onError: (error: string) => { | ||
toast.error(error); | ||
}, | ||
}); | ||
}; |
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,28 @@ | ||
import toast from 'react-hot-toast'; | ||
import { useMutation, useQueryClient } from 'react-query'; | ||
|
||
import { api } from '..'; | ||
|
||
const speakerUpdatePhoto = async (speakerFile: { | ||
id: number | undefined; | ||
file: File; | ||
}) => { | ||
const data = new FormData(); | ||
data.append('file', speakerFile.file); | ||
|
||
return await api.patchForm(`/speaker/photo/${speakerFile.id}`, data); | ||
}; | ||
|
||
export const useSpeakerUpdatePhoto = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation(speakerUpdatePhoto, { | ||
onSuccess: () => { | ||
queryClient.invalidateQueries(['speaker']); | ||
toast.success('Slika uspješno uploadana'); | ||
}, | ||
onError: (error: string) => { | ||
toast.error(error); | ||
}, | ||
}); | ||
}; |
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
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,77 @@ | ||
import { SpeakerModifyDto } from '@ddays-app/types'; | ||
import { classValidatorResolver } from '@hookform/resolvers/class-validator'; | ||
import { useForm } from 'react-hook-form'; | ||
|
||
import { useSpeakerCreate } from '../api/speaker/useSpeakerCreate'; | ||
import { useSpeakerGetOne } from '../api/speaker/useSpeakerGetOne'; | ||
import { useSpeakerUpdate } from '../api/speaker/useSpeakerUpdate'; | ||
import { Button } from '../components/Button'; | ||
import { InputHandler } from '../components/InputHandler'; | ||
import { Question, QuestionType } from '../types/question'; | ||
|
||
type SpeakerFormProps = { | ||
id?: number; | ||
onSuccess: () => void; | ||
}; | ||
|
||
export const SpeakerForm: React.FC<SpeakerFormProps> = ({ id, onSuccess }) => { | ||
const { data: speaker, isLoading } = useSpeakerGetOne(id); | ||
|
||
const createSpeaker = useSpeakerCreate(); | ||
const updateSpeaker = useSpeakerUpdate(); | ||
|
||
const questions: Question[] = [ | ||
{ | ||
id: 'firstName', | ||
type: QuestionType.Field, | ||
title: 'Ime', | ||
defaultValue: speaker?.firstName, | ||
}, | ||
{ | ||
id: 'lastName', | ||
type: QuestionType.Field, | ||
title: 'Prezime', | ||
defaultValue: speaker?.lastName, | ||
}, | ||
{ | ||
id: 'title', | ||
type: QuestionType.Field, | ||
title: 'Titula', | ||
defaultValue: speaker?.title, | ||
}, | ||
{ | ||
id: 'companyId', | ||
type: QuestionType.Number, | ||
title: 'CompanyId (0 for null)', | ||
defaultValue: speaker?.companyId, | ||
}, | ||
]; | ||
|
||
const form = useForm<SpeakerModifyDto>({ | ||
resolver: classValidatorResolver(SpeakerModifyDto), | ||
}); | ||
|
||
if (id && isLoading) { | ||
return <div>Loading...</div>; | ||
} | ||
|
||
return ( | ||
<> | ||
{questions.map((q) => { | ||
return <InputHandler question={q} form={form} key={q.id} />; | ||
})} | ||
|
||
<Button | ||
onClick={form.handleSubmit(async (formData) => { | ||
if (id) { | ||
await updateSpeaker.mutateAsync({ ...formData, id }); | ||
} else { | ||
await createSpeaker.mutateAsync(formData); | ||
} | ||
onSuccess(); | ||
})}> | ||
Submit | ||
</Button> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.