Refactor NotesPanel component to integrate ApiServerContext for fetching notes, add support for parent type in new note creation, and streamline note handling logic.
This commit is contained in:
parent
9a1f58aafe
commit
38f03f8fe9
@ -26,6 +26,7 @@ import MarkdownDisplay from './MarkdownDisplay'
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import config from '../../../config'
|
import config from '../../../config'
|
||||||
import { AuthContext } from '../context/AuthContext'
|
import { AuthContext } from '../context/AuthContext'
|
||||||
|
import { ApiServerContext } from '../context/ApiServerContext'
|
||||||
import InfoCircleIcon from '../../Icons/InfoCircleIcon'
|
import InfoCircleIcon from '../../Icons/InfoCircleIcon'
|
||||||
import NoteTypeSelect from './NoteTypeSelect'
|
import NoteTypeSelect from './NoteTypeSelect'
|
||||||
import IdDisplay from './IdDisplay'
|
import IdDisplay from './IdDisplay'
|
||||||
@ -259,7 +260,7 @@ NoteItem.propTypes = {
|
|||||||
onChildNoteAdded: PropTypes.func
|
onChildNoteAdded: PropTypes.func
|
||||||
}
|
}
|
||||||
|
|
||||||
const NotesPanel = ({ _id, onNewNote }) => {
|
const NotesPanel = ({ _id, onNewNote, type }) => {
|
||||||
const [newNoteOpen, setNewNoteOpen] = useState(false)
|
const [newNoteOpen, setNewNoteOpen] = useState(false)
|
||||||
const [showMarkdown, setShowMarkdown] = useState(false)
|
const [showMarkdown, setShowMarkdown] = useState(false)
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
@ -273,6 +274,7 @@ const NotesPanel = ({ _id, onNewNote }) => {
|
|||||||
const [expandedNotes, setExpandedNotes] = useState({})
|
const [expandedNotes, setExpandedNotes] = useState({})
|
||||||
const [newNoteForm] = Form.useForm()
|
const [newNoteForm] = Form.useForm()
|
||||||
const [selectedParentId, setSelectedParentId] = useState(null)
|
const [selectedParentId, setSelectedParentId] = useState(null)
|
||||||
|
const [selectedParentType, setSelectedParentType] = useState(null)
|
||||||
const [childNoteCallbacks, setChildNoteCallbacks] = useState({})
|
const [childNoteCallbacks, setChildNoteCallbacks] = useState({})
|
||||||
const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false)
|
const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false)
|
||||||
const [noteToDelete, setNoteToDelete] = useState(null)
|
const [noteToDelete, setNoteToDelete] = useState(null)
|
||||||
@ -289,22 +291,12 @@ const NotesPanel = ({ _id, onNewNote }) => {
|
|||||||
}, [newNoteForm, newNoteFormUpdateValues])
|
}, [newNoteForm, newNoteFormUpdateValues])
|
||||||
|
|
||||||
const { authenticated, userProfile } = useContext(AuthContext)
|
const { authenticated, userProfile } = useContext(AuthContext)
|
||||||
|
const { fetchNotes } = useContext(ApiServerContext)
|
||||||
|
|
||||||
const fetchData = useCallback(async (id) => {
|
const fetchData = useCallback(
|
||||||
|
async (id) => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(`${config.backendUrl}/notes`, {
|
const newData = await fetchNotes(id)
|
||||||
params: {
|
|
||||||
parent: id,
|
|
||||||
sort: 'createdAt',
|
|
||||||
order: 'ascend'
|
|
||||||
},
|
|
||||||
headers: {
|
|
||||||
Accept: 'application/json'
|
|
||||||
},
|
|
||||||
withCredentials: true
|
|
||||||
})
|
|
||||||
|
|
||||||
const newData = response.data
|
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
return newData
|
return newData
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -312,7 +304,20 @@ const NotesPanel = ({ _id, onNewNote }) => {
|
|||||||
setError(error)
|
setError(error)
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
}, [])
|
},
|
||||||
|
[fetchNotes]
|
||||||
|
)
|
||||||
|
|
||||||
|
const handleNewChildNote = useCallback(
|
||||||
|
(parentId) => {
|
||||||
|
setSelectedParentId(parentId)
|
||||||
|
setSelectedParentType('note')
|
||||||
|
setNewNoteOpen(true)
|
||||||
|
newNoteForm.resetFields()
|
||||||
|
setNewNoteFormValues({})
|
||||||
|
},
|
||||||
|
[newNoteForm]
|
||||||
|
)
|
||||||
|
|
||||||
const generateNotes = useCallback(
|
const generateNotes = useCallback(
|
||||||
async (id) => {
|
async (id) => {
|
||||||
@ -345,7 +350,7 @@ const NotesPanel = ({ _id, onNewNote }) => {
|
|||||||
expandedNotes={expandedNotes}
|
expandedNotes={expandedNotes}
|
||||||
setExpandedNotes={setExpandedNotes}
|
setExpandedNotes={setExpandedNotes}
|
||||||
fetchData={fetchData}
|
fetchData={fetchData}
|
||||||
onNewNote={handleNewNoteFromDropdown}
|
onNewNote={handleNewChildNote}
|
||||||
onDeleteNote={handleDeleteNote}
|
onDeleteNote={handleDeleteNote}
|
||||||
userProfile={userProfile}
|
userProfile={userProfile}
|
||||||
onChildNoteAdded={(noteId, callback) => {
|
onChildNoteAdded={(noteId, callback) => {
|
||||||
@ -357,7 +362,7 @@ const NotesPanel = ({ _id, onNewNote }) => {
|
|||||||
/>
|
/>
|
||||||
))
|
))
|
||||||
},
|
},
|
||||||
[loading, fetchData, expandedNotes, userProfile]
|
[loading, fetchData, expandedNotes, userProfile, handleNewChildNote]
|
||||||
)
|
)
|
||||||
|
|
||||||
const handleNewNote = async () => {
|
const handleNewNote = async () => {
|
||||||
@ -365,7 +370,11 @@ const NotesPanel = ({ _id, onNewNote }) => {
|
|||||||
try {
|
try {
|
||||||
await axios.post(
|
await axios.post(
|
||||||
`${config.backendUrl}/notes`,
|
`${config.backendUrl}/notes`,
|
||||||
{ ...newNoteFormValues, parent: selectedParentId || _id },
|
{
|
||||||
|
...newNoteFormValues,
|
||||||
|
parent: selectedParentId,
|
||||||
|
parentType: selectedParentType
|
||||||
|
},
|
||||||
{
|
{
|
||||||
withCredentials: true
|
withCredentials: true
|
||||||
}
|
}
|
||||||
@ -402,13 +411,6 @@ const NotesPanel = ({ _id, onNewNote }) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleNewNoteFromDropdown = (parentId) => {
|
|
||||||
setSelectedParentId(parentId)
|
|
||||||
setNewNoteOpen(true)
|
|
||||||
newNoteForm.resetFields()
|
|
||||||
setNewNoteFormValues({})
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleDeleteNote = async (noteId) => {
|
const handleDeleteNote = async (noteId) => {
|
||||||
setNoteToDelete(noteId)
|
setNoteToDelete(noteId)
|
||||||
setDeleteConfirmOpen(true)
|
setDeleteConfirmOpen(true)
|
||||||
@ -499,7 +501,8 @@ const NotesPanel = ({ _id, onNewNote }) => {
|
|||||||
setLoading(true)
|
setLoading(true)
|
||||||
handleReloadData()
|
handleReloadData()
|
||||||
} else if (key === 'newNote') {
|
} else if (key === 'newNote') {
|
||||||
setSelectedParentId(null)
|
setSelectedParentId(_id)
|
||||||
|
setSelectedParentType(type)
|
||||||
setNewNoteOpen(true)
|
setNewNoteOpen(true)
|
||||||
newNoteForm.resetFields()
|
newNoteForm.resetFields()
|
||||||
setNewNoteFormValues({})
|
setNewNoteFormValues({})
|
||||||
@ -522,7 +525,8 @@ const NotesPanel = ({ _id, onNewNote }) => {
|
|||||||
icon={<PlusIcon />}
|
icon={<PlusIcon />}
|
||||||
disabled={loading}
|
disabled={loading}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setSelectedParentId(null)
|
setSelectedParentId(_id)
|
||||||
|
setSelectedParentType(type)
|
||||||
setNewNoteOpen(true)
|
setNewNoteOpen(true)
|
||||||
newNoteForm.resetFields()
|
newNoteForm.resetFields()
|
||||||
setNewNoteFormValues({})
|
setNewNoteFormValues({})
|
||||||
@ -688,6 +692,7 @@ const NotesPanel = ({ _id, onNewNote }) => {
|
|||||||
|
|
||||||
NotesPanel.propTypes = {
|
NotesPanel.propTypes = {
|
||||||
_id: PropTypes.string.isRequired,
|
_id: PropTypes.string.isRequired,
|
||||||
|
type: PropTypes.string.isRequired,
|
||||||
onNewNote: PropTypes.func
|
onNewNote: PropTypes.func
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user