mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
updating related on tags changed
This commit is contained in:
parent
96f1529f2b
commit
1dcf0753ea
3 changed files with 19 additions and 7 deletions
|
@ -41,7 +41,7 @@ const NodePage: FC<Props> = ({
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NodeContextProvider node={node} isLoading={isLoading}>
|
<NodeContextProvider node={node} isLoading={isLoading}>
|
||||||
<NodeRelatedProvider id={parseInt(id, 10)}>
|
<NodeRelatedProvider id={parseInt(id, 10)} tags={node.tags}>
|
||||||
<CommentContextProvider
|
<CommentContextProvider
|
||||||
comments={comments}
|
comments={comments}
|
||||||
count={commentsCount}
|
count={commentsCount}
|
||||||
|
|
|
@ -4,12 +4,15 @@ import { AxiosResponse } from 'axios';
|
||||||
import { ApiGetNodeRelatedResult } from '~/redux/node/types';
|
import { ApiGetNodeRelatedResult } from '~/redux/node/types';
|
||||||
import { API } from '~/constants/api';
|
import { API } from '~/constants/api';
|
||||||
import { api } from '~/utils/api';
|
import { api } from '~/utils/api';
|
||||||
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
export const useGetNodeRelated = (id?: INode['id']) => {
|
export const useGetNodeRelated = (id?: INode['id']) => {
|
||||||
const { data, isValidating: isLoading } = useSWR<AxiosResponse<ApiGetNodeRelatedResult>>(
|
const { data, isValidating: isLoading, mutate } = useSWR<AxiosResponse<ApiGetNodeRelatedResult>>(
|
||||||
API.NODE.RELATED(id),
|
API.NODE.RELATED(id),
|
||||||
api.get
|
api.get
|
||||||
);
|
);
|
||||||
|
|
||||||
return { related: data?.data.related, isLoading };
|
const refresh = useCallback(() => mutate(data, true), [data, mutate]);
|
||||||
|
|
||||||
|
return { related: data?.data.related, isLoading, refresh };
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import React, { FC } from 'react';
|
import React, { FC, useEffect } from 'react';
|
||||||
import { INode } from '~/redux/types';
|
import { INode, ITag } from '~/redux/types';
|
||||||
import { NodeRelatedContextProvider } from '~/utils/context/NodeRelatedContextProvider';
|
import { NodeRelatedContextProvider } from '~/utils/context/NodeRelatedContextProvider';
|
||||||
import { INodeRelated } from '~/redux/node/types';
|
import { INodeRelated } from '~/redux/node/types';
|
||||||
import { useGetNodeRelated } from '~/utils/hooks/data/useGetNodeRelated';
|
import { useGetNodeRelated } from '~/utils/hooks/data/useGetNodeRelated';
|
||||||
|
|
||||||
interface NodeRelatedProviderProps {
|
interface NodeRelatedProviderProps {
|
||||||
id: INode['id'];
|
id: INode['id'];
|
||||||
|
tags?: ITag[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultValue: INodeRelated = {
|
const defaultValue: INodeRelated = {
|
||||||
|
@ -13,8 +14,16 @@ const defaultValue: INodeRelated = {
|
||||||
similar: [],
|
similar: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
const NodeRelatedProvider: FC<NodeRelatedProviderProps> = ({ id, children }) => {
|
const NodeRelatedProvider: FC<NodeRelatedProviderProps> = ({ id, children, tags }) => {
|
||||||
const { related, isLoading } = useGetNodeRelated(id);
|
const { related, isLoading, refresh } = useGetNodeRelated(id);
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() => {
|
||||||
|
refresh();
|
||||||
|
},
|
||||||
|
// eslint-disable-next-line
|
||||||
|
[tags]
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NodeRelatedContextProvider related={related || defaultValue} isLoading={isLoading}>
|
<NodeRelatedContextProvider related={related || defaultValue} isLoading={isLoading}>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue