Skip to content

Commit

Permalink
feat(calendar): add time input to occurrence dialog (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
domhhv authored Dec 28, 2024
1 parent 6c98f83 commit 6ab8ed9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
12 changes: 4 additions & 8 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@
}
},
"rules": {
"object-shorthand": "error",
"no-undef": "off",
"no-duplicate-imports": "error",
"no-useless-rename": "error",
"no-console": ["warn", { "allow": ["warn", "error"] }],
"@typescript-eslint/consistent-type-imports": [
"error",
{
"fixStyle": "inline-type-imports"
}
],
"object-shorthand": "error",
"import/no-duplicates": "error",
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/no-unused-vars": [
"error",
{
Expand Down
53 changes: 43 additions & 10 deletions src/components/calendar/AddOccurrenceDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parseAbsoluteToLocal, ZonedDateTime } from '@internationalized/date';
import {
Button,
Modal,
Expand All @@ -10,8 +11,9 @@ import {
Select,
SelectItem,
SelectSection,
type Selection,
TimeInput,
} from '@nextui-org/react';
import type { TimeInputValue, Selection } from '@nextui-org/react';
import { ArrowsClockwise } from '@phosphor-icons/react';
import { useHabitsStore, useNotesStore, useOccurrencesStore } from '@stores';
import { useUser } from '@supabase/auth-helpers-react';
Expand All @@ -31,32 +33,53 @@ const AddOccurrenceDialog = ({
onClose,
date,
}: AddOccurrenceDialogProps) => {
const { habits } = useHabitsStore();
const user = useUser();
const { habits } = useHabitsStore();
const { addNote, addingNote } = useNotesStore();
const { addOccurrence, addingOccurrence } = useOccurrencesStore();
const [selectedHabitId, setSelectedHabitId] = React.useState<Selection>(
new Set<number>([])
);
const [note, setNote] = React.useState('');
const { addNote, addingNote } = useNotesStore();
const [selectedHabitId, setSelectedHabitId] = React.useState<
Selection & { currentKey?: string }
>(new Set<number>([]));
const [time, setTime] = React.useState<TimeInputValue | null>(null);

const habitsByTraitName = React.useMemo(() => {
return Object.groupBy(habits, (habit) => habit.trait?.name || 'Unknown');
}, [habits]);

const hasHabits = habits.length > 0;

React.useEffect(() => {
if (!date) {
setTime(parseAbsoluteToLocal(new Date().toISOString()));
return;
}

const occurrenceDateTime = new Date();

occurrenceDateTime.setFullYear(date.getFullYear());
occurrenceDateTime.setMonth(date.getMonth());
occurrenceDateTime.setDate(date.getDate());

setTime(parseAbsoluteToLocal(occurrenceDateTime.toISOString()));
}, [date]);

const handleSubmit: MouseEventHandler<HTMLButtonElement> = async (event) => {
event.preventDefault();

if (!user || !hasHabits || !selectedHabitId) {
if (!user || !date || !hasHabits || !selectedHabitId.currentKey) {
return null;
}

const occurrenceDateTime = new Date(date);

if (time instanceof ZonedDateTime) {
occurrenceDateTime.setHours(time.hour);
occurrenceDateTime.setMinutes(time.minute);
}

const newOccurrence = await addOccurrence({
timestamp: +date!,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
timestamp: +occurrenceDateTime,
habitId: +selectedHabitId.currentKey,
userId: user?.id as string,
});
Expand Down Expand Up @@ -138,6 +161,15 @@ const AddOccurrenceDialog = ({
placeholder="Note"
variant="faded"
/>
<div className="flex w-full flex-col gap-y-2">
<TimeInput
label="Time"
value={time}
onChange={setTime}
variant="faded"
maxValue={parseAbsoluteToLocal(new Date().toISOString())}
/>
</div>
</ModalBody>
<ModalFooter>
<Button
Expand All @@ -146,6 +178,7 @@ const AddOccurrenceDialog = ({
color="primary"
isLoading={addingOccurrence || addingNote}
onClick={hasHabits ? handleSubmit : undefined}
isDisabled={hasHabits && !selectedHabitId.currentKey}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
to={hasHabits ? undefined : '/habits'}
Expand Down

0 comments on commit 6ab8ed9

Please sign in to comment.