-
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.
api seprate page done + redux toolkit implemented
- Loading branch information
1 parent
c5cf072
commit 4ddd12a
Showing
8 changed files
with
189 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
export const constants = { | ||
// endpoint goes here | ||
endPoint: "https://localhost", | ||
}; |
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 |
---|---|---|
@@ -1,13 +1,61 @@ | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit"; | ||
import { loginAPI, signupAPI } from "../services/user"; | ||
|
||
export const loginItem = createAsyncThunk( | ||
"user/loginItem", | ||
async (payload, thunkAPI) => { | ||
let response = await loginAPI(payload); | ||
console.log("res=", response); | ||
if (response.isSuccessful === true) { | ||
// all localstorage work goes here | ||
return response.data; | ||
} else thunkAPI.rejectWithValue("network call failed"); | ||
} | ||
); | ||
export const signupItem = createAsyncThunk( | ||
"user/signupItem", | ||
async (payload, thunkAPI) => { | ||
let response = await signupAPI(payload); | ||
console.log("res=", response); | ||
if (response.isSuccessful === true) { | ||
return response.data; | ||
} else thunkAPI.rejectWithValue("network call failed"); | ||
} | ||
); | ||
|
||
export const userSlice = createSlice({ | ||
name: "user", | ||
initialState: { value: { name: "tarun", email: "", password: "" } }, | ||
initialState: { userInfo: [], loggedIn: false }, | ||
reducers: { | ||
login: (state, action) => { | ||
state.value = action.payload; | ||
logoutFunction: (state, action) => { | ||
state.userInfo = ""; | ||
let updateConnector = false; | ||
state.loggedIn = updateConnector; | ||
// localStorage.removeItem("userdata"); | ||
}, | ||
}, | ||
extraReducers: { | ||
[loginItem.fulfilled]: (state, action) => { | ||
if (action.payload.token) { | ||
state.userInfo = action.payload; | ||
let updateConnector = true; | ||
state.loggedIn = updateConnector; | ||
localStorage.setItem("loggedIn", state.loggedIn); | ||
} else { | ||
state.loggedIn = false; | ||
} | ||
}, | ||
[signupItem.fulfilled]: (state, action) => { | ||
if (action.payload.token) { | ||
state.userInfo = action.payload; | ||
let updateConnector = true; | ||
state.loggedIn = updateConnector; | ||
localStorage.setItem("loggedIn", state.loggedIn); | ||
} else { | ||
state.loggedIn = false; | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export const { logoutFunction } = userSlice.actions; | ||
export default userSlice.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import axios from "axios"; | ||
import { constants } from "../config"; | ||
|
||
export const api = constants.endPoint; | ||
|
||
export const error = (error) => { | ||
return { status: "error", isSuccessful: false, message: error }; | ||
}; | ||
|
||
export const success = (data) => { | ||
return { status: "success", isSuccessful: true, data: data }; | ||
}; | ||
|
||
export const getAuthHeaders = () => { | ||
return { Authorization: "Bearer " + localStorage.getItem("token") }; | ||
}; | ||
|
||
export const get = async (url, headers) => { | ||
try { | ||
let response = await axios.get(url, { headers }) | ||
return success(response.data) | ||
} | ||
catch (e) { | ||
console.log(e) | ||
return error(e) | ||
} | ||
} | ||
export const post = async (url, data, headers) => { | ||
try { | ||
console.log("this is url=",url); | ||
let response = await axios.post(url, data, { headers }) | ||
return success(response.data) | ||
} | ||
catch (e) { | ||
console.log(e) | ||
return error(e) | ||
} | ||
} | ||
|
||
export const update = async (url, data, headers) => { | ||
try { | ||
let response = await axios.put(url, data, { headers }) | ||
return success(response.data) | ||
} | ||
catch (e) { | ||
console.log(e) | ||
return error(e) | ||
} | ||
} | ||
|
||
export const del = async (url, headers) => { | ||
try { | ||
let response = await axios.delete(url, { headers }) | ||
return success(response.data) | ||
} | ||
catch (e) { | ||
console.log(e) | ||
return error(e) | ||
} | ||
} |
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,14 @@ | ||
import { api, getAuthHeaders, get, post, update, del } from "./common"; | ||
|
||
export const loginAPI = async (userlogin) => { | ||
//path e.g. | ||
let url = `${api}/user/login`; | ||
let authHeader = getAuthHeaders(); | ||
return await post(url, userlogin, { ...authHeader }); | ||
}; | ||
export const signupAPI = async (userlogin) => { | ||
//another path e.g. | ||
let url = `${api}/user/signup`; | ||
let authHeader = getAuthHeaders(); | ||
return await post(url, userlogin, { ...authHeader }); | ||
}; |