Skip to content

Commit

Permalink
feat: add real-time updates for children recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
codeofmochi committed Jul 13, 2021
1 parent 0474da5 commit 762d7d4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/components/common/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import Alert from '@material-ui/lab/Alert';
import { useTranslation } from 'react-i18next';
import PropTypes from 'prop-types';
import { hooks } from '../../config/queryClient';
import { hooks, ws } from '../../config/queryClient';
import { ITEM_TYPES } from '../../enums';
import FolderButton from './FolderButton';
import {
Expand All @@ -38,9 +38,11 @@ const Item = ({ id, isChildren }) => {
const { data: item, isLoading, isError } = useItem(id);

// fetch children if item is folder
const isFolder = Boolean(item?.get('type') === ITEM_TYPES.FOLDER);
const { data: children, isLoading: isChildrenLoading } = useChildren(id, {
enabled: Boolean(item?.get('type') === ITEM_TYPES.FOLDER),
enabled: isFolder,
});
ws.hooks.useChildrenUpdates(isFolder ? id : null);

// fetch file content if type is file
const { data: content, isError: isFileError } = useFileContent(id, {
Expand Down
17 changes: 14 additions & 3 deletions src/components/common/MainMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Loader,
} from '@graasp/ui';
import { buildTreeItemClass, MAIN_MENU_ID } from '../../config/selectors';
import { hooks } from '../../config/queryClient';
import { hooks, ws } from '../../config/queryClient';
import { ITEM_TYPES } from '../../enums';

const { useItem, useChildren } = hooks;
Expand All @@ -23,13 +23,18 @@ const MainMenu = () => {
isLoading: rootItemIsLoading,
isError: rootItemIsError,
} = useItem(rootId);

const isFolder = Boolean(
rootItem && rootItem.get('type') === ITEM_TYPES.FOLDER,
);
const {
data: children,
isLoading,
isError: childrenIsError,
} = useChildren(rootId, {
enabled: Boolean(rootItem && rootItem.get('type') === ITEM_TYPES.FOLDER),
enabled: isFolder,
});
ws.hooks.useChildrenUpdates(isFolder ? rootId : null);

// display nothing when no item is defined
if (!rootId) {
Expand All @@ -44,13 +49,19 @@ const MainMenu = () => {
return <Alert severity="error">{t('An unexpected error occured.')}</Alert>;
}

const useChildrenWithUpdates = (childId, ...args) => {
const ret = useChildren(childId, ...args);
ws.hooks.useChildrenUpdates(childId);
return ret;
};

return (
<GraaspMainMenu id={MAIN_MENU_ID}>
<DynamicTreeView
rootLabel={rootItem.get('name')}
rootId={rootId}
useItem={useItem}
useChildren={useChildren}
useChildren={useChildrenWithUpdates}
buildTreeItemClass={(nodeId) => buildTreeItemClass(nodeId)}
initialExpendedItems={[rootId]}
showCheckbox={false}
Expand Down
3 changes: 3 additions & 0 deletions src/config/queryClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ const {
queryClient,
QueryClientProvider,
hooks,
ws,
useMutation,
ReactQueryDevtools,
} = configureQueryClient({
API_HOST,
notifier,
enableWebsocket: true,
});
export {
queryClient,
QueryClientProvider,
hooks,
ws,
useMutation,
ReactQueryDevtools,
};
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,7 @@

"@graasp/query-client@git://github.com/graasp/graasp-query-client.git":
version "0.1.0"
resolved "git://github.com/graasp/graasp-query-client.git#d799b21e9c9348fd9185593e8883feb6592ccb2f"
resolved "git://github.com/graasp/graasp-query-client.git#0fafee61773fe5f451cc3d312e40944b6075d2c9"
dependencies:
http-status-codes "2.1.4"
immutable "4.0.0-rc.12"
Expand Down

0 comments on commit 762d7d4

Please sign in to comment.