Skip to content

Commit

Permalink
feat: delete file feature added to files list
Browse files Browse the repository at this point in the history
  • Loading branch information
srijitcoder committed Oct 23, 2024
1 parent d7b53ef commit 64ce5e5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/api/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export async function filesListFromSession(
cache,
) {
try {
await new Promise((resolve) => setTimeout(resolve, 1000));

const response = await octokit.rest.pulls.listFiles({
owner: githubConfig.username,
repo: githubConfig.repo,
Expand All @@ -32,3 +34,35 @@ export async function filesListFromSession(
return error;
}
}

export async function deleteFile(
octokit,
githubConfig,
owner,
repo,
path,
message,
sha,
ref,
) {
try {
await octokit.rest.repos.deleteFile({
owner,
repo,
path,
message,
sha,
branch: ref,
});

return {
text: `Successfully Deleted ${path}`,
status: "success",
};
} catch (error) {
return {
text: error.message,
status: "error",
};
}
}
16 changes: 15 additions & 1 deletion src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
sessionDetails,
} from "@/api/session";
import useOctokitStore from "@/stores/octokit";
import { filesListFromSession } from "@/api/file";
import { deleteFile, filesListFromSession } from "@/api/file";

export async function initOctokit() {
try {
Expand Down Expand Up @@ -89,3 +89,17 @@ export async function getFilesListFromSession(sessionNumber, currPage, cache) {
cache,
);
}

export async function deleteFileBySHA(owner, repo, path, message, sha, ref) {
const { githubConfig, octokit } = useOctokitStore();
return deleteFile(
octokit,
githubConfig,
owner,
repo,
path,
message,
sha,
ref,
);
}
49 changes: 48 additions & 1 deletion src/views/SessionView.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<script setup>
import { inject, onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { getFilesListFromSession, getSessionDetails } from "@/api/index.js";
import {
deleteFileBySHA,
getFilesListFromSession,
getSessionDetails,
} from "@/api/index.js";
import {
queryFilesListMethod,
querySessionDetailsMethod,
} from "@/methods/session-view/index.js";
import OctIcon from "@/components/global/OctIcon.vue";
import Tooltip from "@/components/global/Tooltip.vue";
import { useLoader } from "@/helpers/index.js";
const route = useRoute();
const router = useRouter();
Expand All @@ -16,6 +21,7 @@ const sessionNumber = route.params.sessionNumber;
const session = ref(null);
const fileChangesList = ref(null);
const snackbar = ref(false);
const loader = ref({});
const totalPage = ref(0);
const deleteFile = ref(false);
const page = ref(route.query.page ? parseInt(route.query.page, 10) : 1);
Expand Down Expand Up @@ -55,6 +61,31 @@ const onPageChange = async (newPage) => {
await router.push({ query: { ...route.query, page: newPage } });
await updateSessionDetails(true);
};
const deleteFileHandle = async () => {
if (deleteFile.value) {
loader.value = useLoader().show();
const owner = session.value.head.repo.owner.login;
const repo = session.value.head.repo.name;
const path = deleteFile.value.title;
const message = `Deleting ${path} file from the pull request`;
const sha = deleteFile.value.sha;
const ref = session.value.head.ref;
snackbar.value = await deleteFileBySHA(
owner,
repo,
path,
message,
sha,
ref,
);
deleteFile.value = false;
loader.value.hide();
await updateSessionDetails();
}
};
</script>

<template>
Expand Down Expand Up @@ -181,6 +212,22 @@ const onPageChange = async (newPage) => {
></v-pagination>
</div>

<v-dialog v-model="deleteFile" width="auto">
<v-card max-width="400" prepend-icon="mdi-alert" title="Delete Session">
<template v-slot:text>
Are you sure you want to delete the file:
<strong>{{ deleteFile.title }}</strong>
</template>
<template v-slot:actions>
<v-spacer></v-spacer>
<v-btn @click="deleteFile = false"> Cancel </v-btn>
<v-btn color="red" variant="flat" @click="deleteFileHandle">
Delete
</v-btn>
</template>
</v-card>
</v-dialog>

<v-snackbar
v-model="snackbar"
timeout="3000"
Expand Down

0 comments on commit 64ce5e5

Please sign in to comment.