Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue attachments mutation #743

Merged
merged 3 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/app/components/issues/attachment-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const IssueAttachmentUpload = () => {
})
);
setIsLoading(true);

issuesService
.uploadIssueAttachment(
workspaceSlug as string,
Expand All @@ -46,7 +47,11 @@ export const IssueAttachmentUpload = () => {
formData
)
.then((res) => {
mutate<IIssueAttachment[]>(ISSUE_ATTACHMENTS(issueId as string));
mutate<IIssueAttachment[]>(
ISSUE_ATTACHMENTS(issueId as string),
(prevData) => [res, ...(prevData ?? [])],
false
);
setToastAlert({
type: "success",
title: "Success!",
Expand Down
34 changes: 14 additions & 20 deletions apps/app/components/issues/delete-attachment-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React from "react";

import { useRouter } from "next/router";

Expand Down Expand Up @@ -28,24 +28,23 @@ type Props = {
};

export const DeleteAttachmentModal: React.FC<Props> = ({ isOpen, setIsOpen, data }) => {
const [isDeleteLoading, setIsDeleteLoading] = useState(false);

const router = useRouter();
const { workspaceSlug, projectId, issueId } = router.query;

const { setToastAlert } = useToast();

const handleClose = () => {
setIsOpen(false);
setIsDeleteLoading(false);
};

const handleDeletion = async (assetId: string) => {
setIsDeleteLoading(true);

if (!workspaceSlug || !projectId || !data) return;

mutate<IIssueAttachment[]>(ISSUE_ATTACHMENTS(issueId as string));
mutate<IIssueAttachment[]>(
ISSUE_ATTACHMENTS(issueId as string),
(prevData) => (prevData ?? [])?.filter((p) => p.id !== assetId),
false
);

await issuesService
.deleteIssueAttachment(
Expand All @@ -54,22 +53,12 @@ export const DeleteAttachmentModal: React.FC<Props> = ({ isOpen, setIsOpen, data
issueId as string,
assetId as string
)
.then((res) => {
mutate(ISSUE_ATTACHMENTS(issueId as string));
setToastAlert({
type: "success",
title: "Success!",
message: "File removed successfully.",
});
handleClose();
})
.catch((err) => {
.catch(() => {
setToastAlert({
type: "error",
title: "error!",
message: "Something went wrong please try again.",
});
setIsDeleteLoading(false);
});
};

Expand Down Expand Up @@ -129,8 +118,13 @@ export const DeleteAttachmentModal: React.FC<Props> = ({ isOpen, setIsOpen, data
</div>
<div className="flex justify-end gap-2 bg-gray-50 p-4 sm:px-6">
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
<DangerButton onClick={() => handleDeletion(data.id)} loading={isDeleteLoading}>
{isDeleteLoading ? "Deleting..." : "Delete"}
<DangerButton
onClick={() => {
handleDeletion(data.id);
handleClose();
}}
>
Delete
</DangerButton>
</div>
</Dialog.Panel>
Expand Down