Skip to content

Commit

Permalink
download document
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume_Bernier committed Apr 15, 2024
1 parent d8e3cc2 commit 261a3c6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Release v0.1
- [ ] Edit
- [x] Delete
- [x] Upload document
- [ ] Download document
- [x] Download document
- [ ] Date filter
### Transaction label
- [ ] Create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@
export let row: Transactions;
const transaction = transactionSchema.parse(row);
async function openDocument(e: Event) {
e.preventDefault();
const formData = new FormData();
formData.append('download', transaction.id);
try {
const response = await fetch('?/downloadDocument', {
method: 'POST',
body: formData
});
const result = await response.json();
const dataObject = JSON.parse(result.data);
const url = dataObject[3];
if (url) {
window.open(url, '_blank');
} else {
console.error('URL not found in the response:', result);
}
} catch (error) {
console.error('Failed to fetch:', error);
}
}
</script>

<DropdownMenu.Root>
Expand All @@ -22,7 +46,12 @@
</DropdownMenu.Trigger>
<DropdownMenu.Content class="w-[160px]" align="end">
<DropdownMenu.Item>Edit</DropdownMenu.Item>
<DropdownMenu.Item>Download</DropdownMenu.Item>
<form method="POST" action="?/downloadDocument" on:submit={openDocument}>
<input hidden value={transaction.id} name="download" />
<button type="submit" class="w-full">
<DropdownMenu.Item>Download</DropdownMenu.Item>
</button>
</form>
<DropdownMenu.Separator />
<DropdownMenu.Sub>
<DropdownMenu.SubTrigger>Labels</DropdownMenu.SubTrigger>
Expand Down
27 changes: 27 additions & 0 deletions app/src/routes/(app)/admin/transactions/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,31 @@ export const actions: Actions = {
}
throw redirect(303, "/admin/transactions");
},
downloadDocument: async ({ request, locals }) => {
const formData = await request.formData();
const transactionId = formData.get('download');

if (typeof transactionId !== 'string' || !transactionId) {
return fail(400, { error: 'A valid transaction ID is required.' });
}

try {
// Check if authStore.model is not null
if (!locals.pb.authStore.model) {
console.log('No authenticated user found.');
return fail(401, { message: 'No authenticated user found.' });
}

const fileToken = await locals.pb.files.getToken();
const record = await locals.pb.collection('transactions').getOne(transactionId);
const url = locals.pb.files.getUrl(record, record.document, { 'token': fileToken });

return {
status: 200,
body: { url }
};
} catch (error) {
console.error("Failed to download the transaction", error);
}
}
}
18 changes: 18 additions & 0 deletions backend/pb_migrations/1713140419_updated_transactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path="../pb_data/types.d.ts" />
migrate((db) => {
const dao = new Dao(db)
const collection = dao.findCollectionByNameOrId("rybgkg359i2537w")

collection.viewRule = ""
collection.updateRule = ""

return dao.saveCollection(collection)
}, (db) => {
const dao = new Dao(db)
const collection = dao.findCollectionByNameOrId("rybgkg359i2537w")

collection.viewRule = null
collection.updateRule = null

return dao.saveCollection(collection)
})

0 comments on commit 261a3c6

Please sign in to comment.