-
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.
Re-work document create, update, and delete operations to be (mostly) synchronized with search, such that navigating back to the documents list after create / edit / delete reflects the changes in search results. - When creatnig a document, auto-save it and navigate to edit view, adding to current search - When editing, update the document (title, etc) so its reflected in search - When deleting, remove the document from the search results Also re-work document creation to be fully separate from EditableDocument, and remove hack that hid the fact that (technically) stores could be null when pulled from context; updated all calls to append ! so its documented what is happening; hoepfully this protects against future refactors where consumers are yanked out of the provider that sets up the context.
- Loading branch information
Showing
13 changed files
with
194 additions
and
128 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
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,57 @@ | ||
import React, { useContext, useEffect, useState } from "react"; | ||
import { observer } from "mobx-react-lite"; | ||
import { EditLoadingComponent } from "../edit/loading"; | ||
import { useIsMounted } from "../../hooks/useIsMounted"; | ||
import { JournalsStoreContext } from "../../hooks/useJournalsLoader"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { SearchStoreContext } from "../documents/SearchStore"; | ||
import useClient from "../../hooks/useClient"; | ||
|
||
// Creates a new document and immediately navigates to it | ||
function useCreateDocument() { | ||
const journalsStore = useContext(JournalsStoreContext)!; | ||
|
||
// NOTE: Could move this hook but, but it assumes searchStore is defined, and its setup | ||
// in the root documents view. So better to keep it here for now. | ||
const searchStore = useContext(SearchStoreContext)!; | ||
const navigate = useNavigate(); | ||
const client = useClient(); | ||
const isMounted = useIsMounted(); | ||
const [error, setError] = useState<Error | null>(null); | ||
|
||
useEffect(() => { | ||
(async function () { | ||
if (!isMounted) return; | ||
|
||
try { | ||
const document = await client.documents.save({ | ||
content: "", | ||
journalId: journalsStore.defaultJournal(searchStore.selectedJournals), | ||
tags: [], // todo: tagsStore.defaultTags; | ||
}); | ||
|
||
if (!isMounted) return; | ||
|
||
// Ensure the document is added to the search, so its available when user hits | ||
// back (even when that doesn't make sense!) | ||
searchStore.updateSearch(document, "create"); | ||
navigate(`/documents/edit/${document.id}`, { replace: true }); | ||
} catch (err) { | ||
console.error("Error creating document", err); | ||
if (!isMounted) return; | ||
setError(err as Error); | ||
} | ||
})(); | ||
}, []); | ||
|
||
return { error }; | ||
} | ||
|
||
// Creates a new document and immediately navigates to it | ||
function DocumentCreator() { | ||
const { error } = useCreateDocument(); | ||
|
||
return <EditLoadingComponent error={error} />; | ||
} | ||
|
||
export default observer(DocumentCreator); |
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
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
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.