Skip to content

Commit

Permalink
add OVERWRITE download state for checking if file overwrite ok
Browse files Browse the repository at this point in the history
  • Loading branch information
jojortz committed Oct 5, 2023
1 parent 07e2ef8 commit b188ea1
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions pykoi/frontend/src/lib/Chatbots/Components/DownloadModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
FILE_INPUT: 0,
DOWNLOADED: 1,
FAILED_DOWNLOAD: 2,
OVERWRITE: 3,
};
let downloadState = DOWNLOAD_STATE.FILE_INPUT;
const handleSubmit = async (e) => {
e.preventDefault();
const saveToCSV = async (file_name) => {
const request_body = {
file_name,
};
Expand All @@ -34,6 +34,19 @@
}
};
const handleSubmit = async (e) => {
e.preventDefault();
const response = await fetch(
`/file_exists/?file_name=${file_name}.csv`
);
const file_exists_response = await response.json();
if (file_exists_response.file_exists === true) {
downloadState = DOWNLOAD_STATE.OVERWRITE;
} else {
saveToCSV(file_name);
}
};
function handleClose() {
showModal = false;
downloadState = DOWNLOAD_STATE.FILE_INPUT;
Expand Down Expand Up @@ -62,7 +75,7 @@
{/if}
{#if downloadState === DOWNLOAD_STATE.DOWNLOADED}
<div>
✅ Data downloaded to ~/pykoi/{file_name}.csv
✅ Data downloaded to /pykoi/{file_name}.csv
</div>
<div class="btn-container">
<button on:click={handleClose}>Close</button>
Expand All @@ -71,10 +84,19 @@
{#if downloadState === DOWNLOAD_STATE.FAILED_DOWNLOAD}
<div>⚠️ Download failed. Please try again.</div>
<div class="btn-container">
<button on:click={() => downloadState = DOWNLOAD_STATE.FILE_INPUT}>Retry</button>
<button on:click={() => (downloadState = DOWNLOAD_STATE.FILE_INPUT)}>Retry</button>
<button on:click={handleClose}>Close</button>
</div>
{/if}
{#if downloadState === DOWNLOAD_STATE.OVERWRITE}
<div>
⚠️ {file_name}.csv already exists. Do you wish to overwrite it?
</div>
<div class="btn-container">
<button on:click={() => (downloadState = DOWNLOAD_STATE.FILE_INPUT)}>Back</button>
<button on:click={() => saveToCSV(file_name)}>Overwrite</button>
</div>
{/if}
</Modal>

<style>
Expand Down

0 comments on commit b188ea1

Please sign in to comment.