Skip to content

Commit 43922c7

Browse files
refactor: update Outputdialog with new loading pattern
1 parent d21c985 commit 43922c7

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/web/src/__tests__/components/WSEditorCommandContent.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ describe("WSEditorCommandContent", () => {
147147
loadingMessage: "Generating examples from OpenAPI...",
148148
fn: vi.fn().mockResolvedValue([]),
149149
};
150-
vi.mocked(commandApi).updateCommandOutputs.mockResolvedValue(mockCommand);
150+
vi.mocked(commandApi).updateCommandOutputs = {
151+
loadingMessage: "Updating command outputs...",
152+
fn: vi.fn().mockResolvedValue(mockCommand),
153+
};
151154
});
152155

153156
describe("Core Rendering", () => {

src/web/src/services/commandApi.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ export const commandApi = {
5757
await axios.post(resourceUrl, data);
5858
},
5959

60-
updateCommandOutputs: async (leafUrl: string, outputs: any[]): Promise<any> => {
61-
const res = await axios.patch(leafUrl, { outputs });
62-
return res.data;
60+
updateCommandOutputs: {
61+
loadingMessage: "Updating command outputs...",
62+
fn: async (leafUrl: string, outputs: any[]): Promise<any> => {
63+
const res = await axios.patch(leafUrl, { outputs });
64+
return res.data;
65+
},
6366
},
6467

6568
updateCommandArgument: async (argumentUrl: string, data: any): Promise<void> => {

src/web/src/views/workspace/components/WSEditorCommandContent/OutputDialog.tsx

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import {
99
DialogTitle,
1010
FormControlLabel,
1111
FormLabel,
12-
LinearProgress,
1312
Switch,
1413
Typography,
1514
TypographyProps,
1615
FormLabelProps,
1716
} from "@mui/material";
1817
import { styled } from "@mui/material";
1918
import { commandApi, errorHandlerApi } from "../../../../services";
19+
import { useAsyncOperation } from "../../../../services/hooks";
20+
import { AsyncOperationBanner } from "../../../../components";
2021
import { DecodeResponseCommand } from "../../utils/decodeResponseCommand";
2122

2223
interface ObjectOutput {
@@ -81,7 +82,7 @@ interface OutputDialogProps {
8182
}
8283

8384
const OutputDialog: React.FC<OutputDialogProps> = (props) => {
84-
const [updating, setUpdating] = useState<boolean>(false);
85+
const updateOutputsOperation = useAsyncOperation(commandApi.updateCommandOutputs);
8586
const [invalidText, setInvalidText] = useState<string | undefined>(undefined);
8687
const outputs = props.command.outputs ?? [];
8788
const output = outputs[props.idx!];
@@ -95,7 +96,6 @@ const OutputDialog: React.FC<OutputDialogProps> = (props) => {
9596

9697
const handleUpdateOutput = async () => {
9798
setInvalidText(undefined);
98-
setUpdating(true);
9999

100100
if (isObjectOutput(output) || isArrayOutput(output)) {
101101
let commandNames = props.command.names;
@@ -104,22 +104,17 @@ const OutputDialog: React.FC<OutputDialogProps> = (props) => {
104104
commandNames.slice(0, -1).join("/") +
105105
"/Leaves/" +
106106
commandNames[commandNames.length - 1];
107-
console.log("Original clientFlatten: ");
108-
console.log(output.clientFlatten);
107+
109108
output.clientFlatten = !output.clientFlatten;
110-
console.log("New clientFlatten: ");
111-
console.log(output.clientFlatten);
112109

113110
try {
114-
const responseData = await commandApi.updateCommandOutputs(leafUrl, outputs);
111+
const responseData = await updateOutputsOperation.execute(leafUrl, outputs);
115112
const cmd = DecodeResponseCommand(responseData);
116-
setUpdating(false);
117113
props.onClose(cmd);
118114
} catch (err: any) {
119115
console.error(err);
120116
const message = errorHandlerApi.getErrorMessage(err);
121117
setInvalidText(`ResponseError: ${message}`);
122-
setUpdating(false);
123118
}
124119
} else {
125120
console.error(`Invalid output type for flatten switch: ${output.type}`);
@@ -177,13 +172,11 @@ const OutputDialog: React.FC<OutputDialogProps> = (props) => {
177172
)}
178173
</DialogContent>
179174
<DialogActions>
180-
{updating && (
181-
<Box sx={{ width: "100%" }}>
182-
<LinearProgress color="secondary" />
183-
</Box>
184-
)}
185-
{!updating && <Button onClick={handleClose}>Cancel</Button>}
186-
<Button onClick={handleUpdateOutput}>Update</Button>
175+
<AsyncOperationBanner operation={updateOutputsOperation} />
176+
{!updateOutputsOperation.loading && <Button onClick={handleClose}>Cancel</Button>}
177+
<Button onClick={handleUpdateOutput} disabled={updateOutputsOperation.loading}>
178+
Update
179+
</Button>
187180
</DialogActions>
188181
</Dialog>
189182
);

0 commit comments

Comments
 (0)