Skip to content

Commit 6cee325

Browse files
committed
feat: a param to always refetch nodes data
1 parent f999d56 commit 6cee325

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

src/components/NavigationTree/NavigationTree.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export function NavigationTree({
1313
getActions,
1414
activePath,
1515
onActivePathUpdate,
16+
cache,
1617
}: NavigationTreeProps) {
1718
const [state, dispatch] = React.useReducer(reducer, {
1819
[partialRootState.path]: {
@@ -39,6 +40,7 @@ export function NavigationTree({
3940
activePath={activePath}
4041
onItemActivate={onActivePathUpdate}
4142
getActions={getActions}
43+
cache={cache}
4244
/>
4345
</NavigationTreeNode>
4446
);

src/components/NavigationTree/NavigationTreeDirectory.tsx

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface NavigationTreeDirectoryProps {
1515
activePath?: string;
1616
onItemActivate?: (itemPath: string) => void;
1717
getActions?: NavigationTreeProps['getActions'];
18+
cache?: boolean;
1819
}
1920

2021
export function NavigationTreeDirectory({
@@ -25,30 +26,33 @@ export function NavigationTreeDirectory({
2526
activePath,
2627
onItemActivate,
2728
getActions,
29+
cache = true,
2830
}: NavigationTreeDirectoryProps) {
2931
const nodeState = state[path];
3032

3133
React.useEffect(() => {
32-
if (!nodeState.loaded && !nodeState.loading) {
33-
dispatch({
34-
type: NavigationTreeActionType.StartLoading,
35-
payload: {path},
36-
});
34+
if ((nodeState.loaded && cache) || nodeState.loading) {
35+
return;
36+
}
37+
38+
dispatch({
39+
type: NavigationTreeActionType.StartLoading,
40+
payload: {path},
41+
});
3742

38-
fetchPath(path)
39-
.then((data) => {
40-
dispatch({
41-
type: NavigationTreeActionType.FinishLoading,
42-
payload: {path, data},
43-
});
44-
})
45-
.catch((error) => {
46-
dispatch({
47-
type: NavigationTreeActionType.FinishLoading,
48-
payload: {path, error},
49-
});
43+
fetchPath(path)
44+
.then((data) => {
45+
dispatch({
46+
type: NavigationTreeActionType.FinishLoading,
47+
payload: {path, data},
5048
});
51-
}
49+
})
50+
.catch((error) => {
51+
dispatch({
52+
type: NavigationTreeActionType.FinishLoading,
53+
payload: {path, error},
54+
});
55+
});
5256
}, []);
5357

5458
if (nodeState.loading) {
@@ -76,6 +80,7 @@ export function NavigationTreeDirectory({
7680
activePath={activePath}
7781
onItemActivate={onItemActivate}
7882
getActions={getActions}
83+
cache={cache}
7984
/>
8085
);
8186
}

src/components/NavigationTree/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export function reducer(state: NavigationTreeState = {}, action: NavigationTreeA
3939
loading: true,
4040
loaded: false,
4141
error: false,
42+
children: [],
4243
},
4344
};
4445
case NavigationTreeActionType.FinishLoading: {

src/components/NavigationTree/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ export interface NavigationTreeProps<D = any> {
2828
getActions?: (path: string, type: NavigationTreeNodeType) => DropdownMenuItemMixed<D>[];
2929
activePath?: string;
3030
onActivePathUpdate?: (activePath: string) => void;
31+
cache?: boolean;
3132
}

0 commit comments

Comments
 (0)