Add login api

This commit is contained in:
pikiou
2022-01-03 17:55:47 +01:00
parent 4540e92171
commit 395955f32a
36 changed files with 340 additions and 437 deletions

View File

@@ -13,27 +13,7 @@ import { JavGame } from "../../services/javGames"
jest.mock("axios")
const mockFrenchData: any[] = [
{
id: 5,
titre: "6 qui prend!",
auteur: "Wolfgang Kramer",
editeur: "(uncredited) , Design Edge , B",
minJoueurs: 2,
maxJoueurs: 10,
duree: 45,
type: "Ambiance",
poufpaf: "0-9-2/6-qui-prend-6-nimmt",
bggPhoto:
"https://cf.geekdo-images.com/thumb/img/lzczxR5cw7an7tRWeHdOrRtLyes=/fit-in/200x150/pic772547.jpg",
bggId: 432,
exemplaires: 1,
dispoPret: 1,
nonRangee: 0,
ean: "3421272101313",
},
]
const mockEnglishData: JavGame[] = [
const mockData: JavGame[] = [
{
id: 5,
title: "6 qui prend!",
@@ -70,14 +50,12 @@ describe("JavGameList reducer", () => {
})
it("should handle success correctly", () => {
expect(JavGameList(undefined, { type: getSuccess.type, payload: mockEnglishData })).toEqual(
{
...initialState,
readyStatus: "success",
ids: _.map(mockEnglishData, "id"),
entities: _.keyBy(mockEnglishData, "id"),
}
)
expect(JavGameList(undefined, { type: getSuccess.type, payload: mockData })).toEqual({
...initialState,
readyStatus: "success",
ids: _.map(mockData, "id"),
entities: _.keyBy(mockData, "id"),
})
})
it("should handle failure correctly", () => {
@@ -94,11 +72,11 @@ describe("JavGameList action", () => {
const { dispatch, getActions } = mockStore()
const expectedActions = [
{ type: getRequesting.type, payload: undefined },
{ type: getSuccess.type, payload: mockEnglishData },
{ type: getSuccess.type, payload: mockData },
]
// @ts-expect-error
axios.get.mockResolvedValue({ data: mockFrenchData })
axios.get.mockResolvedValue({ data: mockData })
await dispatch(fetchJavGameList())
expect(getActions()).toEqual(expectedActions)

View File

@@ -12,23 +12,7 @@ import { Volunteer } from "../../services/volunteers"
jest.mock("axios")
const mockFrenchData: any = {
id: 1,
nom: "Aupeix",
prenom: "Amélie",
mail: "pakouille.lakouille@yahoo.fr",
telephone: "0675650392",
photo: "images/volunteers/$taille/amélie_aupeix.jpg",
alimentation: "Végétarien",
majeur: 1,
privilege: 0,
actif: 0,
commentaire: "",
horodatage: "0000-00-00",
passe: "$2y$10$fSxY9AIuxSiEjwF.J3eXGubIxUPlobkyRrNIal8ASimSjNj4SR.9O",
}
const mockEnglishData: Volunteer = {
const mockData: Volunteer = {
id: 1,
lastname: "Aupeix",
firstname: "Amélie",
@@ -40,10 +24,10 @@ const mockEnglishData: Volunteer = {
privileges: 0,
active: 0,
comment: "",
timestamp: "0000-00-00",
timestamp: new Date(0),
password: "$2y$10$fSxY9AIuxSiEjwF.J3eXGubIxUPlobkyRrNIal8ASimSjNj4SR.9O",
}
const { id } = mockEnglishData
const { id } = mockData
const mockError = "Oops! Something went wrong."
describe("volunteer reducer", () => {
@@ -62,9 +46,9 @@ describe("volunteer reducer", () => {
expect(
volunteer(undefined, {
type: getSuccess.type,
payload: mockEnglishData,
payload: mockData,
})
).toEqual({ readyStatus: "success", entity: mockEnglishData })
).toEqual({ readyStatus: "success", entity: mockData })
})
it("should handle failure correctly", () => {
@@ -82,11 +66,11 @@ describe("volunteer action", () => {
const { dispatch, getActions } = mockStore()
const expectedActions = [
{ type: getRequesting.type, payload: undefined },
{ type: getSuccess.type, payload: mockEnglishData },
{ type: getSuccess.type, payload: mockData },
]
// @ts-expect-error
axios.get.mockResolvedValue({ data: mockFrenchData })
axios.get.mockResolvedValue({ data: mockData })
await dispatch(fetchVolunteer(id))
expect(getActions()).toEqual(expectedActions)

View File

@@ -13,25 +13,7 @@ import { Volunteer } from "../../services/volunteers"
jest.mock("axios")
const mockFrenchData: any[] = [
{
id: 1,
nom: "Aupeix",
prenom: "Amélie",
mail: "pakouille.lakouille@yahoo.fr",
telephone: "0675650392",
photo: "images/volunteers/$taille/amélie_aupeix.jpg",
alimentation: "Végétarien",
majeur: 1,
privilege: 0,
actif: 0,
commentaire: "",
horodatage: "0000-00-00",
passe: "$2y$10$fSxY9AIuxSiEjwF.J3eXGubIxUPlobkyRrNIal8ASimSjNj4SR.9O",
},
]
const mockEnglishData: Volunteer[] = [
const mockData: Volunteer[] = [
{
id: 1,
lastname: "Aupeix",
@@ -44,7 +26,7 @@ const mockEnglishData: Volunteer[] = [
privileges: 0,
active: 0,
comment: "",
timestamp: "0000-00-00",
timestamp: new Date(0),
password: "$2y$10$fSxY9AIuxSiEjwF.J3eXGubIxUPlobkyRrNIal8ASimSjNj4SR.9O",
},
]
@@ -65,13 +47,11 @@ describe("volunteerList reducer", () => {
})
it("should handle success correctly", () => {
expect(
volunteerList(undefined, { type: getSuccess.type, payload: mockEnglishData })
).toEqual({
expect(volunteerList(undefined, { type: getSuccess.type, payload: mockData })).toEqual({
...initialState,
readyStatus: "success",
ids: _.map(mockEnglishData, "id"),
entities: _.keyBy(mockEnglishData, "id"),
ids: _.map(mockData, "id"),
entities: _.keyBy(mockData, "id"),
})
})
@@ -89,11 +69,11 @@ describe("volunteerList action", () => {
const { dispatch, getActions } = mockStore()
const expectedActions = [
{ type: getRequesting.type, payload: undefined },
{ type: getSuccess.type, payload: mockEnglishData },
{ type: getSuccess.type, payload: mockData },
]
// @ts-expect-error
axios.get.mockResolvedValue({ data: mockFrenchData })
axios.get.mockResolvedValue({ data: mockData })
await dispatch(fetchVolunteerList())
expect(getActions()).toEqual(expectedActions)

View File

@@ -32,7 +32,8 @@ export const fetchPreVolunteerCount = elementValueFetch(
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors du chargement des volunteers: ${error.message}`)
(error: Error) =>
toastError(`Erreur lors du chargement des bénévoles potentiels: ${error.message}`)
)
const shouldFetchPreVolunteerCount = (state: AppState) =>

View File

@@ -8,6 +8,7 @@ import volunteer from "./volunteer"
import volunteerAdd from "./volunteerAdd"
import volunteerList from "./volunteerList"
import volunteerSet from "./volunteerSet"
import volunteerLogin from "./volunteerLogin"
import preVolunteerAdd from "./preVolunteerAdd"
import preVolunteerCount from "./preVolunteerCount"
@@ -21,6 +22,7 @@ export default (history: History) => ({
volunteerAdd,
volunteerList,
volunteerSet,
volunteerLogin,
preVolunteerAdd,
preVolunteerCount,
router: connectRouter(history) as any,

View File

@@ -33,32 +33,32 @@ export function toastSuccess(message: string): void {
}
export function elementFetch<Element>(
elementService: (id: number) => Promise<{
elementService: (...idArgs: any[]) => 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 */
}
): (id: number) => AppThunk {
return (id: number): AppThunk =>
errorMessage?: (error: Error) => void,
successMessage?: (data: Element) => void
): (...idArgs: any[]) => AppThunk {
return (...idArgs: any[]): AppThunk =>
async (dispatch) => {
dispatch(getRequesting())
const { error, data } = await elementService(id)
const { error, data } = await elementService(...idArgs)
if (error) {
dispatch(getFailure(error.message))
errorMessage(error)
if (errorMessage) {
errorMessage(error)
}
} else {
dispatch(getSuccess(data as Element))
successMessage()
if (successMessage) {
successMessage(data as Element)
}
}
}
}

View File

@@ -36,7 +36,7 @@ export const fetchVolunteer = elementFetch(
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors du chargement d'un volunteer: ${error.message}`)
(error: Error) => toastError(`Erreur lors du chargement d'un bénévole: ${error.message}`)
)
const shouldFetchVolunteer = (state: AppState, id: number) =>

View File

@@ -33,6 +33,6 @@ export const fetchVolunteerAdd = elementAddFetch(
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors de l'ajout d'une volunteer: ${error.message}`),
(error: Error) => toastError(`Erreur lors de l'ajout d'un bénévole: ${error.message}`),
() => toastSuccess("Volunteer ajoutée !")
)

View File

@@ -36,7 +36,7 @@ export const fetchVolunteerList = elementListFetch(
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors du chargement des volunteers: ${error.message}`)
(error: Error) => toastError(`Erreur lors du chargement des bénévoles: ${error.message}`)
)
const shouldFetchVolunteerList = (state: AppState) => state.volunteerList.readyStatus !== "success"

View File

@@ -0,0 +1,43 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { StateRequest, elementFetch } from "./utils"
import { VolunteerLogin, volunteerLogin } from "../services/volunteers"
import { setJWT } from "../services/auth"
type StateVolunteer = { entity?: VolunteerLogin } & StateRequest
export const initialState: StateVolunteer = {
readyStatus: "idle",
}
const volunteerLoginSlice = createSlice({
name: "volunteer",
initialState,
reducers: {
getRequesting: (_) => ({
readyStatus: "request",
}),
getSuccess: (_, { payload }: PayloadAction<VolunteerLogin>) => ({
readyStatus: "success",
entity: payload,
}),
getFailure: (_, { payload }: PayloadAction<string>) => ({
readyStatus: "failure",
error: payload,
}),
},
})
export default volunteerLoginSlice.reducer
export const { getRequesting, getSuccess, getFailure } = volunteerLoginSlice.actions
export const fetchVolunteerLogin = elementFetch(
volunteerLogin,
getRequesting,
getSuccess,
getFailure,
undefined,
(login: VolunteerLogin) => {
setJWT(login.jwt)
}
)

View File

@@ -33,6 +33,6 @@ export const fetchVolunteerSet = elementSet(
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors de la modification d'un volunteer: ${error.message}`),
() => toastSuccess("Volunteer modifié !")
(error: Error) => toastError(`Erreur lors de la modification d'un bénévole: ${error.message}`),
() => toastSuccess("Bénévole modifié !")
)

View File

@@ -33,6 +33,6 @@ export const fetchWishAdd = elementAddFetch(
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors de l'ajout d'une wish: ${error.message}`),
() => toastSuccess("Wish ajoutée !")
(error: Error) => toastError(`Erreur lors de l'ajout d'une envie: ${error.message}`),
() => toastSuccess("Envie ajoutée !")
)

View File

@@ -34,7 +34,7 @@ export const fetchWishList = elementListFetch(
getRequesting,
getSuccess,
getFailure,
(error: Error) => toastError(`Erreur lors du chargement des wishes: ${error.message}`)
(error: Error) => toastError(`Erreur lors du chargement des envies: ${error.message}`)
)
const shouldFetchWishList = (state: AppState) => state.wishList.readyStatus !== "success"