Skip to content

Commit

Permalink
added profile services and profile slice to redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
riad40 committed May 31, 2023
1 parent 0175433 commit 2e8816a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/services/profileServices.ts
Original file line number Diff line number Diff line change
@@ -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 }
72 changes: 72 additions & 0 deletions src/state/features/profile/profileSlice.ts
Original file line number Diff line number Diff line change
@@ -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<string | null>) => {
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<string | null>) => {
state.error = action.payload
state.loading = false
},
},
})

export { profileSlice }

export default profileSlice.reducer
2 changes: 2 additions & 0 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -11,6 +12,7 @@ const store = configureStore({
products: productSlice.reducer,
patients: patientSlice.reducer,
prescriptions: prescriptionSlice.reducer,
profile: profileSlice.reducer,
},
})

Expand Down

0 comments on commit 2e8816a

Please sign in to comment.