|
1 | | -import LoginForm from '@/components/LoginForm'; |
| 1 | +'use client'; |
2 | 2 | import TrainingLogCard, { mockData } from '@/components/TrainingLogCard'; |
3 | | -import Link from 'next/link'; |
4 | | -import Image from 'next/image'; |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import { TrainingLogDocument } from '../../../../../server/mongodb/models/TrainingLog'; |
| 5 | +import Skeleton from 'react-loading-skeleton'; |
5 | 6 |
|
6 | 7 | export default function AdminTrainingLogsPage() { |
| 8 | + const [isLoading, setIsLoading] = useState(true); |
| 9 | + const [logs, setLogs] = useState<TrainingLogDocument[]>([]); |
| 10 | + |
| 11 | + useEffect(() => { |
| 12 | + const fetchTrainingLogs = async () => { |
| 13 | + try { |
| 14 | + const response = await fetch('/api/training-log?all=true', { |
| 15 | + method: 'GET', |
| 16 | + credentials: 'include', |
| 17 | + }); |
| 18 | + const respBody = await response.json(); |
| 19 | + const logs: TrainingLogDocument[] = respBody?.data; |
| 20 | + |
| 21 | + if (logs != undefined) { |
| 22 | + setLogs(logs); |
| 23 | + } |
| 24 | + setIsLoading(false); |
| 25 | + } catch (error) { |
| 26 | + setIsLoading(false); |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + fetchTrainingLogs(); |
| 31 | + }, [isLoading]); |
| 32 | + |
7 | 33 | return ( |
8 | | - <main className="min-h-full w-full bg-white dark:bg-black flex flex-col"> |
9 | | - <div className='p-8 flex flex-row justify-between items-center'> |
| 34 | + <main className="min-h-full w-full bg-white dark:bg-black flex flex-col"> |
| 35 | + <div className="p-8 flex flex-row justify-between items-center"> |
10 | 36 | <h1 className="text-left text-2xl font-bold text-neutral-600"> |
11 | | - All training logs |
| 37 | + All training Logs |
12 | 38 | </h1> |
13 | | - </div> |
14 | | - |
15 | | - <hr /> |
16 | | - <div className="mt-8 mx-4 overflow-y-scroll flex-1 flex flex-col gap-4 justify-start items-center"> |
17 | | - <TrainingLogCard data={mockData} /> |
18 | | - <TrainingLogCard data={mockData} /> |
19 | | - <TrainingLogCard data={mockData} /> |
20 | | - </div> |
21 | | - </main> |
| 39 | + </div> |
| 40 | + |
| 41 | + <hr /> |
| 42 | + <div className="mt-8 mx-4 overflow-y-scroll flex-1 flex flex-col gap-4 justify-start items-center"> |
| 43 | + {isLoading ? ( |
| 44 | + <Skeleton count={10} /> |
| 45 | + ) : ( |
| 46 | + logs.map((l, i) => ( |
| 47 | + <TrainingLogCard |
| 48 | + key={i} |
| 49 | + data={{ |
| 50 | + ...l, |
| 51 | + _id: l._id.toString(), |
| 52 | + user: l.user as any, |
| 53 | + animal: l.animal as any, |
| 54 | + date: l.date as any, |
| 55 | + }} |
| 56 | + /> |
| 57 | + )) |
| 58 | + )} |
| 59 | + {!isLoading && logs.length == 0 && <p>No training logs yet!</p>} |
| 60 | + </div> |
| 61 | + </main> |
22 | 62 | ); |
23 | 63 | } |
0 commit comments