From b4ff2da7f548b061ab083a0c1a9fe1bcf64055e4 Mon Sep 17 00:00:00 2001 From: gibbyb Date: Tue, 23 Jul 2024 15:18:27 -0500 Subject: [PATCH] Add History Drawer & Clean up APIs. Will clean up more when I am home and have updated the iOS app. --- src/app/api/technicians/route.ts | 44 ++++++----- src/app/api/update_status_by_id/route.ts | 44 +++++++++++ src/app/api/update_technicians/route.ts | 3 +- src/app/api/v2/get_employees/route.ts | 17 ---- src/app/api/v2/update_status/route.ts | 38 --------- src/components/ui/History_Drawer.tsx | 98 +++++++++++++++++++----- src/components/ui/Tech_Table.tsx | 22 +++--- 7 files changed, 160 insertions(+), 106 deletions(-) create mode 100644 src/app/api/update_status_by_id/route.ts delete mode 100644 src/app/api/v2/get_employees/route.ts delete mode 100644 src/app/api/v2/update_status/route.ts diff --git a/src/app/api/technicians/route.ts b/src/app/api/technicians/route.ts index 53d4c15..315d096 100644 --- a/src/app/api/technicians/route.ts +++ b/src/app/api/technicians/route.ts @@ -1,6 +1,7 @@ "use server"; import { NextResponse } from 'next/server'; import { getEmployees } from '~/server/functions'; +import { auth } from '~/auth'; type Technician = { name: string; @@ -10,27 +11,30 @@ type Technician = { export const GET = async (request: Request) => { try { - const url = new URL(request.url); - const apiKey = url.searchParams.get('apikey'); - if (apiKey !== process.env.API_KEY) - return NextResponse.json( - { message: 'Unauthorized' }, - { status: 401 } - ); - const employees = await getEmployees(); - // Necessary because I haven't updated the iOS app - // yet to expect updatedAt rather than time - const formattedEmployees = employees.map((employee: Technician) => ({ - name: employee.name, - status: employee.status, - time: employee.updatedAt - })); - return NextResponse.json(formattedEmployees, { status: 200 }); + const session = await auth(); + if (!session) { + const url = new URL(request.url); + const apiKey = url.searchParams.get('apikey'); + if (apiKey !== process.env.API_KEY) + return NextResponse.json( + { message: 'Unauthorized' }, + { status: 401 } + ); + else { + const employees = await getEmployees(); + const formattedEmployees = employees.map((employee: Technician) => ({ + name: employee.name, + status: employee.status, + time: employee.updatedAt + })); + return NextResponse.json(formattedEmployees, { status: 200 }); + } + } else { + const employees = await getEmployees(); + return NextResponse.json(employees, { status: 200 }); + } } catch (error) { console.error('Error fetching employees:', error); - return NextResponse.json( - { message: 'Internal server error' }, - { status: 500 } - ); + return NextResponse.json({ message: 'Internal server error' }, { status: 500 }); } }; diff --git a/src/app/api/update_status_by_id/route.ts b/src/app/api/update_status_by_id/route.ts new file mode 100644 index 0000000..62e94e5 --- /dev/null +++ b/src/app/api/update_status_by_id/route.ts @@ -0,0 +1,44 @@ +// Update Employee Status by IDs +"use server"; +import { NextResponse } from 'next/server'; +import type { NextRequest } from 'next/server'; +import { updateEmployeeStatus } from '~/server/functions'; +import { auth } from '~/auth'; + +type UpdateStatusBody = { + employeeIds: string[]; + newStatus: string; +}; + +export const POST = async (req: NextRequest) => { + const session = await auth(); + if (!session) { + const url = new URL(req.url); + const apiKey = url.searchParams.get('apikey'); + if (apiKey !== process.env.API_KEY) + return NextResponse.json( + { message: 'Unauthorized' }, + { status: 401 } + ); + } else { + const { employeeIds, newStatus } = await req.json() as UpdateStatusBody; + if (!Array.isArray(employeeIds) || typeof newStatus !== 'string') + return NextResponse.json( + { message: 'Invalid input' }, + { status: 400 } + ); + try { + await updateEmployeeStatus(employeeIds, newStatus); + return NextResponse.json( + { message: 'Status updated successfully' }, + { status: 200 } + ); + } catch (error) { + console.error('Error updating status:', error); + return NextResponse.json( + { message: 'Internal server error' }, + { status: 500 } + ); + } + } +}; diff --git a/src/app/api/update_technicians/route.ts b/src/app/api/update_technicians/route.ts index 64a9c73..8b7ef2c 100644 --- a/src/app/api/update_technicians/route.ts +++ b/src/app/api/update_technicians/route.ts @@ -1,8 +1,9 @@ +// Update Employee Status by Names "use server"; import { NextResponse } from 'next/server'; import { updateEmployeeStatusByName } from '~/server/functions'; -interface Technician { +type Technician = { name: string; status: string; } diff --git a/src/app/api/v2/get_employees/route.ts b/src/app/api/v2/get_employees/route.ts deleted file mode 100644 index d7e06ff..0000000 --- a/src/app/api/v2/get_employees/route.ts +++ /dev/null @@ -1,17 +0,0 @@ -"use server"; -import { NextResponse } from 'next/server'; -import { getEmployees } from '~/server/functions'; -import { auth } from '~/auth'; - -export const GET = async () => { - try { - const session = await auth(); - if (!session) - return NextResponse.json({ message: 'Unauthorized' }, { status: 401 }); - const employees = await getEmployees(); - return NextResponse.json(employees, { status: 200 }); - } catch (error) { - console.error('Error fetching employees:', error); - return NextResponse.json({ message: 'Internal server error' }, { status: 500 }); - } -}; diff --git a/src/app/api/v2/update_status/route.ts b/src/app/api/v2/update_status/route.ts deleted file mode 100644 index 3ad344d..0000000 --- a/src/app/api/v2/update_status/route.ts +++ /dev/null @@ -1,38 +0,0 @@ -"use server"; -import { NextResponse } from 'next/server'; -import type { NextRequest } from 'next/server'; -import { updateEmployeeStatus } from '~/server/functions'; -import { auth } from '~/auth'; - -type UpdateStatusBody = { - employeeIds: string[]; - newStatus: string; -}; - -export const POST = async (req: NextRequest) => { - const session = await auth(); - if (!session) - return NextResponse.json( - { message: 'Unauthorized' }, - { status: 401 } - ); - const { employeeIds, newStatus } = await req.json() as UpdateStatusBody; - if (!Array.isArray(employeeIds) || typeof newStatus !== 'string') - return NextResponse.json( - { message: 'Invalid input' }, - { status: 400 } - ); - try { - await updateEmployeeStatus(employeeIds, newStatus); - return NextResponse.json( - { message: 'Status updated successfully' }, - { status: 200 } - ); - } catch (error) { - console.error('Error updating status:', error); - return NextResponse.json( - { message: 'Internal server error' }, - { status: 500 } - ); - } -}; diff --git a/src/components/ui/History_Drawer.tsx b/src/components/ui/History_Drawer.tsx index d0fb81f..fea7542 100644 --- a/src/components/ui/History_Drawer.tsx +++ b/src/components/ui/History_Drawer.tsx @@ -1,27 +1,87 @@ -import { Button } from "~/components/ui/shadcn/button"; import { - Drawer, DrawerClose, DrawerContent, - DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, - DrawerTrigger, } from "~/components/ui/shadcn/drawer"; +import { + Table, + TableBody, + TableCell, + TableHead, + TableHeader, + TableRow, +} from "~/components/ui/shadcn/table"; +import { + Pagination, + PaginationContent, + PaginationEllipsis, + PaginationItem, + PaginationLink, + PaginationNext, + PaginationPrevious, +} from "~/components/ui/shadcn/pagination"; +//import { Button } from "~/components/ui/shadcn/button"; +import Image from "next/image"; - - Open - - - Are you absolutely sure? - This action cannot be undone. - - - - - - - - - +export default function History_Drawer() { + //const + return ( + + + +
+ Tech Tracker Logo +

+ History +

+
+
+
+ + + + Name + Status + Updated At + + + + + INV001 + Paid + Credit Card + + +
+ + + + + + + + 1 + + + + + + + + + + + + +
+ ); +}; diff --git a/src/components/ui/Tech_Table.tsx b/src/components/ui/Tech_Table.tsx index 1164591..9e82eda 100644 --- a/src/components/ui/Tech_Table.tsx +++ b/src/components/ui/Tech_Table.tsx @@ -3,9 +3,10 @@ import { useState, useEffect, useCallback } from 'react'; import { useSession } from "next-auth/react"; import Loading from "~/components/ui/Loading"; import { useTVMode } from "~/components/context/TVModeContext"; +import { Drawer, DrawerTrigger } from "~/components/ui/shadcn/drawer"; +import History_Drawer from "~/components/ui/History_Drawer"; -// Define the Employee interface to match data fetched on the server -interface Employee { +type Employee = { id: number; name: string; status: string; @@ -22,7 +23,7 @@ export default function Tech_Table({ employees }: { employees: Employee[] }) { const [employeeData, setEmployeeData] = useState(employees); const fetch_employees = useCallback(async (): Promise => { - const res = await fetch('/api/v2/get_employees', { + const res = await fetch('/api/technicians', { method: 'GET', headers: { 'Authorization': `Bearer ${process.env.API_KEY}` @@ -35,12 +36,10 @@ export default function Tech_Table({ employees }: { employees: Employee[] }) { if (!session) { alert("You must be signed in to update status."); return; - } - - if (selectedIds.length === 0 && employeeStatus.trim() !== '') { + } else if (selectedIds.length === 0 && employeeStatus.trim() !== '') { const cur_user = employees.find(employee => employee.name === session.user?.name); if (cur_user) { - await fetch('/api/v2/update_status', { + await fetch('/api/update_status_by_id', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -50,7 +49,7 @@ export default function Tech_Table({ employees }: { employees: Employee[] }) { }); } } else if (employeeStatus.trim() !== '') { - await fetch('/api/v2/update_status', { + await fetch('/api/update_status_by_id', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -165,9 +164,10 @@ export default function Tech_Table({ employees }: { employees: Employee[] }) { )} Name - + + Status + + Updated At