Skip to content

Commit

Permalink
Merge pull request #169 from La-Fresca/discount
Browse files Browse the repository at this point in the history
Branch Manager List for Top-level Manager
  • Loading branch information
suruthiii authored Dec 3, 2024
2 parents ce7585c + 873f51a commit ec51ce9
Show file tree
Hide file tree
Showing 16 changed files with 1,028 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ import Inventory_TLM from '@/pages/TopLevelManager/Inventory';
import Discount_TLM from '@/pages/TopLevelManager/Discount';
import FoodList_TLM from './pages/TopLevelManager/Foods/index';
import ComboList_TLM from './pages/TopLevelManager/FoodCombos/index';
import BranchManagerList from './pages/TopLevelManager/BrancheManager/index';
import AddBranchManager from './pages/TopLevelManager/BrancheManager/AddBranchManager';
import EditBranchManager from './pages/TopLevelManager/BrancheManager/EditBranchManager';

import ViewInventory_TLM from './pages/TopLevelManager/Inventory/view';
import ViewInventoryByName_TLM from './pages/TopLevelManager/Inventory/viewByName';
Expand Down Expand Up @@ -920,7 +923,7 @@ const routes = createRoutesFromElements(
element={
<>
<PageTitle title="Top Level Manager | Users" />
<User />
<BranchManagerList />
</>
}
/>
Expand All @@ -929,7 +932,7 @@ const routes = createRoutesFromElements(
element={
<>
<PageTitle title="Top Level Manager | Add User" />
<UserAdd />
<AddBranchManager />
</>
}
/>
Expand All @@ -938,7 +941,7 @@ const routes = createRoutesFromElements(
element={
<>
<PageTitle title="Top Level Manager | Edit User" />
<UserEdit />
<EditBranchManager />
</>
}
/>
Expand Down
21 changes: 21 additions & 0 deletions src/api/useUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ export const useUsers = () => {
}
};

const getAllBranchManagers = async () => {
try {
const response = await fetch(`${API_URL}/user/availableBranchManagers`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${getToken()}`,
},
});
if (!response.ok) {
throw new Error('Failed to fetch item');
} else {
return response.json();
}
} catch (error: any) {
console.error(error);
throw new Error(error);
}
};

const addUser = async (data: User) => {
try {
const response = await fetch(`${API_URL}/user`, {
Expand Down Expand Up @@ -157,5 +177,6 @@ export const useUsers = () => {
getUserById,
getWaiterById,
getDeliveryPersonById,
getAllBranchManagers,
};
};
14 changes: 13 additions & 1 deletion src/components/TopLevelManager/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import StockIconWhite from '@images/icon/stock-white.png';
import ReportIcon from '@images/icon/report.png';
import ReportIconWhite from '@images/icon/report-white.png';
import { BuildingOffice2Icon } from '@heroicons/react/24/outline';
import { Bars3Icon } from '@heroicons/react/24/outline';
import { Bars3Icon, UserIcon } from '@heroicons/react/24/outline';
import { Squares2X2Icon } from '@heroicons/react/24/outline';

interface SidebarProps {
Expand Down Expand Up @@ -223,6 +223,18 @@ const Sidebar = ({ sidebarOpen, setSidebarOpen }: SidebarProps) => {
Reports
</NavLink>
</li>
<li>
<NavLink
to="/top-level-manager/users"
className={`group relative flex items-center gap-2.5 rounded-xl py-2 px-4 font-medium text-black dark:text-white duration-300 ease-in-out hover:bg-yellow-100 dark:hover:bg-meta-4 ${
pathname.includes('/top-level-manager/users') &&
'bg-yellow-100 dark:bg-meta-4'
}`}
>
<UserIcon className="w-6 h-6" />
Branch Managers
</NavLink>
</li>
</ul>
</div>
</nav>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import React from 'react';
import { useNavigate } from 'react-router-dom';
import { z } from 'zod';
import { useForm, SubmitHandler } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { useUsers } from '@/api/useUser';
import { User } from '@/types/user';
import { swalSuccess } from '@/components/UI/SwalSuccess';

const FormSchema = z.object({
firstName: z.string().min(1, { message: 'First Name is required' }),
lastName: z.string().min(1, { message: 'Last Name is required' }),
username: z.string().min(1, { message: 'Username is required' }),
email: z.string().min(1, { message: 'Email is required' }),
phoneNumber: z.string().min(1, { message: 'Contact is required' }),
role: z.string().min(1, { message: 'Group is required' }),
address: z.string().min(1, { message: 'Address is required' }),
password: z.string().min(1, { message: 'Password is required' }),
confirmPassword: z
.string()
.min(1, { message: 'Confirm Password is required' }),
});

type FormSchemaType = z.infer<typeof FormSchema>;

const AddUser: React.FC = () => {
const navigate = useNavigate();
const { addUser } = useUsers();
const { showSwal } = swalSuccess({
message: 'User Added successfully',
});

const { register, handleSubmit, formState } = useForm<FormSchemaType>({
resolver: zodResolver(FormSchema),
});

const onSubmit: SubmitHandler<FormSchemaType> = async (data) => {
const transformedData: User = {
...data,
};
try {
addUser(transformedData);
} catch (error) {
console.error('Error adding user', error);
} finally {
setTimeout(() => {
showSwal();
navigate('/top-level-manager/users');
}, 2000);
}
};

const { errors } = formState;

return (
<div className="min-h-screen bg-transparent text-white p-8">
<h2 className="text-4xl mb-4 font-bold">Add User</h2>
<form onSubmit={handleSubmit(onSubmit)}>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div className="mb-2">
<label className="block mb-2">First Name</label>
<input
type="text"
placeholder="First Name"
className="w-full p-2 border border-gray-500 rounded bg-black text-white focus:outline-none focus:ring-2 focus:ring-orange-600 transition duration-300"
{...register('firstName')}
/>
{errors.firstName && (
<p className="text-red-500 text-xs mt-1">
{errors.firstName.message}
</p>
)}
</div>
<div className="mb-2">
<label className="block mb-2">Last Name</label>
<input
type="text"
placeholder="Last Name"
className="w-full p-2 border border-gray-500 rounded bg-black text-white focus:outline-none focus:ring-2 focus:ring-orange-600 transition duration-300"
{...register('lastName')}
/>
{errors.lastName && (
<p className="text-red-500 text-xs mt-1">
{errors.lastName.message}
</p>
)}
</div>
<div className="mb-2">
<label className="block mb-2">Username</label>
<input
type="text"
placeholder="Username"
className="w-full p-2 border border-gray-500 rounded bg-black text-white focus:outline-none focus:ring-2 focus:ring-orange-600 transition duration-300"
{...register('username')}
/>
{errors.username && (
<p className="text-red-500 text-xs mt-1">
{errors.username.message}
</p>
)}
</div>
<div className="mb-2">
<label className="block mb-2">Email</label>
<input
type="email"
placeholder="[email protected]"
className="w-full p-2 border border-gray-500 rounded bg-black text-white focus:outline-none focus:ring-2 focus:ring-orange-600 transition duration-300"
{...register('email')}
/>
{errors.email && (
<p className="text-red-500 text-xs mt-1">
{errors.email.message}
</p>
)}
</div>
<div className="mb-2">
<label className="block mb-2">Address</label>
<input
type="text"
placeholder="Address"
className="w-full p-2 border border-gray-500 rounded bg-black text-white focus:outline-none focus:ring-2 focus:ring-orange-600 transition duration-300"
{...register('address')}
/>
{errors.address && (
<p className="text-red-500 text-xs mt-1">
{errors.address.message}
</p>
)}
</div>
<div className="mb-2">
<label className="block mb-2">Phone</label>
<input
type="text"
placeholder="Eg: 1234567890"
className="w-full p-2 border border-gray-500 rounded bg-black text-white focus:outline-none focus:ring-2 focus:ring-orange-600 transition duration-300"
{...register('phoneNumber')}
/>
{errors.phoneNumber && (
<p className="text-red-500 text-xs mt-1">
{errors.phoneNumber.message}
</p>
)}
</div>
<div className="mb-2 hidden">
<input
type="text"
value="BRANCH_MANAGER"
hidden
className="w-full p-2 border border-gray-500 rounded bg-black text-white focus:outline-none focus:ring-2 focus:ring-orange-600 transition duration-300"
{...register('role')}
/>
</div>
<div className="mb-2">
<label className="block mb-2">Password</label>
<input
type="password"
placeholder="Type strong password"
className="w-full p-2 border border-gray-500 rounded bg-black text-white focus:outline-none focus:ring-2 focus:ring-orange-600 transition duration-300"
{...register('password')}
/>
{errors.password && (
<p className="text-red-500 text-xs mt-1">
{errors.password.message}
</p>
)}
</div>
<div className="mb-2">
<label className="block mb-2">Confirm Password</label>
<input
type="password"
placeholder="Confirm Password"
className="w-full p-2 border border-gray-500 rounded bg-black text-white focus:outline-none focus:ring-2 focus:ring-orange-600 transition duration-300"
{...register('confirmPassword')}
/>
{errors.confirmPassword && (
<p className="text-red-500 text-xs mt-1">
{errors.confirmPassword.message}
</p>
)}
</div>
</div>
<div className="flex items-center justify-end mt-2 gap-4">
<button
type="button"
onClick={() => navigate('/branch-manager/users')}
className="bg-transparent hover:bg-transparent text-white px-8 py-4 border-2 rounded-lg transition duration-200"
>
Cancel
</button>
<button
type="submit"
className="text-white bg-gradient-to-r from-orange-600 to-orange-400 hover:from-orange-400 hover:to-orange-600 px-12 py-4 rounded-lg transition duration-300 shadow-md"
>
Save
</button>
</div>
</form>
</div>
);
};

export default AddUser;
Loading

0 comments on commit ec51ce9

Please sign in to comment.