-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
658 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,5 +77,5 @@ type Season struct { | |
|
||
type StreakDay struct { | ||
BaseModel | ||
Date time.Time | ||
Date time.Time `json:"date"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Grid } from "@mui/material"; | ||
import { useSnackbar } from "notistack"; | ||
import { createContext, Dispatch, SetStateAction, useState } from "react"; | ||
import { LoadingSkeleton } from "../../../components/LoadingSkeleton"; | ||
import { useFetch } from "../../../hooks/useFetch"; | ||
import { convertDayJSON, Day } from "../../../types/days"; | ||
import { getApi } from "../../../util/fetch"; | ||
import { DaysAdd } from "./DaysAdd"; | ||
import { DaysTable } from "./DaysTable"; | ||
|
||
interface DayContextProps { | ||
days: readonly Day[]; | ||
setDays: Dispatch<SetStateAction<readonly Day[]>>; | ||
reloadDays: () => void; | ||
} | ||
|
||
export const DayContext = createContext<DayContextProps>({ | ||
days: [], | ||
setDays: () => {}, | ||
reloadDays: () => null, | ||
}); | ||
|
||
export const Days = () => { | ||
const [days, setDays] = useState<readonly Day[]>([]); | ||
const { loading } = useFetch<readonly Day[]>( | ||
"admin/days", | ||
setDays, | ||
convertDayJSON | ||
); | ||
|
||
const { enqueueSnackbar } = useSnackbar(); | ||
|
||
const reloadDays = () => { | ||
getApi<readonly Day[]>("admin/days", convertDayJSON) | ||
.then((data) => setDays(data)) | ||
// This is the admin page so just show the error | ||
.catch((error) => | ||
enqueueSnackbar(`Error getting all days: ${error}`, { | ||
variant: "error", | ||
}) | ||
); | ||
}; | ||
|
||
return ( | ||
<LoadingSkeleton loading={loading}> | ||
<DayContext.Provider value={{ days, setDays, reloadDays }}> | ||
<Grid | ||
container | ||
justifyContent="space-between" | ||
columnSpacing={4} | ||
rowSpacing={6} | ||
> | ||
<Grid item xs={12} md={6}> | ||
<DaysTable /> | ||
</Grid> | ||
<Grid item xs={12} md={6}> | ||
<DaysAdd /> | ||
</Grid> | ||
</Grid> | ||
</DayContext.Provider> | ||
</LoadingSkeleton> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Box, Button, Paper, Stack } from "@mui/material"; | ||
import { DatePicker, LocalizationProvider } from "@mui/x-date-pickers"; | ||
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"; | ||
import dayjs, { Dayjs } from "dayjs"; | ||
import { useSnackbar } from "notistack"; | ||
import { Dispatch, SetStateAction, useContext, useState } from "react"; | ||
import { TypographyG } from "../../../components/TypographyG"; | ||
import { postApi } from "../../../util/fetch"; | ||
import { DayContext } from "./Days"; | ||
export const DaysAdd = () => { | ||
const { reloadDays } = useContext(DayContext); | ||
const [startDate, setStartDate] = useState<Dayjs | null>(dayjs()); | ||
const [endDate, setEndDate] = useState<Dayjs | null>(dayjs()); | ||
|
||
const { enqueueSnackbar } = useSnackbar(); | ||
|
||
const handleDateChange = ( | ||
date: Dayjs | null, | ||
setter: Dispatch<SetStateAction<Dayjs | null>> | ||
) => setter(date); | ||
|
||
const handleOnClick = () => { | ||
if (!startDate || !endDate) { | ||
enqueueSnackbar("Please select a start and end date", { | ||
variant: "error", | ||
}); | ||
return; | ||
} | ||
|
||
postApi("admin/days", { | ||
startDate: startDate.toISOString(), | ||
endDate: endDate.toISOString(), | ||
}) | ||
.then(() => { | ||
enqueueSnackbar("successfully saved days", { | ||
variant: "success", | ||
}); | ||
reloadDays(); | ||
}) | ||
.catch((error) => | ||
// This is the admin page so just show the error | ||
enqueueSnackbar(`Failed to save days: ${error}`, { | ||
variant: "error", | ||
}) | ||
); | ||
}; | ||
|
||
return ( | ||
<Paper elevation={4} sx={{ py: 2 }}> | ||
<Stack | ||
display="flex" | ||
justifyContent="center" | ||
alignItems="center" | ||
spacing={4} | ||
> | ||
<TypographyG variant="h4">Add days</TypographyG> | ||
<Stack direction="row" spacing={2}> | ||
<LocalizationProvider dateAdapter={AdapterDayjs}> | ||
<DatePicker | ||
label="Start Date" | ||
value={startDate} | ||
onChange={(newValue) => | ||
handleDateChange(newValue, setStartDate) | ||
} | ||
/> | ||
<DatePicker | ||
label="End Date" | ||
value={endDate} | ||
onChange={(newValue) => | ||
handleDateChange(newValue, setEndDate) | ||
} | ||
/> | ||
</LocalizationProvider> | ||
</Stack> | ||
<Box | ||
display="flex" | ||
justifyContent="end" | ||
width="100%" | ||
sx={{ pr: 2 }} | ||
> | ||
<Button variant="outlined" onClick={handleOnClick}> | ||
Add | ||
</Button> | ||
</Box> | ||
</Stack> | ||
</Paper> | ||
); | ||
}; |
Oops, something went wrong.