mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
added node related provider
This commit is contained in:
parent
3090f3a306
commit
d202a8cb18
6 changed files with 67 additions and 26 deletions
|
@ -1,18 +1,35 @@
|
|||
import { INodeRelated } from '~/redux/node/types';
|
||||
import React, { createContext, FC, useContext } from 'react';
|
||||
import React, { FC } from 'react';
|
||||
import { INode } from '~/redux/types';
|
||||
import { NodeRelatedContextProvider } from '~/utils/context/NodeRelatedContextProvider';
|
||||
import { ApiGetNodeRelatedResult, INodeRelated } from '~/redux/node/types';
|
||||
import useSWR from 'swr';
|
||||
import { API } from '~/constants/api';
|
||||
import { api } from '~/utils/api';
|
||||
import { AxiosResponse } from 'axios';
|
||||
|
||||
interface NodeRelatedProviderProps {
|
||||
related: INodeRelated;
|
||||
isLoading: boolean;
|
||||
id: INode['id'];
|
||||
}
|
||||
|
||||
const NodeRelatedContext = createContext<NodeRelatedProviderProps>({
|
||||
related: { albums: {}, similar: [] },
|
||||
isLoading: false,
|
||||
});
|
||||
const defaultValue: INodeRelated = {
|
||||
albums: {},
|
||||
similar: [],
|
||||
};
|
||||
|
||||
export const NodeRelatedProvider: FC<NodeRelatedProviderProps> = ({ children, ...rest }) => (
|
||||
<NodeRelatedContext.Provider value={rest}>{children}</NodeRelatedContext.Provider>
|
||||
);
|
||||
const NodeRelatedProvider: FC<NodeRelatedProviderProps> = ({ id, children }) => {
|
||||
const { data, isValidating } = useSWR<AxiosResponse<ApiGetNodeRelatedResult>>(
|
||||
API.NODE.RELATED(id),
|
||||
api.get
|
||||
);
|
||||
|
||||
export const useNodeRelatedContext = () => useContext<NodeRelatedProviderProps>(NodeRelatedContext);
|
||||
return (
|
||||
<NodeRelatedContextProvider
|
||||
related={data?.data?.related || defaultValue}
|
||||
isLoading={isValidating}
|
||||
>
|
||||
{children}
|
||||
</NodeRelatedContextProvider>
|
||||
);
|
||||
};
|
||||
|
||||
export { NodeRelatedProvider };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue