Skip to content

Commit 46e147f

Browse files
committed
- Add asyncbutton to variant edit dialog for handleSave
1 parent 397112e commit 46e147f

File tree

1 file changed

+39
-30
lines changed
  • app/views/ReportView/components/RapidSummary/components/VariantEditDialog

1 file changed

+39
-30
lines changed

app/views/ReportView/components/RapidSummary/components/VariantEditDialog/index.tsx

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
Button,
1212
Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Chip,
1313
} from '@mui/material';
14+
import AsyncButton from '@/components/AsyncButton';
15+
import snackbar from '@/services/SnackbarUtils';
1416
import DeleteIcon from '@mui/icons-material/HighlightOff';
1517
import ReportContext from '@/context/ReportContext';
1618
import ConfirmContext from '@/context/ConfirmContext';
@@ -114,6 +116,7 @@ const VariantEditDialog = ({
114116
const { showConfirmDialog } = useConfirmDialog();
115117
const [data, setData] = useState(editData);
116118
const [editDataDirty, setEditDataDirty] = useState<boolean>(false);
119+
const [isApiCalling, setIsApiCalling] = useState(false);
117120

118121
useEffect(() => {
119122
if (editData) {
@@ -144,41 +147,47 @@ const VariantEditDialog = ({
144147
}, [data?.kbMatches, handleDataChange]);
145148

146149
const handleSave = useCallback(async () => {
150+
setIsApiCalling(true);
147151
if (editDataDirty) {
148-
let variantId = data?.ident;
149-
// The relevance was appeneded to Id due to row concatenation, needs to be removed here to call API
150-
if ((data).potentialClinicalAssociation) {
151-
variantId = variantId.substr(0, variantId.lastIndexOf('-'));
152-
}
152+
try {
153+
let variantId = data?.ident;
154+
// The relevance was appeneded to Id due to row concatenation, needs to be removed here to call API
155+
if ((data).potentialClinicalAssociation) {
156+
variantId = variantId.substr(0, variantId.lastIndexOf('-'));
157+
}
153158

154-
const calls = [];
159+
const calls = [];
155160

156-
if (fields.includes(FIELDS.comments) && data?.comments) {
157-
calls.push(api.put(
158-
`/reports/${report.ident}/${VARIANT_TYPE_TO_API_MAP[data.variantType]}/${variantId}`,
159-
{
160-
comments: data.comments,
161-
},
162-
));
163-
}
161+
if (fields.includes(FIELDS.comments) && data?.comments) {
162+
calls.push(api.put(
163+
`/reports/${report.ident}/${VARIANT_TYPE_TO_API_MAP[data.variantType]}/${variantId}`,
164+
{
165+
comments: data.comments,
166+
},
167+
));
168+
}
164169

165-
if (fields.includes(FIELDS.kbMatches) && data?.kbMatches && editData?.kbMatches) {
166-
const existingIds = [].concat(...editData.kbMatches.map((match) => match.kbMatchedStatements.map(({ ident }) => ident)));
167-
data?.kbMatches.forEach((kbMatch) => {
168-
const remainingIds = new Set(kbMatch.kbMatchedStatements.map(({ ident }) => ident));
169-
existingIds.filter((id) => !remainingIds.has(id)).forEach((stmtId) => {
170-
calls.push(api.del(`/reports/${report.ident}/kb-matches/kb-matched-statements/${stmtId}`, {}));
170+
if (fields.includes(FIELDS.kbMatches) && data?.kbMatches && editData?.kbMatches) {
171+
const existingIds = [].concat(...editData.kbMatches.map((match) => match.kbMatchedStatements.map(({ ident }) => ident)));
172+
data?.kbMatches.forEach((kbMatch) => {
173+
const remainingIds = new Set(kbMatch.kbMatchedStatements.map(({ ident }) => ident));
174+
existingIds.filter((id) => !remainingIds.has(id)).forEach((stmtId) => {
175+
calls.push(api.del(`/reports/${report.ident}/kb-matches/kb-matched-statements/${stmtId}`, {}));
176+
});
171177
});
172-
});
173-
}
178+
}
174179

175-
const callSet = new ApiCallSet(calls);
180+
const callSet = new ApiCallSet(calls);
176181

177-
if (isSigned) {
178-
showConfirmDialog(callSet);
179-
} else {
180-
await callSet.request();
181-
onClose(true);
182+
if (isSigned) {
183+
showConfirmDialog(callSet);
184+
} else {
185+
await callSet.request();
186+
onClose(true);
187+
}
188+
setIsApiCalling(false);
189+
} catch (e) {
190+
snackbar.error(`Error editing variant: ${e.message}`);
182191
}
183192
} else {
184193
onClose(null);
@@ -219,9 +228,9 @@ const VariantEditDialog = ({
219228
<Button onClick={handleDialogClose}>
220229
Close
221230
</Button>
222-
<Button color="secondary" onClick={handleSave}>
231+
<AsyncButton isLoading={isApiCalling} color="secondary" onClick={handleSave}>
223232
Save Changes
224-
</Button>
233+
</AsyncButton>
225234
</DialogActions>
226235
</Dialog>
227236
);

0 commit comments

Comments
 (0)