From 75d083f11f5bc4443b60267a3f11a99da45d1f6f Mon Sep 17 00:00:00 2001
From: hzhang
Date: Sun, 29 Dec 2024 18:52:39 +0000
Subject: [PATCH] add: order paths & mds
---
src/components/Footer.js | 2 +-
src/components/Navigations/PathNode.js | 87 +++++++++++++++++---
src/components/Navigations/SideNavigation.js | 5 +-
src/utils/markdown-queries.js | 18 +++-
src/utils/path-queries.js | 20 ++++-
webpack.config.js | 1 -
6 files changed, 116 insertions(+), 17 deletions(-)
diff --git a/src/components/Footer.js b/src/components/Footer.js
index b26df9c..8e04aea 100644
--- a/src/components/Footer.js
+++ b/src/components/Footer.js
@@ -32,7 +32,7 @@ const Footer = () => {
git
- v0.0.6
+ v0.0.7
)}
{
diff --git a/src/components/Navigations/PathNode.js b/src/components/Navigations/PathNode.js
index bcb5fb7..faa9f81 100644
--- a/src/components/Navigations/PathNode.js
+++ b/src/components/Navigations/PathNode.js
@@ -2,8 +2,8 @@ import React, {useState} from "react";
import { Link } from "react-router-dom";
import PermissionGuard from "../PermissionGuard";
import "./PathNode.css";
-import {useDeletePath, usePaths, useUpdatePath} from "../../utils/path-queries";
-import {useIndexMarkdown, useMarkdownsByPath} from "../../utils/markdown-queries";
+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);
@@ -17,6 +17,8 @@ const PathNode = ({ path, isRoot = false }) => {
const {data: indexMarkdown, isLoading: isIndexLoading, error: indexMarkdownError} = useIndexMarkdown(path.id);
+ const movePath = useMovePath();
+ const moveMarkdown = useMoveMarkdown();
const toggleExpand = () => {
setIsExpanded(!isExpanded);
@@ -40,18 +42,34 @@ const PathNode = ({ path, isRoot = false }) => {
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 Error...;
}
-
return (
-
+
{isEditing ? (
-
{
) : (
-
+
{
indexMarkdown ? (
@@ -112,25 +133,67 @@ const PathNode = ({ path, isRoot = false }) => {
+
+
+
+
{isExpanded && (
- { isChildLoading && Loading...
}
- { childPaths.map((child) => (
+ {isChildLoading && Loading...
}
+ {sortedPaths.map((child) => (
))}
- { markdowns.filter(md => md.title !== "index").map((markdown) => (
+ {sortedMarkdowns.filter(md => md.title !== "index").map((markdown) => (
-
-
- {markdown.title}
-
+
+
+
+ {markdown.title}
+
+
+
+
+
+
+
+
+
+
))}
diff --git a/src/components/Navigations/SideNavigation.js b/src/components/Navigations/SideNavigation.js
index da0a1be..8b77b6f 100644
--- a/src/components/Navigations/SideNavigation.js
+++ b/src/components/Navigations/SideNavigation.js
@@ -9,6 +9,9 @@ const SideNavigation = () => {
const deletePath = useDeletePath();
const updatePath = useUpdatePath();
+ const sortedPaths = paths
+ ? paths.slice().sort((a, b) => a.order.localeCompare(b.order))
+ : [];
const handleDelete = (id) => {
if (window.confirm("Are you sure you want to delete this path?")){
deletePath.mutate(id, {
@@ -47,7 +50,7 @@ const SideNavigation = () => {
{isLoading && Loading...
}
- {paths.map((path) => (
+ {sortedPaths.map((path) => (
{
})
},{
onSuccess: (res, variables) => {
- queryClient.invalidateQueries(["markdownsByPath", variables.data.parent_id]);
+ queryClient.invalidateQueries(["markdownsByPath", variables.data.path_id]);
queryClient.invalidateQueries(["markdown", variables.data.id]);
},
});
};
+export const useMoveMarkdown = () => {
+ const queryClient = useQueryClient();
+ const config = useConfig();
+
+ return useMutation(
+ ({markdown, direction}) => {
+ const apiEndpoint = `${config.BACKEND_HOST}/api/markdown/move_${direction}/${markdown.id}`;
+ return fetch_(apiEndpoint, {method: "PATCH"});
+ },
+ {
+ onSuccess: () => {
+ queryClient.invalidateQueries("paths");
+ }
+ }
+ );
+};
diff --git a/src/utils/path-queries.js b/src/utils/path-queries.js
index 1d729ee..512d35f 100644
--- a/src/utils/path-queries.js
+++ b/src/utils/path-queries.js
@@ -92,4 +92,22 @@ export const useDeletePath = () => {
},
}
);
-};
\ No newline at end of file
+};
+
+
+export const useMovePath = () => {
+ const queryClient = useQueryClient();
+ const config = useConfig();
+
+ return useMutation(
+ ({path, direction}) => {
+ const apiEndpoint = `${config.BACKEND_HOST}/api/path/move_${direction}/${path.id}`;
+ return fetch_(apiEndpoint, {method: "PATCH"});
+ },
+ {
+ onSuccess: () => {
+ queryClient.invalidateQueries("paths");
+ }
+ }
+ );
+};
diff --git a/webpack.config.js b/webpack.config.js
index b1aa53d..8d53579 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -1,6 +1,5 @@
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
-const Dotenv = require('dotenv-webpack');
module.exports = {
entry: './src/index.js',