-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added profile services and profile slice to redux store
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters