mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-09-11 22:06:29 +02:00
Add Ask Discord
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
import axios from "axios"
|
||||
|
||||
import mockStore from "../../utils/mockStore"
|
||||
import volunteer, {
|
||||
getRequesting,
|
||||
getSuccess,
|
||||
getFailure,
|
||||
fetchVolunteer,
|
||||
initialState,
|
||||
} from "../volunteer"
|
||||
import { Volunteer, volunteerExample } from "../../services/volunteers"
|
||||
|
||||
jest.mock("axios")
|
||||
|
||||
const mockData: Volunteer = volunteerExample
|
||||
const { id } = mockData
|
||||
const mockError = "Oops! Something went wrong."
|
||||
|
||||
describe("volunteer reducer", () => {
|
||||
it("should handle initial state correctly", () => {
|
||||
// @ts-expect-error
|
||||
expect(volunteer(undefined, {})).toEqual(initialState)
|
||||
})
|
||||
|
||||
it("should handle requesting correctly", () => {
|
||||
expect(volunteer(undefined, { type: getRequesting.type, payload: id })).toEqual({
|
||||
readyStatus: "request",
|
||||
})
|
||||
})
|
||||
|
||||
it("should handle success correctly", () => {
|
||||
expect(
|
||||
volunteer(undefined, {
|
||||
type: getSuccess.type,
|
||||
payload: mockData,
|
||||
})
|
||||
).toEqual({ readyStatus: "success", entity: mockData })
|
||||
})
|
||||
|
||||
it("should handle failure correctly", () => {
|
||||
expect(
|
||||
volunteer(undefined, {
|
||||
type: getFailure.type,
|
||||
payload: mockError,
|
||||
})
|
||||
).toEqual({ readyStatus: "failure", error: mockError })
|
||||
})
|
||||
})
|
||||
|
||||
describe("volunteer action", () => {
|
||||
it("fetches volunteer data successful", async () => {
|
||||
const { dispatch, getActions } = mockStore()
|
||||
const expectedActions = [
|
||||
{ type: getRequesting.type, payload: undefined },
|
||||
{ type: getSuccess.type, payload: mockData },
|
||||
]
|
||||
|
||||
// @ts-expect-error
|
||||
axios.get.mockResolvedValue({ data: mockData })
|
||||
|
||||
await dispatch(fetchVolunteer(id))
|
||||
expect(getActions()).toEqual(expectedActions)
|
||||
})
|
||||
|
||||
it("fetches volunteer data failed", async () => {
|
||||
const { dispatch, getActions } = mockStore()
|
||||
const expectedActions = [
|
||||
{ type: getRequesting.type },
|
||||
{ type: getFailure.type, payload: mockError },
|
||||
]
|
||||
|
||||
// @ts-expect-error
|
||||
axios.get.mockRejectedValue({ message: mockError })
|
||||
|
||||
await dispatch(fetchVolunteer(id))
|
||||
expect(getActions()).toEqual(expectedActions)
|
||||
})
|
||||
})
|
@@ -35,9 +35,9 @@ export const auth = createSlice({
|
||||
|
||||
export const { setCurrentUser, logoutUser } = auth.actions
|
||||
|
||||
export const selectAuthData = (state: AppState): AuthState => state.auth
|
||||
const selectAuthData = (state: AppState): AuthState => state.auth
|
||||
|
||||
export const selectRouter = (state: AppState): AppState["router"] => state.router
|
||||
const selectRouter = (state: AppState): AppState["router"] => state.router
|
||||
|
||||
export const selectUserJwtToken = createSelector(selectAuthData, (authData) => authData.jwt)
|
||||
|
||||
|
@@ -7,8 +7,8 @@ import announcementList from "./announcementList"
|
||||
import postulantAdd from "./postulantAdd"
|
||||
import teamList from "./teamList"
|
||||
import ui from "./ui"
|
||||
import volunteer from "./volunteer"
|
||||
import volunteerAdd from "./volunteerPartialAdd"
|
||||
import volunteerDiscordId from "./volunteerDiscordId"
|
||||
import volunteerList from "./volunteerList"
|
||||
import volunteerSet from "./volunteerSet"
|
||||
import volunteerLogin from "./volunteerLogin"
|
||||
@@ -29,8 +29,8 @@ export default (history: History) => ({
|
||||
postulantAdd,
|
||||
teamList,
|
||||
ui,
|
||||
volunteer,
|
||||
volunteerAdd,
|
||||
volunteerDiscordId,
|
||||
volunteerList,
|
||||
volunteerSet,
|
||||
volunteerLogin,
|
||||
|
@@ -1,53 +0,0 @@
|
||||
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
|
||||
|
||||
import { StateRequest, toastError, elementFetch } from "./utils"
|
||||
import { Volunteer } from "../services/volunteers"
|
||||
import { AppThunk, AppState } from "."
|
||||
import { volunteerGet } from "../services/volunteersAccessors"
|
||||
|
||||
type StateVolunteer = { entity?: Volunteer } & StateRequest
|
||||
|
||||
export const initialState: StateVolunteer = {
|
||||
readyStatus: "idle",
|
||||
}
|
||||
|
||||
const volunteer = createSlice({
|
||||
name: "volunteer",
|
||||
initialState,
|
||||
reducers: {
|
||||
getRequesting: (_) => ({
|
||||
readyStatus: "request",
|
||||
}),
|
||||
getSuccess: (_, { payload }: PayloadAction<Volunteer>) => ({
|
||||
readyStatus: "success",
|
||||
entity: payload,
|
||||
}),
|
||||
getFailure: (_, { payload }: PayloadAction<string>) => ({
|
||||
readyStatus: "failure",
|
||||
error: payload,
|
||||
}),
|
||||
},
|
||||
})
|
||||
|
||||
export default volunteer.reducer
|
||||
export const { getRequesting, getSuccess, getFailure } = volunteer.actions
|
||||
|
||||
export const fetchVolunteer = elementFetch(
|
||||
volunteerGet,
|
||||
getRequesting,
|
||||
getSuccess,
|
||||
getFailure,
|
||||
(error: Error) => toastError(`Erreur lors du chargement d'un bénévole: ${error.message}`)
|
||||
)
|
||||
|
||||
const shouldFetchVolunteer = (state: AppState, id: number) =>
|
||||
state.volunteer.readyStatus !== "success" ||
|
||||
(state.volunteer.entity && state.volunteer.entity.id !== id)
|
||||
|
||||
export const fetchVolunteerIfNeed =
|
||||
(id: number): AppThunk =>
|
||||
(dispatch, getState) => {
|
||||
if (shouldFetchVolunteer(getState(), id)) return dispatch(fetchVolunteer(id))
|
||||
|
||||
return null
|
||||
}
|
65
src/store/volunteerDiscordId.ts
Normal file
65
src/store/volunteerDiscordId.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { PayloadAction, createSlice, createSelector } from "@reduxjs/toolkit"
|
||||
|
||||
import { StateRequest, toastError, elementFetch } from "./utils"
|
||||
import { VolunteerDiscordId } from "../services/volunteers"
|
||||
import { AppThunk, AppState } from "."
|
||||
import { volunteerDiscordIdGet } from "../services/volunteersAccessors"
|
||||
|
||||
type StateVolunteerDiscordId = { entity?: VolunteerDiscordId } & StateRequest
|
||||
|
||||
export const initialState: StateVolunteerDiscordId = {
|
||||
readyStatus: "idle",
|
||||
}
|
||||
|
||||
const volunteerDiscordId = createSlice({
|
||||
name: "volunteerDiscordId",
|
||||
initialState,
|
||||
reducers: {
|
||||
getRequesting: (_) => ({
|
||||
readyStatus: "request",
|
||||
}),
|
||||
getSuccess: (_, { payload }: PayloadAction<VolunteerDiscordId>) => ({
|
||||
readyStatus: "success",
|
||||
entity: payload,
|
||||
}),
|
||||
getFailure: (_, { payload }: PayloadAction<string>) => ({
|
||||
readyStatus: "failure",
|
||||
error: payload,
|
||||
}),
|
||||
},
|
||||
})
|
||||
|
||||
export default volunteerDiscordId.reducer
|
||||
export const { getRequesting, getSuccess, getFailure } = volunteerDiscordId.actions
|
||||
|
||||
export const fetchVolunteerDiscordId = elementFetch(
|
||||
volunteerDiscordIdGet,
|
||||
getRequesting,
|
||||
getSuccess,
|
||||
getFailure,
|
||||
(error: Error) =>
|
||||
toastError(`Erreur lors du chargement du discordId d'un bénévole: ${error.message}`)
|
||||
)
|
||||
|
||||
const shouldFetchVolunteerDiscordId = (state: AppState, id: number) =>
|
||||
state.volunteerDiscordId.readyStatus !== "success" ||
|
||||
(state.volunteerDiscordId.entity && state.volunteerDiscordId.entity.id !== id)
|
||||
|
||||
export const fetchVolunteerDiscordIdIfNeed =
|
||||
(id = 0): AppThunk =>
|
||||
(dispatch, getState) => {
|
||||
let jwt = ""
|
||||
|
||||
if (!id) {
|
||||
;({ jwt, id } = getState().auth)
|
||||
}
|
||||
if (shouldFetchVolunteerDiscordId(getState(), id))
|
||||
return dispatch(fetchVolunteerDiscordId(jwt, id))
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export const selectVolunteerDiscordId = createSelector(
|
||||
(state: AppState) => state,
|
||||
(state): number | undefined => state.volunteerDiscordId?.entity?.id
|
||||
)
|
Reference in New Issue
Block a user