import React, {useState} from "react"; import { Link } from "react-router-dom"; import PermissionGuard from "../PermissionGuard"; import "./PathNode.css"; import {useDeletePath, useMovePath, usePaths, useUpdatePath} from "../../utils/path-queries"; import {useIndexMarkdown, useMarkdownsByPath, useMoveMarkdown} from "../../utils/markdown-queries"; const PathNode = ({ path, isRoot = false }) => { const [isExpanded, setIsExpanded] = useState(isRoot); const [isEditing, setIsEditing] = useState(false); const [newName, setNewName] = useState(path.name); const { data: childPaths, isLoading: isChildLoading, error: childError } = usePaths(path.id); const { data: markdowns, isLoading: isMarkdownLoading, error: markdownError } = useMarkdownsByPath(path.id); const deletePath = useDeletePath(); const updatePath = useUpdatePath(); const {data: indexMarkdown, isLoading: isIndexLoading, error: indexMarkdownError} = useIndexMarkdown(path.id); const movePath = useMovePath(); const moveMarkdown = useMoveMarkdown(); const toggleExpand = () => { setIsExpanded(!isExpanded); }; const handleSave = () => { console.log(`handleSave ${path.id}`); updatePath.mutate({id: path.id, data: {name: newName}}, { onsuccess: () => setIsEditing(false), onError: err => alert("failed to update this path"), }) }; const handleDelete = () => { if(window.confirm("Are you sure?")) { deletePath.mutate(path.id, { onError: err => alert("failed to delete this path"), }) } }; const handleEdit = () => { setIsEditing(true); }; const handleMovePath = (pth, direction) => { movePath.mutate({path: pth, direction: direction}, { onError: () => alert("failed to move this path"), }); }; const handleMoveMarkdown = (md, direction) => { moveMarkdown.mutate({markdown: md, direction: direction}, { onError: () => alert("failed to move this markdown"), }) }; const sortedPaths = childPaths ? childPaths.slice().sort((a, b) => a.order.localeCompare(b.order)) : []; const sortedMarkdowns = markdowns ? markdowns.filter(md => md.title !== "index").sort((a, b) => a.order.localeCompare(b.order)) : []; if(childError || markdownError){ return
) : (
)}
Loading...
} {sortedPaths.map((child) => (