Add volunteerDayWishesSet in store

This commit is contained in:
pikiou
2022-01-21 07:34:50 +01:00
parent fc65a2d42b
commit 93e82a36ee
11 changed files with 256 additions and 38 deletions

View File

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

View File

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