+
{
diff --git a/src/components/habit/edit-habit/EditHabitDialog.test.tsx b/src/components/habit/edit-habit/EditHabitDialog.test.tsx
index 6077d01..d6d7316 100644
--- a/src/components/habit/edit-habit/EditHabitDialog.test.tsx
+++ b/src/components/habit/edit-habit/EditHabitDialog.test.tsx
@@ -37,7 +37,6 @@ describe(EditHabitDialog.name, () => {
});
const props: EditHabitDialogProps = {
- open: true,
onClose: jest.fn(),
habit: {
id: 7,
@@ -72,11 +71,6 @@ describe(EditHabitDialog.name, () => {
expect(queryByRole('dialog')).toBeNull();
});
- it('should not render if open is false', () => {
- const { queryByRole } = render(
);
- expect(queryByRole('dialog')).toBeNull();
- });
-
it('should call onClose when closed', async () => {
const onClose = jest.fn();
const { getByLabelText } = render(
diff --git a/src/components/habit/edit-habit/EditHabitDialog.tsx b/src/components/habit/edit-habit/EditHabitDialog.tsx
index 9cb70ad..0b0f16a 100644
--- a/src/components/habit/edit-habit/EditHabitDialog.tsx
+++ b/src/components/habit/edit-habit/EditHabitDialog.tsx
@@ -11,6 +11,7 @@ import {
Select,
SelectItem,
Textarea,
+ useDisclosure,
} from '@nextui-org/react';
import { useHabitsStore, useTraitsStore } from '@stores';
import { useUser } from '@supabase/auth-helpers-react';
@@ -18,17 +19,12 @@ import { toEventLike } from '@utils';
import React from 'react';
export type EditHabitDialogProps = {
- open: boolean;
habit: Habit | null;
onClose?: () => void;
};
-const EditHabitDialog = ({
- open = false,
- habit,
- onClose,
-}: EditHabitDialogProps) => {
- const [isOpen, setIsOpen] = React.useState(false);
+const EditHabitDialog = ({ habit, onClose }: EditHabitDialogProps) => {
+ const { isOpen, onOpen, onClose: onDisclosureClose } = useDisclosure();
const [name, handleNameChange] = useTextField();
const [description, handleDescriptionChange] = useTextField();
const [traitId, setTraitId] = React.useState('');
@@ -37,8 +33,12 @@ const EditHabitDialog = ({
const user = useUser();
React.useEffect(() => {
- setIsOpen(open);
- }, [open]);
+ if (habit) {
+ onOpen();
+ } else {
+ onClose?.();
+ }
+ }, [habit, onOpen, onClose]);
React.useEffect(() => {
if (habit) {
@@ -48,12 +48,12 @@ const EditHabitDialog = ({
}
}, [habit, handleNameChange, handleDescriptionChange]);
- if (!isOpen || !habit) {
+ if (!habit) {
return null;
}
const handleClose = () => {
- setIsOpen(false);
+ onDisclosureClose();
onClose?.();
};
@@ -75,7 +75,7 @@ const EditHabitDialog = ({
return (
{
))}
-
+