mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-09-11 22:06:29 +02:00
Add custom db read support
This commit is contained in:
@@ -41,4 +41,6 @@ export type AppThunk = ThunkAction<void, AppState, unknown, Action<string>>
|
||||
|
||||
export type EntitiesRequest<T> = EntityState<T> & StateRequest
|
||||
|
||||
export type ValueRequest<T> = { value?: T } & StateRequest
|
||||
|
||||
export default createStore
|
||||
|
45
src/store/preVolunteerCount.ts
Normal file
45
src/store/preVolunteerCount.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
|
||||
|
||||
import { StateRequest, toastError, elementValueFetch } from "./utils"
|
||||
import { preVolunteerCountGet } from "../services/preVolunteers"
|
||||
import { AppThunk, AppState } from "."
|
||||
|
||||
export const initialState: StateRequest & { value?: number } = { readyStatus: "idle" }
|
||||
|
||||
const preVolunteerCount = createSlice({
|
||||
name: "preVolunteerCount",
|
||||
initialState,
|
||||
reducers: {
|
||||
getRequesting: (state) => {
|
||||
state.readyStatus = "request"
|
||||
},
|
||||
getSuccess: (state, { payload }: PayloadAction<number>) => {
|
||||
state.readyStatus = "success"
|
||||
state.value = payload
|
||||
},
|
||||
getFailure: (state, { payload }: PayloadAction<string>) => {
|
||||
state.readyStatus = "failure"
|
||||
state.error = payload
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
export default preVolunteerCount.reducer
|
||||
export const { getRequesting, getSuccess, getFailure } = preVolunteerCount.actions
|
||||
|
||||
export const fetchPreVolunteerCount = elementValueFetch(
|
||||
preVolunteerCountGet,
|
||||
getRequesting,
|
||||
getSuccess,
|
||||
getFailure,
|
||||
(error: Error) => toastError(`Erreur lors du chargement des volunteers: ${error.message}`)
|
||||
)
|
||||
|
||||
const shouldFetchPreVolunteerCount = (state: AppState) =>
|
||||
state.preVolunteerCount.readyStatus !== "success"
|
||||
|
||||
export const fetchPreVolunteerCountIfNeed = (): AppThunk => (dispatch, getState) => {
|
||||
if (shouldFetchPreVolunteerCount(getState())) return dispatch(fetchPreVolunteerCount())
|
||||
|
||||
return null
|
||||
}
|
@@ -9,6 +9,7 @@ import volunteerAdd from "./volunteerAdd"
|
||||
import volunteerList from "./volunteerList"
|
||||
import volunteerSet from "./volunteerSet"
|
||||
import preVolunteerAdd from "./preVolunteerAdd"
|
||||
import preVolunteerCount from "./preVolunteerCount"
|
||||
|
||||
// Use inferred return type for making correctly Redux types
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
@@ -21,6 +22,7 @@ export default (history: History) => ({
|
||||
volunteerList,
|
||||
volunteerSet,
|
||||
preVolunteerAdd,
|
||||
preVolunteerCount,
|
||||
router: connectRouter(history) as any,
|
||||
// Register more reducers...
|
||||
})
|
||||
|
@@ -154,3 +154,33 @@ export function elementSet<Element>(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function elementValueFetch<Element>(
|
||||
elementListService: () => Promise<{
|
||||
data?: Element | undefined
|
||||
error?: Error | undefined
|
||||
}>,
|
||||
getRequesting: ActionCreatorWithoutPayload<string>,
|
||||
getSuccess: ActionCreatorWithPayload<Element, string>,
|
||||
getFailure: ActionCreatorWithPayload<string, string>,
|
||||
errorMessage: (error: Error) => void = (_error) => {
|
||||
/* Meant to be empty */
|
||||
},
|
||||
successMessage: () => void = () => {
|
||||
/* Meant to be empty */
|
||||
}
|
||||
): () => AppThunk {
|
||||
return (): AppThunk => async (dispatch) => {
|
||||
dispatch(getRequesting())
|
||||
|
||||
const { error, data } = await elementListService()
|
||||
|
||||
if (error) {
|
||||
dispatch(getFailure(error.message))
|
||||
errorMessage(error)
|
||||
} else {
|
||||
dispatch(getSuccess(data as Element))
|
||||
successMessage()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user