From eb9f6d0ca84588ed496bafb43967952bcb2f9557 Mon Sep 17 00:00:00 2001 From: Araozu Date: Mon, 18 Dec 2023 15:13:48 -0500 Subject: [PATCH] [FE][Certs] Buttons to set date yesterday/sunday --- .../certs/NewRegister/ManualRegistration.tsx | 42 ++++++++++++++++++- .../src/certs/NewRegister/RegisterPresets.tsx | 40 +++++++++++++++++- frontend/src/certs/NewRegister/index.tsx | 4 +- frontend/src/components/Chip.tsx | 8 +++- 4 files changed, 89 insertions(+), 5 deletions(-) diff --git a/frontend/src/certs/NewRegister/ManualRegistration.tsx b/frontend/src/certs/NewRegister/ManualRegistration.tsx index 0c60f78..9947f76 100644 --- a/frontend/src/certs/NewRegister/ManualRegistration.tsx +++ b/frontend/src/certs/NewRegister/ManualRegistration.tsx @@ -4,6 +4,7 @@ import { CustomLabelSelect } from "./CustomLabelSelect"; import { RegistrationPreview } from "."; import { useCourses } from "../CourseContext"; import { useCustomLabel } from "../CustomLabelContext"; +import { Chip } from "../../components/Chip"; type HTMLEventFn = JSX.EventHandlerUnion { + if (datePicker === undefined) { + return; + } + + datePicker.value = new Date(Date.now() - 86400000).toISOString() + .split("T")[0]; + }; + + const setDateAsLastSunday = () => { + if (datePicker === undefined) { + return; + } + + datePicker.value = getLastSunday(); + }; + return ( <>
@@ -101,7 +119,7 @@ export function ManualRegistration(props: { /> -
+
+
+ + +
+
); } + +// If today is sunday, this returns today. Otherwise, it returns the last sunday +function getLastSunday() { + const today = new Date(); + const day = today.getDay(); + const diff = today.getDate() - day; + const lastSunday = new Date(today.setDate(diff)); + return lastSunday.toISOString().split("T")[0]; +} diff --git a/frontend/src/certs/NewRegister/RegisterPresets.tsx b/frontend/src/certs/NewRegister/RegisterPresets.tsx index 84c9e72..49b620d 100644 --- a/frontend/src/certs/NewRegister/RegisterPresets.tsx +++ b/frontend/src/certs/NewRegister/RegisterPresets.tsx @@ -101,6 +101,23 @@ export function RegisterPresets(props: { setSelectedPreset("None"); }; + const setDateAsYesterday = () => { + if (datePicker === undefined) { + return; + } + + datePicker.value = new Date(Date.now() - 86400000).toISOString() + .split("T")[0]; + }; + + const setDateAsLastSunday = () => { + if (datePicker === undefined) { + return; + } + + datePicker.value = getLastSunday(); + }; + return ( <>
@@ -118,7 +135,7 @@ export function RegisterPresets(props: {
-
+
+
+ + +
@@ -158,3 +187,12 @@ export function RegisterPresets(props: { ); } + +// If today is sunday, this returns today. Otherwise, it returns the last sunday +function getLastSunday() { + const today = new Date(); + const day = today.getDay(); + const diff = today.getDate() - day; + const lastSunday = new Date(today.setDate(diff)); + return lastSunday.toISOString().split("T")[0]; +} diff --git a/frontend/src/certs/NewRegister/index.tsx b/frontend/src/certs/NewRegister/index.tsx index 38afeb6..f408629 100644 --- a/frontend/src/certs/NewRegister/index.tsx +++ b/frontend/src/certs/NewRegister/index.tsx @@ -30,11 +30,11 @@ export function NewRegister(props: { return (
-

Registrar certificados

+

Registrar certificados

-
+
setSelections((x) => [...x, v])} diff --git a/frontend/src/components/Chip.tsx b/frontend/src/components/Chip.tsx index 17dee9d..b200a9f 100644 --- a/frontend/src/components/Chip.tsx +++ b/frontend/src/components/Chip.tsx @@ -1,4 +1,9 @@ -export function Chip(props: {text: string, selected: boolean, select: () => void}) { +export function Chip(props: { + text: string, + selected: boolean, + select: () => void, + type?: "button" | "submit", +}) { const selectedClasses = () => { if (props.selected) { return "bg-c-surface-variant text-c-on-surface-variant shadow"; @@ -13,6 +18,7 @@ export function Chip(props: {text: string, selected: boolean, select: () => void hover:bg-c-surface-variant hover:text-c-on-surface-variant transition-colors select-none ${selectedClasses()}`} onClick={props.select} + type={props.type ?? "button"} > {props.text}