Add teamList in store

This commit is contained in:
pikiou
2022-01-20 01:09:32 +01:00
parent 1011a293d0
commit d5eeb44d2f
10 changed files with 208 additions and 11 deletions

View File

@@ -2,9 +2,10 @@ import { History } from "history"
import { connectRouter } from "connected-react-router"
import auth from "./auth"
import wishAdd from "./wishAdd"
import wishList from "./wishList"
import javGameList from "./javGameList"
import preVolunteerAdd from "./preVolunteerAdd"
import preVolunteerCount from "./preVolunteerCount"
import teamList from "./teamList"
import volunteer from "./volunteer"
import volunteerAdd from "./volunteerAdd"
import volunteerList from "./volunteerList"
@@ -12,16 +13,17 @@ import volunteerSet from "./volunteerSet"
import volunteerLogin from "./volunteerLogin"
import volunteerForgot from "./volunteerForgot"
import volunteerNotifsSet from "./volunteerNotifsSet"
import preVolunteerAdd from "./preVolunteerAdd"
import preVolunteerCount from "./preVolunteerCount"
import wishAdd from "./wishAdd"
import wishList from "./wishList"
// Use inferred return type for making correctly Redux types
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default (history: History) => ({
auth,
wishAdd,
wishList,
javGameList,
preVolunteerAdd,
preVolunteerCount,
teamList,
volunteer,
volunteerAdd,
volunteerList,
@@ -29,8 +31,8 @@ export default (history: History) => ({
volunteerLogin,
volunteerForgot,
volunteerNotifsSet,
preVolunteerAdd,
preVolunteerCount,
wishAdd,
wishList,
router: connectRouter(history) as any,
// Register more reducers...
})

48
src/store/teamList.ts Normal file
View File

@@ -0,0 +1,48 @@
import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementListFetch } from "./utils"
import { Team, teamListGet } from "../services/teams"
import { AppThunk, AppState } from "."
const teamAdapter = createEntityAdapter<Team>()
export const initialState = teamAdapter.getInitialState({
readyStatus: "idle",
} as StateRequest)
const teamList = createSlice({
name: "teamList",
initialState,
reducers: {
getRequesting: (state) => {
state.readyStatus = "request"
},
getSuccess: (state, { payload }: PayloadAction<Team[]>) => {
state.readyStatus = "success"
teamAdapter.setAll(state, payload)
},
getFailure: (state, { payload }: PayloadAction<string>) => {
state.readyStatus = "failure"
state.error = payload
},
},
})
export default teamList.reducer
export const { getRequesting, getSuccess, getFailure } = teamList.actions
export const fetchTeamList = elementListFetch(
teamListGet,
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors du chargement des équipes: ${error.message}`)
)
const shouldFetchTeamList = (state: AppState) => state.teamList.readyStatus !== "success"
export const fetchTeamListIfNeed = (): AppThunk => (dispatch, getState) => {
if (shouldFetchTeamList(getState())) return dispatch(fetchTeamList())
return null
}