-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
76 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 |
---|---|---|
@@ -1,47 +1,67 @@ | ||
import {useDisclosure} from "@mantine/hooks" | ||
import {useContext} from "react" | ||
import {Tooltip, ActionIcon} from "@mantine/core" | ||
import {IconEdit} from "@tabler/icons-react" | ||
|
||
import {useSelector, useDispatch} from "react-redux" | ||
import { | ||
selectSelectedNodes, | ||
nodeUpdated, | ||
selectSelectedNodeIds, | ||
clearNodesSelection | ||
} from "@/slices/dualPanel/dualPanel" | ||
import edit_node_title from "@/components/modals/EditNodeTitle" | ||
import {EditNodeTitleModal} from "@/components/modals/EditNodeTitle" | ||
|
||
import type {RootState} from "@/app/types" | ||
|
||
import type {NodeType, PanelMode} from "@/types" | ||
import type {PanelMode, NodeType} from "@/types" | ||
|
||
import PanelContext from "@/contexts/PanelContext" | ||
import {selectNodesByIds} from "@/features/nodes/nodesSlice" | ||
|
||
export default function EditNodeTitleButton() { | ||
const [opened, {open, close}] = useDisclosure(false) | ||
const mode: PanelMode = useContext(PanelContext) | ||
const dispatch = useDispatch() | ||
const selectedIds = useSelector((state: RootState) => | ||
selectSelectedNodeIds(state, mode) | ||
) | ||
const selectedNodes = useSelector((state: RootState) => | ||
selectSelectedNodes(state, mode) | ||
selectNodesByIds(state, selectedIds || []) | ||
) | ||
const onEditNodeTitle = () => { | ||
|
||
const dispatch = useDispatch() | ||
let node: NodeType = selectedNodes[0] | ||
|
||
const onClick = () => { | ||
if (selectedNodes.length < 1) { | ||
console.log("Error: no selected nodes") | ||
return | ||
} | ||
node = selectedNodes[0] | ||
open() | ||
} | ||
|
||
let node: NodeType = selectedNodes[0] | ||
const onSubmit = () => { | ||
dispatch(clearNodesSelection(mode)) | ||
close() | ||
} | ||
|
||
edit_node_title(node) | ||
.then((node: NodeType) => { | ||
dispatch(nodeUpdated({node, mode})) | ||
}) | ||
.finally(() => dispatch(clearNodesSelection(mode))) | ||
const onCancel = () => { | ||
dispatch(clearNodesSelection(mode)) | ||
close() | ||
} | ||
|
||
return ( | ||
<Tooltip label="Change title" withArrow> | ||
<ActionIcon size={"lg"} variant="default" onClick={onEditNodeTitle}> | ||
<IconEdit stroke={1.4} /> | ||
</ActionIcon> | ||
</Tooltip> | ||
<> | ||
<Tooltip label="Change title" withArrow> | ||
<ActionIcon size={"lg"} variant="default" onClick={onClick}> | ||
<IconEdit stroke={1.4} /> | ||
</ActionIcon> | ||
</Tooltip> | ||
<EditNodeTitleModal | ||
opened={opened} | ||
node={node} | ||
onSubmit={onSubmit} | ||
onCancel={onCancel} | ||
/> | ||
</> | ||
) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,45 @@ | ||
import { | ||
createSelector, | ||
createEntityAdapter, | ||
createSlice, | ||
PayloadAction | ||
} from "@reduxjs/toolkit" | ||
import {RootState} from "@/app/types" | ||
import {apiSliceWithNodes} from "./apiSlice" | ||
import type {NodeType, Paginated} from "@/types" | ||
|
||
export const selectFolderResult = (folderID: string) => | ||
const nodeAdapter = createEntityAdapter<NodeType>() | ||
const initialState = nodeAdapter.getInitialState() | ||
|
||
const nodesSlice = createSlice({ | ||
name: "nodes", | ||
initialState, | ||
reducers: {}, | ||
extraReducers(builder) { | ||
builder.addMatcher( | ||
apiSliceWithNodes.endpoints.getPaginatedNodes.matchFulfilled, | ||
(state, action: PayloadAction<Paginated<NodeType>>) => { | ||
const payload = action.payload | ||
nodeAdapter.upsertMany(state, payload.items) | ||
} | ||
) | ||
} | ||
}) | ||
|
||
export default nodesSlice.reducer | ||
|
||
export const selectFoldersResult = (folderID: string) => | ||
apiSliceWithNodes.endpoints.getFolder.select(folderID) | ||
|
||
export const selectItemIds = (_: RootState, itemIds: string[]) => itemIds | ||
|
||
export const {selectEntities: selectNodeEntities} = nodeAdapter.getSelectors( | ||
(state: RootState) => state.nodes | ||
) | ||
|
||
export const selectNodesByIds = createSelector( | ||
[selectNodeEntities, selectItemIds], | ||
(entities, ids) => { | ||
return Object.values(entities).filter(i => ids.includes(i.id)) | ||
} | ||
) |