mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-06-10 09:34:21 +02:00
60 lines
2.0 KiB
TypeScript
60 lines
2.0 KiB
TypeScript
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
|
|
|
|
import { StateRequest, toastError, elementFetch } from "./utils"
|
|
import { VolunteerTeamWishes } from "../services/volunteers"
|
|
import { AppThunk, AppState } from "."
|
|
import { volunteerTeamWishesSet } from "../services/volunteersAccessors"
|
|
|
|
type StateVolunteerTeamWishesSet = { entity?: VolunteerTeamWishes } & StateRequest
|
|
|
|
export const initialState: StateVolunteerTeamWishesSet = {
|
|
readyStatus: "idle",
|
|
}
|
|
|
|
const volunteerTeamWishesSetSlice = createSlice({
|
|
name: "volunteerTeamWishesSet",
|
|
initialState,
|
|
reducers: {
|
|
getRequesting: (_) => ({
|
|
readyStatus: "request",
|
|
}),
|
|
getSuccess: (_, { payload }: PayloadAction<VolunteerTeamWishes>) => ({
|
|
readyStatus: "success",
|
|
entity: payload,
|
|
}),
|
|
getFailure: (_, { payload }: PayloadAction<string>) => ({
|
|
readyStatus: "failure",
|
|
error: payload,
|
|
}),
|
|
},
|
|
})
|
|
|
|
export default volunteerTeamWishesSetSlice.reducer
|
|
export const { getRequesting, getSuccess, getFailure } = volunteerTeamWishesSetSlice.actions
|
|
|
|
export const fetchVolunteerTeamWishesSet = elementFetch(
|
|
volunteerTeamWishesSet,
|
|
getRequesting,
|
|
getSuccess,
|
|
getFailure,
|
|
(error: Error) => toastError(`Erreur lors du chargement des choix d'équipe: ${error.message}`)
|
|
)
|
|
|
|
const shouldFetchVolunteerTeamWishesSet = (state: AppState, id: number) =>
|
|
state.volunteerTeamWishesSet?.readyStatus !== "success" ||
|
|
(state.volunteerTeamWishesSet?.entity && state.volunteerTeamWishesSet?.entity?.id !== id)
|
|
|
|
export const fetchVolunteerTeamWishesSetIfNeed =
|
|
(id = 0, wishes: Partial<VolunteerTeamWishes> = {}): AppThunk =>
|
|
(dispatch, getState) => {
|
|
let jwt = ""
|
|
|
|
if (!id) {
|
|
;({ jwt, id } = getState().auth)
|
|
}
|
|
if (shouldFetchVolunteerTeamWishesSet(getState(), id))
|
|
return dispatch(fetchVolunteerTeamWishesSet(jwt, id, wishes))
|
|
|
|
return null
|
|
}
|