-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Use same modal for create and edit todo
- Loading branch information
1 parent
6a9cc3c
commit a404d24
Showing
4 changed files
with
197 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <TodoModal dismiss={dismiss} /> | ||
} | ||
|
||
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, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.