Skip to content

Commit

Permalink
created basic file strucuture
Browse files Browse the repository at this point in the history
installed axios
added authController
added authService
added axiosInstance in utils
  • Loading branch information
Ravik-TH committed Feb 13, 2025
1 parent fe68087 commit 02722a9
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 1 deletion.
56 changes: 56 additions & 0 deletions frontend/web-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/web-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@types/react-dom": "^18.3.5",
"ajv": "^8.17.1",
"ajv-keywords": "^5.1.0",
"axios": "^1.7.9",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-router-dom": "^7.1.5",
Expand Down
1 change: 1 addition & 0 deletions frontend/web-app/src/Constants/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const Dummy = 5000 //Dummy constant
37 changes: 37 additions & 0 deletions frontend/web-app/src/Context/AuthContext.tsx
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;
19 changes: 19 additions & 0 deletions frontend/web-app/src/Controller/AuthController.ts
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;
Binary file added frontend/web-app/src/Images/dummy-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions frontend/web-app/src/Interfaces/AuthInterfaces.ts
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,
}
}
17 changes: 16 additions & 1 deletion frontend/web-app/src/Pages/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import React from 'react'
import React, { useEffect } from 'react'
import authController from '../Controller/AuthController'

function HomePage() {
// example of using APIs
// you need not necessarily call APIs in useEffect
// you may also call them through other ways
// but make sure to keep them in async functions
// and use await while calling them to resolve Promise.

useEffect(() => {
const fetchUserData = async () => {
const data = await authController.getUsers();
console.log(data);
}
fetchUserData();
}, [])

return (
<div>HomePage</div>
)
Expand Down
18 changes: 18 additions & 0 deletions frontend/web-app/src/Services/AuthServices.ts
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;
1 change: 1 addition & 0 deletions frontend/web-app/src/Styles/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Global CSS */
13 changes: 13 additions & 0 deletions frontend/web-app/src/Utils/axiosInstance.ts
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;
5 changes: 5 additions & 0 deletions frontend/web-app/src/Utils/global.ts
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;
}

0 comments on commit 02722a9

Please sign in to comment.