-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
120 additions
and
65 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
admin/src/scenes/settings/components/CohortGroupSelector.tsx
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,51 @@ | ||
import React from "react"; | ||
import CreatableSelect from "react-select/creatable"; | ||
import useCreateCohortGroup from "../lib/useCreateCohortGroup"; | ||
import useCohortGroups from "../lib/useCohortGroups"; | ||
import { CohortDto } from "snu-lib"; | ||
|
||
type propType = { | ||
cohort: CohortDto; | ||
setCohort: (cohort: CohortDto) => void; | ||
readOnly: boolean; | ||
}; | ||
|
||
type optionType = { value: string; label: string }; | ||
|
||
export default function CohortGroupSelector({ cohort, setCohort, readOnly }: propType) { | ||
const { isLoading, data } = useCohortGroups(); | ||
const options: optionType[] = data?.map((group) => ({ value: group._id, label: group.name })) || []; | ||
const { mutate } = useCreateCohortGroup(cohort); | ||
|
||
const handleChange = (selected: optionType) => { | ||
if (selected) { | ||
setCohort({ ...cohort, cohortGroupId: selected.value }); | ||
} | ||
}; | ||
|
||
const handleCreate = async (name: string) => { | ||
if (!window.confirm(`Voulez-vous créer le groupe de cohorte ${name} ?`)) return; | ||
mutate(name, { | ||
onSuccess: (data) => setCohort({ ...cohort, cohortGroupId: data._id }), | ||
}); | ||
}; | ||
|
||
return ( | ||
<CreatableSelect | ||
options={options} | ||
value={options?.find((o) => o.value === cohort.cohortGroupId)} | ||
isDisabled={isLoading || readOnly} | ||
onChange={handleChange} | ||
formatCreateLabel={(input) => `Créer le groupe "${input}"`} | ||
onCreateOption={handleCreate} | ||
placeholder="Sélectionnez un groupe de cohorte" | ||
styles={{ | ||
control: (base) => ({ | ||
...base, | ||
padding: "10px", | ||
minHeight: "40px", | ||
}), | ||
}} | ||
/> | ||
); | ||
} |
File renamed without changes.
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,6 +1,10 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { getCohortGroups } from "./cohortGroupService"; | ||
import { getCohortGroups } from "./cohortGroupRepository"; | ||
|
||
export default function useCohortGroups() { | ||
return useQuery({ queryFn: getCohortGroups, queryKey: ["cohortGroups"] }); | ||
return useQuery({ | ||
queryFn: getCohortGroups, | ||
queryKey: ["cohortGroups"], | ||
meta: { errorMessage: "Impossible de charger les groupes de cohortes." }, | ||
}); | ||
} |
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,11 +1,13 @@ | ||
import { queryClient } from "@/services/react-query"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { CohortDto, CohortGroupType } from "snu-lib"; | ||
import { createCohortGroup } from "./cohortGroupService"; | ||
import { createCohortGroup } from "./cohortGroupRepository"; | ||
import { toastr } from "react-redux-toastr"; | ||
|
||
export default function useCreateCohortGroup(cohort: CohortDto) { | ||
return useMutation({ | ||
mutationFn: async (name: string) => await createCohortGroup(name, cohort), | ||
onSuccess: (data) => queryClient.setQueryData(["cohortGroups"], (old: CohortGroupType[]) => [...old, data]), | ||
onError: () => toastr.error("Erreur", "Impossible de créer le groupe de cohortes."), | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,6 +1,15 @@ | ||
import { capture } from "@/sentry"; | ||
import { QueryCache, QueryClient } from "@tanstack/react-query"; | ||
import { toastr } from "react-redux-toastr"; | ||
|
||
export const queryClient = new QueryClient({ | ||
queryCache: new QueryCache({ onError: (error) => capture(error) }), | ||
queryCache: new QueryCache({ | ||
onError: (error, query) => { | ||
// https://tkdodo.eu/blog/breaking-react-querys-api-on-purpose#defining-on-demand-messages | ||
if (query.meta?.errorMessage) { | ||
toastr.error("Erreur", query.meta.errorMessage); | ||
} | ||
capture(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