mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36: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 (
|
||||
<NodeContextProvider node={node} isLoading={isLoading}>
|
||||
<NodeRelatedProvider id={parseInt(id, 10)}>
|
||||
<NodeRelatedProvider id={parseInt(id, 10)} tags={node.tags}>
|
||||
<CommentContextProvider
|
||||
comments={comments}
|
||||
count={commentsCount}
|
||||
|
|
|
@ -4,12 +4,15 @@ import { AxiosResponse } from 'axios';
|
|||
import { ApiGetNodeRelatedResult } from '~/redux/node/types';
|
||||
import { API } from '~/constants/api';
|
||||
import { api } from '~/utils/api';
|
||||
import { useCallback } from 'react';
|
||||
|
||||
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.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 { INode } from '~/redux/types';
|
||||
import React, { FC, useEffect } from 'react';
|
||||
import { INode, ITag } from '~/redux/types';
|
||||
import { NodeRelatedContextProvider } from '~/utils/context/NodeRelatedContextProvider';
|
||||
import { INodeRelated } from '~/redux/node/types';
|
||||
import { useGetNodeRelated } from '~/utils/hooks/data/useGetNodeRelated';
|
||||
|
||||
interface NodeRelatedProviderProps {
|
||||
id: INode['id'];
|
||||
tags?: ITag[];
|
||||
}
|
||||
|
||||
const defaultValue: INodeRelated = {
|
||||
|
@ -13,8 +14,16 @@ const defaultValue: INodeRelated = {
|
|||
similar: [],
|
||||
};
|
||||
|
||||
const NodeRelatedProvider: FC<NodeRelatedProviderProps> = ({ id, children }) => {
|
||||
const { related, isLoading } = useGetNodeRelated(id);
|
||||
const NodeRelatedProvider: FC<NodeRelatedProviderProps> = ({ id, children, tags }) => {
|
||||
const { related, isLoading, refresh } = useGetNodeRelated(id);
|
||||
|
||||
useEffect(
|
||||
() => {
|
||||
refresh();
|
||||
},
|
||||
// eslint-disable-next-line
|
||||
[tags]
|
||||
);
|
||||
|
||||
return (
|
||||
<NodeRelatedContextProvider related={related || defaultValue} isLoading={isLoading}>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue