diff --git a/client/src/app/admin.tsx b/client/src/app/admin.tsx new file mode 100644 index 0000000..e80e320 --- /dev/null +++ b/client/src/app/admin.tsx @@ -0,0 +1,236 @@ +import { useEffect, useState } from "react" +import { SetState, Mode } from "./page" +import { + User, + CookieBreak, + Claim, + Settings, + Day, + dayNumberToDay, + formatAsPrice, +} from "./structs" +import { login, postSettings } from "./api" +import { TailSpin } from "react-loader-spinner" + +const LoginPage = (props: { + setUser: SetState + setBreaks: SetState + setClaims: SetState + setLoadingData: SetState + user: User | undefined + setMode: SetState +}) => { + const [userText, setUserText] = useState("") + const [passwordText, setPasswordText] = useState("") + const [errorText, setErrorText] = useState("") + const [isLoading, setLoading] = useState(false) + const onSubmit = async () => { + let result = await login( + userText, + passwordText, + props.setUser, + setErrorText, + props.setBreaks, + props.setClaims, + setLoading + ) + if (result === 0) { + props.setMode(Mode.Main) + } + } + const onClickSubmitButton = (e: React.MouseEvent) => { + onSubmit() + } + const onUserTextChange = (e: React.ChangeEvent) => + setUserText(e.target.value) + const onPasswordTextChange = (e: React.ChangeEvent) => + setPasswordText(e.target.value) + const onKeyDown = (e: React.KeyboardEvent) => { + if (e.key === "Enter") { + onSubmit() + } + } + return ( +
+

Admin login

+
+
+
+ +
+
+ +
+ {isLoading ? ( + + ) : ( + "" + )} +
+
+
+ ) +} + +const SettingsPage = (props: { + user: User + settings: Settings + setUser: SetState + setMode: SetState +}) => { + const onClickLogout = (e: React.MouseEvent) => { + props.setUser(undefined) + props.setMode(Mode.Main) + } + const onClickSave = (e: React.MouseEvent) => { + let budgetNumber = Number.parseFloat(budgetText) + if (isNaN(budgetNumber)) { + } else { + postSettings( + props.user, + selectedDay, + new Date(`1970-01-01T${selectedTime}`), + locationText, + budgetNumber + ) + } + } + const [selectedDay, setSelectedDay] = useState(props.settings.day) + const [selectedTime, setSelectedTime] = useState( + props.settings.time.toTimeString().substring(0, 5) + ) + const [budgetText, setBudgetText] = useState( + props.settings.budget.toFixed(2) + ) + const [locationText, setLocationText] = useState(props.settings.location) + const onChangeDay = (e: React.ChangeEvent) => + setSelectedDay(dayNumberToDay(Number.parseInt(e.target.value))) + const onChangeTime = (e: React.ChangeEvent) => + setSelectedTime(e.target.value) + const onChangeBudgetText = (e: React.ChangeEvent) => + setBudgetText(e.target.value) + const onChangeLocationText = (e: React.ChangeEvent) => + setLocationText(e.target.value) + const settingHeaderStyle = "font-bold my-2" + return ( +
+

Settings

+
+
+ Logged in as{" "} + {props.user?.user} +
+ +
+
+
Day
+ +
+
+
Time
+ +
+
+
Budget
+ +
+
+
Location
+ +
+ +
+ ) +} + +export const AdminPage = (props: { + setUser: SetState + setBreaks: SetState + setClaims: SetState + setLoadingData: SetState + user: User | undefined + setMode: SetState + settings: Settings | undefined +}) => { + return ( +
+ {!props.user ? ( + + ) : !props.settings ? ( + "" + ) : ( + + )} +
+ ) +}