Add volunteerParticipationDetailsSet to store

This commit is contained in:
pikiou
2022-01-25 03:28:20 +01:00
parent 1e302e6c31
commit f890ab505b
5 changed files with 158 additions and 31 deletions

View File

@@ -13,6 +13,7 @@ import volunteerSet from "./volunteerSet"
import volunteerLogin from "./volunteerLogin"
import volunteerForgot from "./volunteerForgot"
import volunteerNotifsSet from "./volunteerNotifsSet"
import volunteerParticipationDetailsSet from "./volunteerParticipationDetailsSet"
import volunteerDayWishesSet from "./volunteerDayWishesSet"
import volunteerTeamWishesSet from "./volunteerTeamWishesSet"
import wishAdd from "./wishAdd"
@@ -33,6 +34,7 @@ export default (history: History) => ({
volunteerLogin,
volunteerForgot,
volunteerNotifsSet,
volunteerParticipationDetailsSet,
volunteerDayWishesSet,
volunteerTeamWishesSet,
wishAdd,

View File

@@ -0,0 +1,65 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementFetch } from "./utils"
import {
VolunteerParticipationDetails,
volunteerParticipationDetailsSet,
} from "../services/volunteers"
import { AppThunk, AppState } from "."
type StateVolunteerParticipationDetailsSet = {
entity?: VolunteerParticipationDetails
} & StateRequest
export const initialState: StateVolunteerParticipationDetailsSet = {
readyStatus: "idle",
}
const volunteerParticipationDetailsSetSlice = createSlice({
name: "volunteerParticipationDetailsSet",
initialState,
reducers: {
getRequesting: (_) => ({
readyStatus: "request",
}),
getSuccess: (_, { payload }: PayloadAction<VolunteerParticipationDetails>) => ({
readyStatus: "success",
entity: payload,
}),
getFailure: (_, { payload }: PayloadAction<string>) => ({
readyStatus: "failure",
error: payload,
}),
},
})
export default volunteerParticipationDetailsSetSlice.reducer
export const { getRequesting, getSuccess, getFailure } =
volunteerParticipationDetailsSetSlice.actions
export const fetchVolunteerParticipationDetailsSet = elementFetch(
volunteerParticipationDetailsSet,
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors du chargement des notifications: ${error.message}`)
)
const shouldFetchVolunteerParticipationDetailsSet = (state: AppState, id: number) =>
state.volunteerParticipationDetailsSet?.readyStatus !== "success" ||
(state.volunteerParticipationDetailsSet?.entity &&
state.volunteerParticipationDetailsSet?.entity?.id !== id)
export const fetchVolunteerParticipationDetailsSetIfNeed =
(id = 0, wishes: Partial<VolunteerParticipationDetails> = {}): AppThunk =>
(dispatch, getState) => {
let jwt = ""
if (!id) {
;({ id, jwt } = getState().auth)
}
if (shouldFetchVolunteerParticipationDetailsSet(getState(), id))
return dispatch(fetchVolunteerParticipationDetailsSet(jwt, id, wishes))
return null
}