Skip to content

Commit

Permalink
feat(operations) auto refresh ops on visiting the ops list and add a …
Browse files Browse the repository at this point in the history
…refresh button

Signed-off-by: David Edler <david.edler@canonical.com>
  • Loading branch information
edlerd committed Apr 15, 2024
1 parent 2d9a11c commit 40117a5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/context/operationsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type OperationsContextType = {
runningOperations: LxdOperation[];
error: Error | null;
isLoading: boolean;
isFetching: boolean;
refetchOperations: (options?: RefetchOptions) => void;
};

Expand All @@ -29,6 +30,7 @@ const OperationsContext = createContext<OperationsContextType>({
runningOperations: [],
error: null,
isLoading: false,
isFetching: false,
refetchOperations: () => null,
});

Expand All @@ -39,6 +41,7 @@ const OperationsProvider: FC<Props> = ({ children }) => {
data: operationList,
error,
isLoading,
isFetching,
refetch,
} = useQuery({
queryKey: [queryKeys.operations],
Expand Down Expand Up @@ -78,6 +81,7 @@ const OperationsProvider: FC<Props> = ({ children }) => {
runningOperations: running,
error,
isLoading,
isFetching,
refetchOperations: debouncedRefetch,
};

Expand Down
6 changes: 5 additions & 1 deletion src/pages/operations/OperationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import OperationInstanceName from "pages/operations/OperationInstanceName";
import NotificationRow from "components/NotificationRow";
import { getProjectName } from "util/operations";
import { useOperations } from "context/operationsProvider";
import RefreshOperationsBtn from "pages/operations/actions/RefreshOperationsBtn";

const OperationList: FC = () => {
const notify = useNotify();
Expand Down Expand Up @@ -122,7 +123,10 @@ const OperationList: FC = () => {

return (
<>
<BaseLayout title="Ongoing operations">
<BaseLayout
title="Ongoing operations"
controls={<RefreshOperationsBtn />}
>
<NotificationRow />
<Row>
{operations.length > 0 && (
Expand Down
30 changes: 30 additions & 0 deletions src/pages/operations/actions/RefreshOperationsBtn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { FC, useEffect } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { queryKeys } from "util/queryKeys";
import { ActionButton, Icon } from "@canonical/react-components";
import { useOperations } from "context/operationsProvider";

const RefreshOperationsBtn: FC = () => {
const { isFetching } = useOperations();
const queryClient = useQueryClient();

const handleRefresh = () => {
void queryClient.invalidateQueries({ queryKey: [queryKeys.operations] });
};

// force a refresh on first render
useEffect(handleRefresh, []);

return (
<ActionButton
className="u-no-margin--bottom has-icon"
onClick={handleRefresh}
loading={isFetching}
>
<Icon name="restart" />
<span>Refresh</span>
</ActionButton>
);
};

export default RefreshOperationsBtn;

0 comments on commit 40117a5

Please sign in to comment.