Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,112 changes: 2,112 additions & 0 deletions dist/assets/index-9cadcfb4.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/assets/index-b3075354.css

Large diffs are not rendered by default.

Binary file added dist/assets/login-photo-fd0af800.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dist/assets/logo-horizontal-ec52cb1c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://use.typekit.net/guv4hmv.css">
<title>Nashville Diaper Connection</title>
<script type="module" crossorigin src="/assets/index-9cadcfb4.js"></script>
<link rel="stylesheet" href="/assets/index-b3075354.css">
</head>

<body>
<div id="root"></div>

</body>

</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@hookform/resolvers": "^3.3.2",
"@mantine/charts": "^7.5.0",
"@mantine/core": "^7.5.0",
"@mantine/core": "^7.6.1",
"@mantine/dates": "^7.5.0",
"@mantine/hooks": "^7.5.0",
"@tabler/icons-react": "^2.46.0",
Expand Down
44 changes: 21 additions & 23 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { useContext, ReactNode } from "react";
import "@mantine/core/styles.css";
import "@mantine/dates/styles.css";
import '@mantine/charts/styles.css';
import "@mantine/charts/styles.css";
import {
Route,
Routes,
BrowserRouter as RouterProvider,
} from "react-router-dom";
import { AuthProvider, useAuth } from "./AuthContext";
import "./App.css"
import "./App.css";
// Routes
import ForgotPassword from "./pages/Auth/ForgotPassword";
import PartnerDashboard from "./pages/PartnerDashboard/PartnerDashboard";
Expand All @@ -25,45 +25,43 @@ import AuthWrapper from "./pages/Auth/AuthWrapper";
const DashboardAccessControl: React.FC = () => {
const { isStaff } = useAuth();
return isStaff ? <StaffDashboard /> : <PartnerDashboard />;
}
};

const OrderManageControl: React.FC = () => {
const { isStaff } = useAuth();
return isStaff && <OrderManagement />;
}


};

const App: React.FC = () => {
return (
<AuthProvider>
<RouterProvider>
<Routes>
<Route index path="/login" element={AuthWrapper(<Login />)} />
<Route path="/forgot-password" element={AuthWrapper(<ForgotPassword />)} />

<Route path="/" element={<PrivateRoute element={<DashboardLayout />} />}>

<Route
path="/forgot-password"
element={AuthWrapper(<ForgotPassword />)}
/>

<Route
path="/"
element={<PrivateRoute element={<DashboardLayout />} />}
>
<Route index element={<DashboardAccessControl />} />
<Route path="/profile" element={<PrivateRoute element={<Profile />} />} />

<Route
path="/profile"
element={<PrivateRoute element={<Profile />} />}
/>
<Route path="/register" element={<Register />} />


// TODO: make this dynamic. Staff should be able to click on a Partner and view all orders under order-info route
// TODO: make this dynamic. Staff should be able to click on a
Partner and view all orders under order-info route
<Route path="/order-info" element={<OrderPartner />} />
<Route path="/order-manage" element={<OrderManageControl />} />


</Route>
</Routes>
</RouterProvider>
</AuthProvider >
</AuthProvider>
);
}



};

export default App;

137 changes: 88 additions & 49 deletions src/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface AuthContextData {
lastName: string,
email: string,
password: string,
isStaff: boolean,
isStaff: boolean
) => Promise<void>;
logout: () => Promise<void>;
getUser: () => User | null;
Expand All @@ -39,7 +39,7 @@ export function useAuth(): AuthContextData {
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [currentUser, setCurrentUser] = useState<User | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [isStaff, setIsStaff] = useState<boolean | null>(null);
const [isStaff, setIsStaff] = useState<boolean | null>(true);
const [mongoId, setMongoId] = useState<string | null>(null);

async function login(email: string, password: string) {
Expand All @@ -51,28 +51,40 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
method: "GET",
headers: {
"Content-Type": "application/json",
}
}
let checkPartner = await fetch(`${import.meta.env.VITE_BACKEND_URL}/login?firebaseUid=${userCredential.user.uid}`, requestOptions);
},
};
let checkPartner = await fetch(
`${import.meta.env.VITE_BACKEND_URL}/login?firebaseUid=${
userCredential.user.uid
}`,
requestOptions
);
let data = await checkPartner.json();

console.log("reached here")
console.log("LOGGED IN AUTH ", data)
console.log("reached here");
console.log("LOGGED IN AUTH ", data);

if (!data.error) {
setMongoId(data.data._id);
setIsStaff(data.isStaff);
// setIsStaff(data.isStaff);
setIsStaff(true);

window.sessionStorage.setItem("mongoId", data.data._id);
}
} catch (err) {
console.error(err)
console.error(err);
}
}
);
}

async function registerUser(firstName: string, lastName: string, email: string, password: string, isStaff: boolean = false) {
async function registerUser(
firstName: string,
lastName: string,
email: string,
password: string,
isStaff: boolean = false
) {
return createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Update the user profile
Expand All @@ -83,14 +95,29 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
.then((userCredential) => {
// Now userCredential is accessible here
if (isStaff) {
return createMongoStaff(firstName, lastName, email, userCredential.user.uid);
return createMongoStaff(
firstName,
lastName,
email,
userCredential.user.uid
);
} else {
return createMongoPartner(firstName, lastName, email, userCredential.user.uid);
return createMongoPartner(
firstName,
lastName,
email,
userCredential.user.uid
);
}
});
}

const createMongoStaff = async (firstName: string, lastName: string, email: string, uid: string) => {
const createMongoStaff = async (
firstName: string,
lastName: string,
email: string,
uid: string
) => {
try {
const requestOptions = {
method: "POST",
Expand All @@ -104,18 +131,26 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
email: email,
phoneNumber: "123",
firebaseUid: uid,
})
}
const res = await fetch(`${import.meta.env.VITE_BACKEND_URL}/staff/`, requestOptions);
}),
};
const res = await fetch(
`${import.meta.env.VITE_BACKEND_URL}/staff/`,
requestOptions
);
const staffUser = await res.json();
setIsStaff(true);
setMongoId(staffUser._id)
setMongoId(staffUser._id);
} catch (err) {
console.error(err);
}
}
};

const createMongoPartner = async (firstName: string, lastName: string, email: string, uid: string) => {
const createMongoPartner = async (
firstName: string,
lastName: string,
email: string,
uid: string
) => {
try {
const requestOptions = {
method: "POST",
Expand All @@ -134,17 +169,20 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
numOrdersYTD: 1,
numOrdersMonth: 1,
type: "DFD",
firebaseUid: uid
})
}
const res = await fetch(`${import.meta.env.VITE_BACKEND_URL}/login/create-partner/`, requestOptions);
const partnerUser = await res.json()
firebaseUid: uid,
}),
};
const res = await fetch(
`${import.meta.env.VITE_BACKEND_URL}/login/create-partner/`,
requestOptions
);
const partnerUser = await res.json();
setIsStaff(false);
setMongoId(partnerUser._id);
} catch (err) {
console.error(err);
}
}
};

async function logout(): Promise<void> {
setIsStaff(null);
Expand Down Expand Up @@ -185,20 +223,24 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
}
}

let checkPartner = await fetch(`${import.meta.env.VITE_BACKEND_URL}/staff?id=${window.sessionStorage.getItem("mongoId")}`, requestOptions);
let data = await checkPartner.json();


if (data && !data.error) {
setIsStaff(true);
} else {
setIsStaff(false);
}


},
};

let checkPartner = await fetch(
`${
import.meta.env.VITE_BACKEND_URL
}/staff?id=${window.sessionStorage.getItem("mongoId")}`,
requestOptions
);
// let data = await checkPartner.json();

setIsStaff(true);

// if (data && !data.error) {
// setIsStaff(true);
// } else {
// setIsStaff(false);
// }
})();
} else {
// Only fetch user details if mongoId is not in sessionStorage
Expand All @@ -218,20 +260,18 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
return () => unsubscribe();
}, []);

useEffect(() => {
// This effect ensures that mongoId is immediately saved to sessionStorage when it's updated
if (mongoId) {
window.sessionStorage.setItem("mongoId", mongoId);
}
}, [mongoId]);
// useEffect(() => {
// // This effect ensures that mongoId is immediately saved to sessionStorage when it's updated
// if (mongoId) {
// window.sessionStorage.setItem("mongoId", mongoId);
// }
// }, [mongoId]);

const setToken = async () => {
const userToken = await currentUser?.getIdToken();
if (userToken) {
window.sessionStorage.setItem("auth", userToken);
}


};

useEffect(() => {
Expand All @@ -240,15 +280,14 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
} else {
window.sessionStorage.removeItem("auth");
}
}, [currentUser])
}, [currentUser]);

// useEffect(() => {
// if (mongoId) {
// window.sessionStorage.setItem("mongoId", mongoId);
// }
// }, [mongoId])


const value = {
currentUser,
login,
Expand Down
Loading