-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(images) allow bulk select/delete images in a project #618
Signed-off-by: David Edler <[email protected]>
- Loading branch information
Showing
5 changed files
with
202 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import React, { FC, useState } from "react"; | ||
import { deleteImageBulk } from "api/images"; | ||
import { useQueryClient } from "@tanstack/react-query"; | ||
import { queryKeys } from "util/queryKeys"; | ||
import { ConfirmationButton, useNotify } from "@canonical/react-components"; | ||
import { useEventQueue } from "context/eventQueue"; | ||
import { getPromiseSettledCounts } from "util/helpers"; | ||
import { pluralize } from "util/instanceBulkActions"; | ||
|
||
interface Props { | ||
fingerprints: string[]; | ||
project: string; | ||
onStart: () => void; | ||
onFinish: () => void; | ||
} | ||
|
||
const BulkDeleteImageBtn: FC<Props> = ({ | ||
fingerprints, | ||
project, | ||
onStart, | ||
onFinish, | ||
}) => { | ||
const eventQueue = useEventQueue(); | ||
const notify = useNotify(); | ||
const [isLoading, setLoading] = useState(false); | ||
const queryClient = useQueryClient(); | ||
|
||
const count = fingerprints.length; | ||
|
||
const handleDelete = () => { | ||
setLoading(true); | ||
onStart(); | ||
void deleteImageBulk(fingerprints, project, eventQueue).then((results) => { | ||
const { fulfilledCount, rejectedCount } = | ||
getPromiseSettledCounts(results); | ||
if (fulfilledCount === count) { | ||
notify.success( | ||
<> | ||
<b>{fingerprints.length}</b>{" "} | ||
{pluralize("image", fingerprints.length)} deleted. | ||
</>, | ||
); | ||
} else if (rejectedCount === count) { | ||
notify.failure( | ||
"Image bulk deletion failed", | ||
undefined, | ||
<> | ||
<b>{count}</b> {pluralize("image", count)} could not be deleted. | ||
</>, | ||
); | ||
} else { | ||
notify.failure( | ||
"Image bulk deletion partially failed", | ||
undefined, | ||
<> | ||
<b>{fulfilledCount}</b> {pluralize("image", fulfilledCount)}{" "} | ||
deleted. | ||
<br /> | ||
<b>{rejectedCount}</b> {pluralize("image", rejectedCount)} could not | ||
be deleted. | ||
</>, | ||
); | ||
} | ||
void queryClient.invalidateQueries({ | ||
predicate: (query) => query.queryKey[0] === queryKeys.images, | ||
}); | ||
setLoading(false); | ||
onFinish(); | ||
}); | ||
}; | ||
|
||
return ( | ||
<ConfirmationButton | ||
loading={isLoading} | ||
confirmationModalProps={{ | ||
title: "Confirm delete", | ||
children: ( | ||
<p> | ||
This will permanently delete{" "} | ||
<b> | ||
{fingerprints.length} {pluralize("image", fingerprints.length)} | ||
</b> | ||
.<br /> | ||
This action cannot be undone, and can result in data loss. | ||
</p> | ||
), | ||
confirmButtonLabel: "Delete", | ||
onConfirm: handleDelete, | ||
}} | ||
appearance="" | ||
className="bulk-delete-images" | ||
disabled={isLoading} | ||
shiftClickEnabled | ||
showShiftClickHint | ||
> | ||
Delete images | ||
</ConfirmationButton> | ||
); | ||
}; | ||
|
||
export default BulkDeleteImageBtn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters