Refactor Notifications into Asks, replace all <Link> by <a>

This commit is contained in:
pikiou
2022-03-29 06:31:03 +02:00
parent 70b53c5af8
commit 4273ea1603
25 changed files with 636 additions and 663 deletions

View File

@@ -14,7 +14,7 @@ import volunteerList from "./volunteerList"
import volunteerSet from "./volunteerSet"
import volunteerLogin from "./volunteerLogin"
import volunteerForgot from "./volunteerForgot"
import volunteerNotifsSet from "./volunteerNotifsSet"
import volunteerAsksSet from "./volunteerAsksSet"
import volunteerParticipationDetailsSet from "./volunteerParticipationDetailsSet"
import volunteerDayWishesSet from "./volunteerDayWishesSet"
import volunteerTeamWishesSet from "./volunteerTeamWishesSet"
@@ -37,7 +37,7 @@ export default (history: History) => ({
volunteerSet,
volunteerLogin,
volunteerForgot,
volunteerNotifsSet,
volunteerAsksSet,
volunteerParticipationDetailsSet,
volunteerDayWishesSet,
volunteerTeamWishesSet,

View File

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

View File

@@ -1,78 +0,0 @@
import { PayloadAction, createSlice, createSelector } from "@reduxjs/toolkit"
import get from "lodash/get"
import { StateRequest, toastError, elementFetch } from "./utils"
import { VolunteerNotifs } from "../services/volunteers"
import { AppThunk, AppState } from "."
import { volunteerNotifsSet } from "../services/volunteersAccessors"
type StateVolunteerNotifsSet = { entity?: VolunteerNotifs } & StateRequest
export const initialState: StateVolunteerNotifsSet = {
readyStatus: "idle",
}
const volunteerNotifsSetSlice = createSlice({
name: "volunteerNotifsSet",
initialState,
reducers: {
getRequesting: (_) => ({
readyStatus: "request",
}),
getSuccess: (_, { payload }: PayloadAction<VolunteerNotifs>) => ({
readyStatus: "success",
entity: payload,
}),
getFailure: (_, { payload }: PayloadAction<string>) => ({
readyStatus: "failure",
error: payload,
}),
},
})
export default volunteerNotifsSetSlice.reducer
export const { getRequesting, getSuccess, getFailure } = volunteerNotifsSetSlice.actions
export const fetchVolunteerNotifsSet = elementFetch(
volunteerNotifsSet,
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors du chargement des notifications: ${error.message}`)
)
const shouldFetchVolunteerNotifsSet = (state: AppState, id: number) =>
state.volunteerNotifsSet?.readyStatus !== "success" ||
(state.volunteerNotifsSet?.entity && state.volunteerNotifsSet?.entity?.id !== id)
export const fetchVolunteerNotifsSetIfNeed =
(id = 0, notif: Partial<VolunteerNotifs> = {}): AppThunk =>
(dispatch, getState) => {
let jwt = ""
if (!id) {
;({ jwt, id } = getState().auth)
}
if (shouldFetchVolunteerNotifsSet(getState(), id))
return dispatch(fetchVolunteerNotifsSet(jwt, id, notif))
return null
}
export const openedNotifsIds = [1, 2, 3]
export const selectVolunteerNotifsSetState = (state: AppState): StateVolunteerNotifsSet =>
state.volunteerNotifsSet
export const selectHiddenNotifs = createSelector(selectVolunteerNotifsSetState, (notifState) =>
get(notifState, "entity.hiddenNotifs", [])
)
export const selectWaitingsNotifs = createSelector(selectHiddenNotifs, (hidden) =>
openedNotifsIds.filter((id) => !hidden.find((hiddenId: number) => hiddenId === id))
)
export const hasWaitingNotifs = createSelector(
selectWaitingsNotifs,
(waiting) => waiting.length > 0
)