diff --git a/components/pages/Home.tsx b/components/pages/Home.tsx index 2e1e2cd..47d3f9d 100644 --- a/components/pages/Home.tsx +++ b/components/pages/Home.tsx @@ -63,6 +63,7 @@ import useNoteProvider from '../notes/useNoteProvider' import useSettings from '../settings/useSettings' import { SelectedTodoProvider } from '../todos/SelectedTodo' import { useTodoActionSheet } from '../todos/TodoActionSheet' +import { useCreateTodoModal } from '../todos/CreateTodo' const Home = () => { // Search stuff @@ -115,11 +116,14 @@ const Home = () => { // Creating todo stuff const fab = useRef(null) - const [createTodoModalOpen, setCreateTodoModalOpen] = useState(false) + const [presentCreateTodoModal, dismiss] = useCreateTodoModal() const openCreateTodoModal = useCallback(() => { - setCreateTodoModalOpen(true) - if (fab.current) fab.current.activated = true - }, [fab]) + presentCreateTodoModal({ + onWillDismiss: () => { + if (fab.current) fab.current.activated = false + }, + }) + }, [fab, presentCreateTodoModal]) const contentRef = useRef(null) @@ -201,13 +205,6 @@ const Home = () => { - { - setCreateTodoModalOpen(false) - }} - /> )} @@ -735,107 +732,6 @@ export const IceboxItem = ({ ) } -export const CreateTodoModal = ({ - fab, - open, - onWillDismiss, -}: { - fab: RefObject - open: boolean - onWillDismiss: ComponentProps['onWillDismiss'] -}) => { - const modal = useRef(null) - - const input = useRef(null) - const noteInput = useRef(null) - const noteProvider = useNoteProvider() - - const createTodo = useCallback( - async ({ note, title }: { note?: any; title: any }) => { - let uri - if (note && noteProvider) { - uri = await noteProvider.create({ content: note }) - } - await db.todos.add({ - createdAt: new Date(), - title, - ...(uri && { note: { uri } }), - }) - }, - [noteProvider], - ) - - return ( - input.current?.setFocus()} - onKeyDown={event => { - if (event.key === 'Enter') { - event.preventDefault() - modal.current?.dismiss({}, 'confirm') - } - }} - onWillDismiss={event => { - onWillDismiss?.(event) - if (event.detail.role === 'confirm') { - createTodo({ - title: input.current?.value, - note: noteInput.current?.value, - }) - } - if (fab.current) fab.current.activated = false - }} - > - - - Create todo - - modal.current?.dismiss({}, 'cancel')} - > - Cancel - - - - { - modal.current?.dismiss({}, 'confirm') - }} - strong={true} - > - Confirm - - - - - - - {!noteProvider && ( -

Set a note provider in settings to enable this feature.

- )} - -
-
- ) -} - function moveItemInArray( array: T[], fromIndex: number, diff --git a/components/todos/CreateTodo.tsx b/components/todos/CreateTodo.tsx new file mode 100644 index 0000000..bddef91 --- /dev/null +++ b/components/todos/CreateTodo.tsx @@ -0,0 +1,56 @@ +import { IonHeader, IonModal, useIonModal } from '@ionic/react' +import { ComponentProps, RefObject, useCallback, useRef } from 'react' +import useNoteProvider from '../notes/useNoteProvider' +import { db } from '../db' +import TodoModal from './TodoModal' +import { HookOverlayOptions } from '@ionic/react/dist/types/hooks/HookOverlayOptions' + +export function CreateTodoModal({ + dismiss, +}: { + dismiss: (data?: any, role?: string) => void +}) { + return +} + +export function useCreateTodoModal(): [ + ({ + onWillDismiss, + }: { + onWillDismiss: HookOverlayOptions['onWillDismiss'] + }) => void, + (data?: any, role?: string) => void, +] { + const [present, dismiss] = useIonModal(CreateTodoModal, { + dismiss: (data: string, role: string) => dismiss(data, role), + }) + + const noteProvider = useNoteProvider() + const createTodo = useCallback( + async ({ note, title }: { note?: any; title: any }) => { + let uri + if (note && noteProvider) { + uri = await noteProvider.create({ content: note }) + } + await db.todos.add({ + createdAt: new Date(), + title, + ...(uri && { note: { uri } }), + }) + }, + [noteProvider], + ) + + return [ + ({ onWillDismiss }: HookOverlayOptions) => { + present({ + onWillDismiss: event => { + const todo = event.detail.data + if (event.detail.role === 'confirm') createTodo(todo) + onWillDismiss?.(event) + }, + }) + }, + dismiss, + ] +} diff --git a/components/todos/EditTodo.tsx b/components/todos/EditTodo.tsx index 7925cfb..edf8a72 100644 --- a/components/todos/EditTodo.tsx +++ b/components/todos/EditTodo.tsx @@ -1,21 +1,9 @@ -import { - IonButton, - IonButtons, - IonContent, - IonHeader, - IonIcon, - IonInput, - IonPage, - IonTextarea, - IonTitle, - IonToolbar, - useIonModal, -} from '@ionic/react' -import { openOutline } from 'ionicons/icons' -import { useCallback, useEffect, useRef } from 'react' +import { useIonModal } from '@ionic/react' +import { useCallback } from 'react' import { CreatedTodo, db } from '../db' import useNoteProvider from '../notes/useNoteProvider' import useSelectedTodo from './SelectedTodo' +import TodoModal from './TodoModal' export function EditTodoModal({ dismiss, @@ -24,105 +12,18 @@ export function EditTodoModal({ dismiss: (data?: any, role?: string) => void todo: CreatedTodo }) { - const page = useRef(null) - const input = useRef(null) - const noteInput = useRef(null) - - useEffect(() => { - input.current?.setFocus() - }, []) - - const noteProvider = useNoteProvider() - return ( - { - if (event.key === 'Enter') { - event.preventDefault() - dismiss( - { - ...todo, - title: input.current?.value, - note: noteInput.current?.value, - }, - 'confirm', - ) - } - }} - > - - - Edit todo - - dismiss(null, 'cancel')} - > - Cancel - - - - { - dismiss( - { - ...todo, - title: input.current?.value, - note: noteInput.current?.value, - }, - 'confirm', - ) - }} - strong={true} - > - Confirm - - - - - - - {!noteProvider && ( -

Set a note provider in settings to enable this feature.

- )} - {todo?.note ? ( - - ) : ( - - )} -
-
+ ) } -export function useEditTodoModal() { +export function useEditTodoModal(): [ + (todo: CreatedTodo) => void, + (data?: any, role?: string) => void, +] { const [todo, setTodo] = useSelectedTodo() const [present, dismiss] = useIonModal(EditTodoModal, { dismiss: (data: string, role: string) => dismiss(data, role), diff --git a/components/todos/TodoModal.tsx b/components/todos/TodoModal.tsx new file mode 100644 index 0000000..7c53613 --- /dev/null +++ b/components/todos/TodoModal.tsx @@ -0,0 +1,122 @@ +import { useEffect, useRef } from 'react' +import useNoteProvider from '../notes/useNoteProvider' +import { + IonButton, + IonButtons, + IonContent, + IonHeader, + IonIcon, + IonInput, + IonPage, + IonTextarea, + IonTitle, + IonToolbar, +} from '@ionic/react' +import { openOutline } from 'ionicons/icons' +import { Todo } from '../db' + +export default function TodoModal({ + dismiss, + todo = {}, +}: { + dismiss: (data?: any, role?: string) => void + todo?: Partial +}) { + const page = useRef(null) + const input = useRef(null) + const noteInput = useRef(null) + + // Might cause problems that this runs on every render but seems fine atm... + useEffect(() => { + input.current?.setFocus() + }) + + const noteProvider = useNoteProvider() + + return ( + { + if (event.key === 'Enter') { + event.preventDefault() + dismiss( + { + ...todo, + title: input.current?.value, + note: noteInput.current?.value, + }, + 'confirm', + ) + } + }} + > + + + Edit todo + + dismiss(null, 'cancel')} + > + Cancel + + + + { + dismiss( + { + ...todo, + title: input.current?.value, + note: noteInput.current?.value, + }, + 'confirm', + ) + }} + strong={true} + > + Confirm + + + + + + + {!noteProvider && ( +

Set a note provider in settings to enable this feature.

+ )} + {todo?.note ? ( + + ) : ( + + )} +
+
+ ) +}