-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
installed axios added authController added authService added axiosInstance in utils
- Loading branch information
Showing
12 changed files
with
182 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const Dummy = 5000 //Dummy constant |
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,37 @@ | ||
import { createContext, useReducer } from "react"; | ||
import { AuthContextAction, AuthContextState } from "../Interfaces/AuthInterfaces"; | ||
|
||
// Dummy AuthContext | ||
|
||
const AuthContext = createContext({}); | ||
|
||
const initialState : AuthContextState={ | ||
authUser: '', | ||
authToken: '', | ||
isLoading: false, | ||
} | ||
|
||
const reducer = (state: AuthContextState, action: AuthContextAction): AuthContextState =>{ | ||
switch(action.type){ | ||
case 'LOGIN': { | ||
return {...state, authUser: action.data.authUser , authToken: action.data.authToken} | ||
} | ||
default: { | ||
return state; | ||
} | ||
} | ||
} | ||
|
||
export const AuthProvider = ({children}: {children: any}) => { | ||
const [{authUser, authToken, isLoading}, dispatch] = useReducer(reducer, initialState); | ||
const values = { | ||
authUser, | ||
authToken, | ||
isLoading, | ||
dispatch, | ||
} | ||
return <AuthContext.Provider value={values}>{children}</AuthContext.Provider> | ||
|
||
} | ||
|
||
export default AuthContext; |
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,19 @@ | ||
import authService from "../Services/AuthServices"; | ||
import axiosInstance from "../Utils/axiosInstance"; | ||
|
||
// Dummy AuthController | ||
class AuthController{ | ||
public getUsers= async (): Promise<any> =>{ | ||
try{ | ||
const response = await axiosInstance.get('/users'); | ||
const usersData = authService.getUsers(response.data); | ||
return usersData; | ||
}catch(error){ | ||
throw new Error('Error getUsers'); | ||
} | ||
} | ||
} | ||
|
||
const authController = new AuthController(); | ||
|
||
export default authController; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,15 @@ | ||
// Dummy AuthInterfaces | ||
|
||
export interface AuthContextState{ | ||
authUser: string, | ||
authToken: string, | ||
isLoading: boolean, | ||
} | ||
|
||
export interface AuthContextAction{ | ||
type: string, | ||
data: { | ||
authUser: string, | ||
authToken: string, | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// This is an example of how to create service | ||
// please remember that I have used any as an interface for some variable | ||
// but that is not good please make specific interface for specific variable used in controller and service. | ||
|
||
|
||
class AuthService{ | ||
public getUsers=(data:any)=>{ | ||
return data.map((userData: any)=>{ | ||
return { | ||
name: userData.username | ||
} | ||
}) | ||
} | ||
} | ||
|
||
const authService = new AuthService(); | ||
|
||
export default authService; |
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 @@ | ||
/* Global CSS */ |
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,13 @@ | ||
import axios from "axios"; | ||
|
||
// Dummy URL is used here we need to change it later. | ||
|
||
const axiosInstance = axios.create({ | ||
baseURL: 'https://jsonplaceholder.typicode.com', | ||
timeout: 5000, | ||
headers: { | ||
"Content-Type": 'application/json', | ||
} | ||
}) | ||
|
||
export default axiosInstance; |
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,5 @@ | ||
// Dummy Utils function | ||
export const isNull=(item: any)=>{ | ||
if(item) return true; | ||
else return false; | ||
} |