mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-09-11 13:56:29 +02:00
Add notifications to home page
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import axios from "axios"
|
||||
import _ from "lodash"
|
||||
|
||||
import config from "../config"
|
||||
import { axiosConfig } from "./auth"
|
||||
@@ -123,7 +124,9 @@ export default class ServiceAccessors<
|
||||
}
|
||||
}
|
||||
|
||||
customPost(apiName: string): (params: any) => Promise<{
|
||||
customPost<InputElements extends Array<any>>(
|
||||
apiName: string
|
||||
): (...params: InputElements) => Promise<{
|
||||
data?: any
|
||||
error?: Error
|
||||
}> {
|
||||
@@ -131,7 +134,7 @@ export default class ServiceAccessors<
|
||||
data?: any
|
||||
error?: Error
|
||||
}
|
||||
return async (params: any): Promise<ElementGetResponse> => {
|
||||
return async (...params: InputElements): Promise<ElementGetResponse> => {
|
||||
try {
|
||||
const { data } = await axios.post(
|
||||
`${config.API_URL}/${this.elementName}${apiName}`,
|
||||
@@ -147,4 +150,36 @@ export default class ServiceAccessors<
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
securedCustomPost<InputElements extends Array<any>>(
|
||||
apiName: string
|
||||
): (
|
||||
jwt: string,
|
||||
...params: InputElements
|
||||
) => Promise<{
|
||||
data?: any
|
||||
error?: Error
|
||||
}> {
|
||||
interface ElementGetResponse {
|
||||
data?: any
|
||||
error?: Error
|
||||
}
|
||||
return async (jwt: string, ...params: InputElements): Promise<ElementGetResponse> => {
|
||||
try {
|
||||
const auth = { headers: { Authorization: `Bearer ${jwt}` } }
|
||||
const fullAxiosConfig = _.defaultsDeep(auth, axiosConfig)
|
||||
const { data } = await axios.post(
|
||||
`${config.API_URL}/${this.elementName}${apiName}`,
|
||||
params,
|
||||
fullAxiosConfig
|
||||
)
|
||||
if (data.error) {
|
||||
throw Error(data.error)
|
||||
}
|
||||
return { data }
|
||||
} catch (error) {
|
||||
return { error: error as Error }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,7 @@
|
||||
import { AxiosRequestConfig } from "axios"
|
||||
import Cookies from "js-cookie"
|
||||
|
||||
import { VolunteerLogin } from "./volunteers"
|
||||
|
||||
const storage: any = localStorage
|
||||
|
||||
@@ -6,17 +9,30 @@ export const axiosConfig: AxiosRequestConfig = {
|
||||
headers: {},
|
||||
}
|
||||
|
||||
const jwt: string | null = storage?.getItem("id_token")
|
||||
if (jwt) {
|
||||
setJWT(jwt)
|
||||
}
|
||||
|
||||
export function setJWT(token: string): void {
|
||||
export function setJWT(token: string, id: number): void {
|
||||
axiosConfig.headers.Authorization = `Bearer ${token}`
|
||||
storage?.setItem("id_token", token)
|
||||
storage?.setItem("jwt", token)
|
||||
storage?.setItem("id", id)
|
||||
Cookies.set("jwt", token)
|
||||
Cookies.set("id", `${id}`)
|
||||
}
|
||||
|
||||
export function unsetJWT(): void {
|
||||
delete axiosConfig.headers.Authorization
|
||||
storage?.removeItem("id_token")
|
||||
storage?.removeItem("jwt")
|
||||
storage?.removeItem("id")
|
||||
|
||||
Cookies.remove("jwt")
|
||||
Cookies.remove("id")
|
||||
}
|
||||
|
||||
export function getCookieJWT(cookie = ""): VolunteerLogin {
|
||||
const cookies = cookie
|
||||
.split(";")
|
||||
.reduce((res: { [cookieName: string]: string }, el: string) => {
|
||||
const [k, v] = el.split("=")
|
||||
res[k.trim()] = v
|
||||
return res
|
||||
}, {})
|
||||
return { jwt: cookies.jwt, id: +cookies.id }
|
||||
}
|
||||
|
@@ -19,7 +19,9 @@ export class Volunteer {
|
||||
|
||||
privileges = 0
|
||||
|
||||
active = 0
|
||||
active = ""
|
||||
|
||||
hiddenNotifs: number[] = []
|
||||
|
||||
created = new Date()
|
||||
|
||||
@@ -39,6 +41,7 @@ export const translationVolunteer: { [k in keyof Volunteer]: string } = {
|
||||
adult: "majeur",
|
||||
privileges: "privilege",
|
||||
active: "actif",
|
||||
hiddenNotifs: "notifsCachees",
|
||||
created: "creation",
|
||||
password1: "passe1",
|
||||
password2: "passe2",
|
||||
@@ -46,6 +49,23 @@ export const translationVolunteer: { [k in keyof Volunteer]: string } = {
|
||||
|
||||
const elementName = "Volunteer"
|
||||
|
||||
export const volunteerExample: Volunteer = {
|
||||
id: 1,
|
||||
firstname: "Aupeix",
|
||||
lastname: "Amélie",
|
||||
email: "pakouille.lakouille@yahoo.fr",
|
||||
mobile: "0675650392",
|
||||
photo: "images/volunteers/$taille/amélie_aupeix.jpg",
|
||||
food: "Végétarien",
|
||||
adult: 1,
|
||||
privileges: 0,
|
||||
active: "inconnu",
|
||||
hiddenNotifs: [],
|
||||
created: new Date(0),
|
||||
password1: "$2y$10$fSxY9AIuxSiEjwF.J3eXGubIxUPkdq9d5fqpbl8ASimSjNj4SR.9O",
|
||||
password2: "$2y$10$fSxY9AIuxSiEjwF.J3eXGubIxUPkdq9d5fqpbl8ASimSjNj4SR.9O",
|
||||
}
|
||||
|
||||
export const emailRegexp =
|
||||
/^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i
|
||||
export const passwordMinLength = 4
|
||||
@@ -60,12 +80,23 @@ export const volunteerAdd = serviceAccessors.add()
|
||||
export const volunteerSet = serviceAccessors.set()
|
||||
|
||||
export interface VolunteerLogin {
|
||||
firstname: string
|
||||
id: number
|
||||
jwt: string
|
||||
}
|
||||
export const volunteerLogin = serviceAccessors.customPost("Login")
|
||||
export const volunteerLogin =
|
||||
serviceAccessors.customPost<[{ email: string; password: string }]>("Login")
|
||||
|
||||
export interface VolunteerForgot {
|
||||
message: string
|
||||
}
|
||||
export const volunteerForgot = serviceAccessors.customPost("Forgot")
|
||||
export const volunteerForgot = serviceAccessors.customPost<[{ email: string }]>("Forgot")
|
||||
|
||||
export interface VolunteerNotifs {
|
||||
id: number
|
||||
firstname: string
|
||||
adult: number
|
||||
active: string
|
||||
hiddenNotifs: number[]
|
||||
}
|
||||
export const volunteerNotifsSet =
|
||||
serviceAccessors.securedCustomPost<[number, Partial<VolunteerNotifs>]>("NotifsSet")
|
||||
|
Reference in New Issue
Block a user