Skip to content

Commit

Permalink
Perfectly Delete attachments on parent delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj2212 committed Jun 4, 2021
1 parent ed0fcf8 commit 5aa4bd7
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 63 deletions.
37 changes: 22 additions & 15 deletions components/events-props/confirm-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const ConfirmDelete = ({
handleClose,
modal,
id,
main_event,
attachments,
delArray,
}) => {
Expand All @@ -17,24 +18,30 @@ export const ConfirmDelete = ({
if (attachments.length) {
for (let i = 0; i < attachments.length; i++) {
const element = attachments[i];
if (element.url) deleteArray.push(element.url.split("/")[5]);
}
if (deleteArray.length) {
let result = await fetch("/api/gdrive/deletefiles", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(deleteArray),
});
result = await result.json();
if (result instanceof Error) {
console.log("Error Occured");
}
console.log(result);
if (element.url && element.url.split("/")[5])
deleteArray.push(element.url.split("/")[5]);
}
}
if (main_event && main_event.url && main_event.typeLink == false) {
deleteArray.push(main_event.url.split("/")[5]);
}

console.log(main_event);
console.log(deleteArray);
if (deleteArray.length) {
let result = await fetch("/api/gdrive/deletefiles", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(deleteArray),
});
result = await result.json();
if (result instanceof Error) {
console.log("Error Occured");
}
console.log(result);
}
let result = await fetch("/api/delete/event", {
method: "DELETE",
body: id.toString(),
Expand Down
1 change: 1 addition & 0 deletions components/events-props/edit-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const EditForm = ({ data, handleClose, modal }) => {
id={content.id}
attachments={attachments}
delArray={deleteArray.current}
main_event={content.main_attachment}
/>
<DialogContent>
<TextField
Expand Down
30 changes: 15 additions & 15 deletions components/innovation-props/confirm-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ export const ConfirmDelete = ({
if (attachments.length) {
for (let i = 0; i < attachments.length; i++) {
const element = attachments[i];
if (element.url) deleteArray.push(element.url.split("/")[5]);
if (element.url && element.url.split("/")[5])
deleteArray.push(element.url.split("/")[5]);
}
if (deleteArray.length) {
let result = await fetch("/api/gdrive/deletefiles", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(deleteArray),
});
result = await result.json();
if (result instanceof Error) {
console.log("Error Occured");
}
console.log(result);
}
if (deleteArray.length) {
let result = await fetch("/api/gdrive/deletefiles", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(deleteArray),
});
result = await result.json();
if (result instanceof Error) {
console.log("Error Occured");
}
console.log(result);
}

let result = await fetch("/api/delete/innovation", {
method: "DELETE",
body: id.toString(),
Expand Down
38 changes: 24 additions & 14 deletions components/news-props/confirm-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const ConfirmDelete = ({
modal,
id,
attachments,
add_attach,
delArray,
}) => {
const deleteEvent = async () => {
Expand All @@ -19,20 +20,29 @@ export const ConfirmDelete = ({
const element = attachments[i];
if (element.url) deleteArray.push(element.url.split("/")[5]);
}
if (deleteArray.length) {
let result = await fetch("/api/gdrive/deletefiles", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(deleteArray),
});
result = await result.json();
if (result instanceof Error) {
console.log("Error Occured");
}
console.log(result);
}

if (add_attach.length) {
for (let i = 0; i < add_attach.length; i++) {
const element = add_attach[i];
if (element.url && element.url.split("/")[5])
deleteArray.push(element.url.split("/")[5]);
}
}

if (deleteArray.length) {
let result = await fetch("/api/gdrive/deletefiles", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(deleteArray),
});
result = await result.json();
if (result instanceof Error) {
console.log("Error Occured");
}
console.log(result);
}

let result = await fetch("/api/delete/news", {
Expand Down Expand Up @@ -64,7 +74,7 @@ export const ConfirmDelete = ({
>
Delete
</Button>
<Button onClick={handleClose} color="primary" autoFocus>
<Button onClick={handleClose} color="primary">
Cancel
</Button>
</DialogActions>
Expand Down
1 change: 1 addition & 0 deletions components/news-props/edit-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const EditForm = ({ data, handleClose, modal }) => {
handleClose={handleDelete}
id={content.id}
attachments={image}
add_attach={add_attach}
delArray={deleteArray.current}
/>
<DialogContent>
Expand Down
35 changes: 21 additions & 14 deletions components/notices-props/confirm-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const ConfirmDelete = ({
handleClose,
modal,
id,
main_notice,
attachments,
delArray,
}) => {
Expand All @@ -17,22 +18,28 @@ export const ConfirmDelete = ({
if (attachments.length) {
for (let i = 0; i < attachments.length; i++) {
const element = attachments[i];
if (element.url) deleteArray.push(element.url.split("/")[5]);
if (element.url && element.url.split("/")[5])
deleteArray.push(element.url.split("/")[5]);
}
if (deleteArray.length) {
let result = await fetch("/api/gdrive/deletefiles", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(deleteArray),
});
result = await result.json();
if (result instanceof Error) {
console.log("Error Occured");
}
console.log(result);
}

if (main_notice && main_notice.url && main_notice.typeLink == false) {
deleteArray.push(main_notice.url.split("/")[5]);
}

if (deleteArray.length) {
let result = await fetch("/api/gdrive/deletefiles", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(deleteArray),
});
result = await result.json();
if (result instanceof Error) {
console.log("Error Occured");
}
console.log(result);
}

let result = await fetch("/api/delete/notice", {
Expand Down
1 change: 1 addition & 0 deletions components/notices-props/edit-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const EditForm = ({ data, handleClose, modal }) => {
modal={verifyDelete}
handleClose={handleDelete}
id={content.id}
main_notice={content.main_attachment}
delArray={deleteArray.current}
attachments={attachments}
/>
Expand Down
14 changes: 9 additions & 5 deletions pages/api/gdrive/deletefiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ export default async function DeleteFiles(request, response) {
console.log(files);
try {
for (let idx = 0; idx < files.length; idx++) {
const fileId = files[idx];
const result = await drive.files.delete({
fileId: fileId,
});
console.log(result);
try {
const fileId = files[idx];
const result = await drive.files.delete({
fileId: fileId,
});
console.log(result);
} catch (error) {
console.log(error);
}
}
response.json({ message: "Files Deleted Successfully" });
} catch (error) {
Expand Down

0 comments on commit 5aa4bd7

Please sign in to comment.