Skip to content

Commit

Permalink
feat: save file
Browse files Browse the repository at this point in the history
  • Loading branch information
lancatlin committed Mar 10, 2024
1 parent 8c47a67 commit 51c4d74
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 16 additions & 5 deletions app/src/components/edit/EditForm.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { ref, watch } from "vue";
import { getFileContent } from "../../utils/api";
import { getFileContent, saveFile } from "../../utils/api";
import { useToast } from "vue-toast-notification";
const toast = useToast();
Expand All @@ -25,17 +25,28 @@ watch(
);
async function reset() {
code.value = await getFileContent(props.filePath);
fileChanged.value = false;
try {
code.value = await getFileContent(props.filePath);
fileChanged.value = false;
} catch (err) {
toast.error("Failed to open file");
console.error(err);
}
}
function onChange() {
fileChanged.value = true;
}
async function save() {
toast.success("File saved!");
fileChanged.value = false;
try {
await saveFile(props.filePath, code.value);
toast.success("File saved!");
fileChanged.value = false;
} catch (err) {
console.error(err);
toast.error("Failed to save file");
}
}
</script>
<template>
Expand Down
4 changes: 4 additions & 0 deletions app/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ export async function getFileContent(path: string): Promise<string> {
});
return response.data;
}

export async function saveFile(path: string, content: string): Promise<void> {
await api.post(`/files/${path}`, { data: content });
}

0 comments on commit 51c4d74

Please sign in to comment.