Skip to content

Commit

Permalink
feat: prohibit selection of too old start date in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-wasmeier-titanom committed May 22, 2024
1 parent d36acb3 commit 13247e8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
30 changes: 14 additions & 16 deletions frontend/src/screens/create-task-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { fetchWrapper } from "../utils/fetchWrapper";
import { getUsers } from "./assignments";
import UserMultiSelect from "../components/user-multi-select";
import { queryKeys } from "../utils/queryKeys";
import { setTimeToZero, addDays } from "../utils/date";
const createTaskGroupSchema = z.object({
title: z.string().min(1, { message: "Title is missing" }),
description: z.string().optional(),
Expand Down Expand Up @@ -56,6 +57,7 @@ export function CreateTaskGroupScreen() {
handleSubmit,
formState: { errors },
reset: resetForm,
watch,
} = useForm<CreateTaskGroup>({
defaultValues,
resolver: zodResolver(createTaskGroupSchema),
Expand All @@ -72,6 +74,8 @@ export function CreateTaskGroupScreen() {
const [date, setDate] = React.useState<Date | undefined>(new Date());
const [showDatePicker, setShowDatePicker] = React.useState(false);

const intervalDays = watch("intervalDays");

const { mutate } = useMutation({
mutationFn: ({ ...args }: CreateTaskGroup) =>
createTaskGroup({
Expand Down Expand Up @@ -183,20 +187,16 @@ export function CreateTaskGroupScreen() {
{Platform.select({
ios: (
<RNDateTimePicker
value={date ? date : new Date(new Date().setHours(0, 0, 0, 0))}
value={date ? date : setTimeToZero(new Date())}
onChange={(e, date) => {
setDate(
new Date(
date?.setHours(0, 0, 0, 0) ??
new Date(new Date().setHours(0, 0, 0, 0))
)
);
setDate(setTimeToZero(date ?? new Date()));
setShowDatePicker(false);
}}
accentColor="lightblue"
mode="date"
themeVariant="dark"
timeZoneName="Europe/Berlin"
minimumDate={addDays(new Date(), -(Number(intervalDays) - 1))}
/>
),
android: (
Expand All @@ -211,26 +211,24 @@ export function CreateTaskGroupScreen() {
</Pressable>
{showDatePicker && (
<RNDateTimePicker
value={
date ? date : new Date(new Date().setHours(0, 0, 0, 0))
}
value={date ? date : setTimeToZero(new Date())}
onChange={(e, date) => {
setDate(
new Date(
date?.setHours(0, 0, 0, 0) ??
new Date(new Date().setHours(0, 0, 0, 0))
)
);
setDate(setTimeToZero(date ?? new Date()));
setShowDatePicker(false);
}}
accentColor="lightblue"
mode="date"
themeVariant="dark"
minimumDate={addDays(
new Date(),
-(Number(intervalDays) - 1)
)}
/>
)}
</>
),
web: (
// TODO: add mindate
<WebDateTimerPicker
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setDate(
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function addDays(date: Date, days: number) {
const newDate = new Date(date);
newDate.setDate(date.getDate() + days);
return newDate;
}

export function setTimeToZero(date: Date) {
const newDate = new Date(date).setHours(0, 0, 0, 0);
return new Date(newDate);
}

0 comments on commit 13247e8

Please sign in to comment.