Group flight list by date

This commit is contained in:
april 2024-01-05 11:13:21 -06:00
parent 7fa749a5de
commit 05fefd40b1
8 changed files with 379 additions and 294 deletions

View File

@ -1,13 +1,12 @@
import { LogItem, VerticalLogItem } from "@/ui/display/log-item"; import { VerticalLogItem } from "@/ui/display/log-item";
import ErrorDisplay from "@/ui/error-display"; import ErrorDisplay from "@/ui/error-display";
import { client } from "@/util/api"; import { client } from "@/util/api";
import { import {
Center, Center,
Container, Container,
Divider,
Grid, Grid,
Loader, Loader,
ScrollAreaAutosize, ScrollArea,
Stack, Stack,
Title, Title,
} from "@mantine/core"; } from "@mantine/core";
@ -26,8 +25,8 @@ export default function Flight() {
const log = flight.data; const log = flight.data;
return ( return (
<Container> // <Container>
<Stack> <Stack h="calc(100vh-95px)">
{flight.isError ? ( {flight.isError ? (
<Center h="calc(100vh - 95px)"> <Center h="calc(100vh - 95px)">
<ErrorDisplay error="Error Fetching Flight" /> <ErrorDisplay error="Error Fetching Flight" />
@ -41,7 +40,7 @@ export default function Flight() {
<Title order={3} py="lg" style={{ textAlign: "center" }}> <Title order={3} py="lg" style={{ textAlign: "center" }}>
Flight Log Flight Log
</Title> </Title>
<ScrollAreaAutosize mah="calc(100vh - 95px - 110px)" m="0" p="0"> <ScrollArea h="calc(100vh - 95px - 110px)" m="0" p="0">
<Container h="100%"> <Container h="100%">
<Grid justify="center"> <Grid justify="center">
<Grid.Col span={6}> <Grid.Col span={6}>
@ -250,7 +249,7 @@ export default function Flight() {
) : null} ) : null}
</Grid> </Grid>
</Container> </Container>
</ScrollAreaAutosize> </ScrollArea>
</> </>
) : ( ) : (
<Center h="calc(100vh - 95px)"> <Center h="calc(100vh - 95px)">
@ -258,6 +257,6 @@ export default function Flight() {
</Center> </Center>
)} )}
</Stack> </Stack>
</Container> // </Container>
); );
} }

View File

@ -86,7 +86,6 @@ export default function NewFlight() {
mutationFn: async (values: FlightFormSchema) => { mutationFn: async (values: FlightFormSchema) => {
const newFlight = flightCreateHelper(values); const newFlight = flightCreateHelper(values);
const res = await client.post("/flights", newFlight); const res = await client.post("/flights", newFlight);
console.log(res);
return res.data; return res.data;
}, },
retry: (failureCount, error: AxiosError) => { retry: (failureCount, error: AxiosError) => {

View File

@ -8,30 +8,24 @@ import {
Stack, Stack,
Loader, Loader,
Center, Center,
Divider,
Badge, Badge,
Group,
Divider,
} from "@mantine/core"; } from "@mantine/core";
import { randomId } from "@mantine/hooks";
import { Link, useLocation, useNavigate } from "@remix-run/react"; import { Link, useLocation, useNavigate } from "@remix-run/react";
import { IconPlus } from "@tabler/icons-react"; import {
IconArrowRightTail,
IconPlaneTilt,
IconPlus,
} from "@tabler/icons-react";
import { UseQueryResult, useQuery } from "@tanstack/react-query"; import { UseQueryResult, useQuery } from "@tanstack/react-query";
function useFlights() { function useFlights() {
const flights = useQuery({ const flights = useQuery({
queryKey: ["flights-list"], queryKey: ["flights-list"],
queryFn: async () => { queryFn: async () =>
const res = await client.get(`/flights`); await client.get(`/flights/by-date`).then((res) => res.data),
const groupedFlights: { [date: string]: FlightConciseSchema[] } = {};
res.data.map((log: FlightConciseSchema) => {
const dateStr = log.date;
if (!groupedFlights[dateStr]) {
groupedFlights[dateStr] = [];
}
groupedFlights[dateStr].push(log);
});
return groupedFlights;
},
}); });
return flights; return flights;
@ -41,42 +35,128 @@ function FlightsListDisplay({
flights, flights,
page, page,
}: { }: {
flights: UseQueryResult<{ [date: string]: FlightConciseSchema[] }>; flights: UseQueryResult<{
[year: string]: {
[month: string]: { [day: string]: FlightConciseSchema[] };
};
}>;
page: string; page: string;
}) { }) {
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
return ( return (
<> <>
{flights.data ? ( {flights.data ? (
Object.entries(flights.data).map(([date, logs], index: number) => ( Object.entries(flights.data).map(([year, months]) => (
<> <>
<Text key={date} mt="md" mb="xs" fw={700}>
{date}
</Text>
<Divider key={date + index} />
{logs.map((flight: FlightConciseSchema) => (
<NavLink <NavLink
key={flight.id} key={randomId()}
label={`-- ${year} --`}
fw={700}
style={{ textAlign: "center" }}
defaultOpened
childrenOffset={0}
>
<>
<Divider />
{Object.entries(months).map(([month, days]) => (
<NavLink
key={randomId()}
label={monthNames[Number(month) - 1]}
fw={500}
style={{ textAlign: "center" }}
defaultOpened
>
<Divider />
{Object.entries(days).map(([, logs]) => (
<>
{logs.map((flight: FlightConciseSchema) => (
<>
<NavLink
key={randomId()}
component={Link} component={Link}
to={`/logbook/flights/${flight.id}`} to={`/logbook/flights/${flight.id}`}
label={`${ label={
!(flight.waypoint_from || flight.waypoint_to) <Group>
? "No Route" <Badge
: "" color="gray"
}${flight.waypoint_from ? flight.waypoint_from : ""} ${ size="lg"
flight.waypoint_to ? flight.waypoint_to : "" radius="sm"
}`} px="xs"
description={`${flight.date}`} >
{flight.date}
</Badge>
<Text fw={500}>
{`${Number(flight.time_total).toFixed(
1
)} hr`}
</Text>
{flight.waypoint_from ||
flight.waypoint_to ? (
<Text>/</Text>
) : null}
<Group gap="xs">
{flight.waypoint_from ? (
<Text>{flight.waypoint_from}</Text>
) : (
""
)}
{flight.waypoint_from &&
flight.waypoint_to ? (
<IconArrowRightTail />
) : null}
{flight.waypoint_to ? (
<Text>{flight.waypoint_to}</Text>
) : (
""
)}
</Group>
</Group>
}
description={
<Text lineClamp={1}>
{flight.comments
? flight.comments
: "(No Comment)"}
</Text>
}
rightSection={ rightSection={
flight.aircraft ? ( flight.aircraft ? (
<Badge key={"aircraft" + index} color="gray"> <Badge
key={randomId()}
leftSection={<IconPlaneTilt size="1rem" />}
color="gray"
size="lg"
>
{flight.aircraft} {flight.aircraft}
</Badge> </Badge>
) : null ) : null
} }
active={page === flight.id} active={page === flight.id}
/> />
<Divider />
</>
))} ))}
</> </>
))}
</NavLink>
))}
</>
</NavLink>
</>
)) ))
) : flights.isLoading ? ( ) : flights.isLoading ? (
<Center h="calc(100vh - 95px - 50px)"> <Center h="calc(100vh - 95px - 50px)">

View File

@ -1,22 +1,22 @@
import { Divider, Grid, Container, ScrollAreaAutosize } from "@mantine/core"; import { Divider, Grid, Container, ScrollArea } from "@mantine/core";
import { Outlet } from "@remix-run/react"; import { Outlet } from "@remix-run/react";
import { FlightsList } from "./flights-list"; import { FlightsList } from "./flights-list";
export default function FlightsLayout() { export default function FlightsLayout() {
return ( return (
<> <>
<Grid h="100%" visibleFrom="md"> <Grid h="100%" visibleFrom="lg">
<Grid.Col span={3}> <Grid.Col span={4}>
<FlightsList /> <FlightsList />
</Grid.Col> </Grid.Col>
<Divider orientation="vertical" m="sm" /> <Divider orientation="vertical" m="sm" />
<Grid.Col span="auto"> <Grid.Col span="auto">
<ScrollAreaAutosize mah="calc(100vh - 95px)"> <ScrollArea.Autosize mah="calc(100vh - 95px)">
<Outlet /> <Outlet />
</ScrollAreaAutosize> </ScrollArea.Autosize>
</Grid.Col> </Grid.Col>
</Grid> </Grid>
<Container hiddenFrom="md"> <Container hiddenFrom="lg" style={{ paddingLeft: 0, paddingRight: 0 }}>
<Outlet /> <Outlet />
</Container> </Container>
</> </>

View File

@ -40,7 +40,7 @@ export function VerticalLogItem({
return ( return (
<Card> <Card>
<Stack gap="xs" align="center"> <Stack gap="xs" align="center" h="100%">
<Text c="dimmed" style={{ textalign: "center" }}> <Text c="dimmed" style={{ textalign: "center" }}>
{label} {label}
</Text> </Text>

View File

@ -19,7 +19,7 @@ export function TailfinAppShell({ children }: { children: React.ReactNode }) {
return ( return (
<AppShell <AppShell
header={{ height: 60 }} header={{ height: 60 }}
navbar={{ width: 300, breakpoint: "sm", collapsed: { mobile: !opened } }} navbar={{ width: 300, breakpoint: "xl", collapsed: { mobile: !opened } }}
padding="md" padding="md"
> >
<AppShell.Header> <AppShell.Header>
@ -28,7 +28,7 @@ export function TailfinAppShell({ children }: { children: React.ReactNode }) {
<Burger <Burger
opened={opened} opened={opened}
onClick={toggle} onClick={toggle}
hiddenFrom="sm" hiddenFrom="xl"
size="sm" size="sm"
/> />
</Group> </Group>
@ -44,7 +44,7 @@ export function TailfinAppShell({ children }: { children: React.ReactNode }) {
</Group> </Group>
</AppShell.Header> </AppShell.Header>
<AppShell.Navbar> <AppShell.Navbar>
<Navbar /> <Navbar opened={opened} toggle={toggle} />
</AppShell.Navbar> </AppShell.Navbar>
<AppShell.Main>{children}</AppShell.Main> <AppShell.Main>{children}</AppShell.Main>
</AppShell> </AppShell>

View File

@ -8,7 +8,13 @@ import {
IconUser, IconUser,
} from "@tabler/icons-react"; } from "@tabler/icons-react";
export default function Navbar() { export default function Navbar({
opened,
toggle,
}: {
opened: boolean;
toggle: () => void;
}) {
const location = useLocation(); const location = useLocation();
const page = location.pathname.split("/")[2]; const page = location.pathname.split("/")[2];
@ -24,6 +30,7 @@ export default function Navbar() {
label="Dashboard" label="Dashboard"
leftSection={<IconPlaneDeparture />} leftSection={<IconPlaneDeparture />}
active={page == null} active={page == null}
onClick={() => (opened ? toggle() : null)}
/> />
<NavLink <NavLink
p="md" p="md"
@ -32,6 +39,7 @@ export default function Navbar() {
label="Flights" label="Flights"
leftSection={<IconBook2 />} leftSection={<IconBook2 />}
active={page === "flights"} active={page === "flights"}
onClick={() => (opened ? toggle() : null)}
/> />
</Stack> </Stack>
<Stack gap="0"> <Stack gap="0">

View File

@ -27,7 +27,6 @@ client.interceptors.request.use(
error.config.headers.Authorization = `Bearer ${newAccessToken}`; error.config.headers.Authorization = `Bearer ${newAccessToken}`;
return client(error.config); return client(error.config);
} catch (err) { } catch (err) {
console.log("ERRORRRRRRRRRRRRRR");
clearUser(); clearUser();
window.location.href = "/login"; window.location.href = "/login";
} }