From 2e8816a23b04dafbf432a2cbefa289d6b0ce492f Mon Sep 17 00:00:00 2001 From: riad40 Date: Wed, 31 May 2023 12:51:03 +0100 Subject: [PATCH] added profile services and profile slice to redux store --- src/services/profileServices.ts | 48 +++++++++++++++ src/state/features/profile/profileSlice.ts | 72 ++++++++++++++++++++++ src/state/store.ts | 2 + 3 files changed, 122 insertions(+) create mode 100644 src/services/profileServices.ts create mode 100644 src/state/features/profile/profileSlice.ts diff --git a/src/services/profileServices.ts b/src/services/profileServices.ts new file mode 100644 index 0000000..0f147d5 --- /dev/null +++ b/src/services/profileServices.ts @@ -0,0 +1,48 @@ +import api from "../configs/api" +import { createAsyncThunk } from "@reduxjs/toolkit" + +const USER_ID = "6473487fa17e6c0ae79b0a5a" + +const CLINIC_ID = "6473487fa17e6c0ae79b0a5c" + +// get user infos +const getUserInfos = createAsyncThunk("users/id", async () => { + try { + const response = await api.get("/users/" + USER_ID) + return response.data + } catch (error) { + console.log(error) + } +}) + +// update user infos +const updateUserInfos = createAsyncThunk("users/id", async (data: any) => { + try { + const response = await api.put("/users/" + USER_ID, data) + return response.data + } catch (error) { + console.log(error) + } +}) + +// get clinic infos +const getClinicInfos = createAsyncThunk("clinics/id", async () => { + try { + const response = await api.get("/clinics/" + CLINIC_ID) + return response.data + } catch (error) { + console.log(error) + } +}) + +// update clinic infos +const updateClinicInfos = createAsyncThunk("clinics/id", async (data: any) => { + try { + const response = await api.put("/clinics/" + CLINIC_ID, data) + return response.data + } catch (error) { + console.log(error) + } +}) + +export { getUserInfos, updateUserInfos, getClinicInfos, updateClinicInfos } diff --git a/src/state/features/profile/profileSlice.ts b/src/state/features/profile/profileSlice.ts new file mode 100644 index 0000000..6b5c4fd --- /dev/null +++ b/src/state/features/profile/profileSlice.ts @@ -0,0 +1,72 @@ +import { createSlice, PayloadAction } from "@reduxjs/toolkit" +import { getUserInfos, getClinicInfos } from "../../../services/profileServices" +import { User, Clinic } from "../../../@types" + +interface ProfileState { + user: User + clinic: Clinic + loading: boolean + error: string | null +} + +const initialState: ProfileState = { + user: { + _id: "", + fullName: "", + email: "", + password: "", + avatar: "", + speciality: "", + dateOfBirth: "", + inpe: "", + phone: "", + }, + clinic: { + _id: "", + name: "", + address: "", + phone: "", + email: "", + fax: "", + owner: "", + city: "", + }, + loading: false, + error: null, +} + +const profileSlice = createSlice({ + name: "profile", + initialState, + reducers: {}, + extraReducers: { + // getUserInfos + [getUserInfos.pending.type]: state => { + state.loading = true + }, + [getUserInfos.fulfilled.type]: (state, action) => { + state.user = action.payload + state.loading = false + }, + [getUserInfos.rejected.type]: (state, action: PayloadAction) => { + state.error = action.payload + state.loading = false + }, + // getClinicInfos + [getClinicInfos.pending.type]: state => { + state.loading = true + }, + [getClinicInfos.fulfilled.type]: (state, action) => { + state.clinic = action.payload + state.loading = false + }, + [getClinicInfos.rejected.type]: (state, action: PayloadAction) => { + state.error = action.payload + state.loading = false + }, + }, +}) + +export { profileSlice } + +export default profileSlice.reducer diff --git a/src/state/store.ts b/src/state/store.ts index ce7dd06..1920ab6 100644 --- a/src/state/store.ts +++ b/src/state/store.ts @@ -3,6 +3,7 @@ import { dashboardSlice } from "./features/dashboard/dashboardSlice" import { productSlice } from "./features/products/productSlice" import { patientSlice } from "./features/patients/patientsSlice" import { prescriptionSlice } from "./features/prescriptions/prescriptionSlice" +import { profileSlice } from "./features/profile/profileSlice" // store const store = configureStore({ @@ -11,6 +12,7 @@ const store = configureStore({ products: productSlice.reducer, patients: patientSlice.reducer, prescriptions: prescriptionSlice.reducer, + profile: profileSlice.reducer, }, })