mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-09-11 22:06:29 +02:00
Add volunteerDayWishesSet in store
This commit is contained in:
@@ -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,
|
||||
|
58
src/store/volunteerDayWishesSet.ts
Normal file
58
src/store/volunteerDayWishesSet.ts
Normal 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
|
||||
}
|
Reference in New Issue
Block a user