Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/lib/components/template/TemplateTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,29 @@
function toggleMenu(id) {
activeMenu = activeMenu === id ? null : id;
}

async function exportFormTemplate(templateId: string, templateTitle: string) {
const response = await fetch(`/api/server/template/export`, {
method: 'POST',
body: JSON.stringify({
// sessionId,
templateId: templateId,
}),
headers: { 'content-type': 'application/json' }
});
if (!response.ok) {
throw new Error(`Failed to export care plan: ${response.statusText}`);
}

const filename = `${templateTitle}.json`;
const blob = await response.blob();
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
</script>

<div class="my-4 flex items-center justify-between">
Expand Down Expand Up @@ -401,6 +424,17 @@
</AlertDialog.Content>
</AlertDialog.Root>

<div>
<Button
variant="ghost"
class="w-36 justify-start"
onclick={() => exportFormTemplate(row.id, row.Title)}
>
<Icon icon="lucide:download" width="20" height="20" />
<span>Export</span>
</Button>
</div>

<AlertDialog.Root bind:open>
<AlertDialog.Trigger class="{buttonVariants({ variant: 'ghost' })} w-36 justify-start">
<Icon
Expand Down
24 changes: 24 additions & 0 deletions src/routes/api/server/template/export/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { RequestEvent } from '@sveltejs/kit';
import { exportFormTemplates } from '../../../services/form-template';

//////////////////////////////////////////////////////////////

export const POST = async (event: RequestEvent) => {
const request = event.request;
const data = await request.json();

try {
const blob = await exportFormTemplates(
// data.sessionId,
data.templateId,
);
return new Response(blob, {
headers: {
'Content-Type': 'application/json'
}
});
} catch (err) {
console.error(`Error in exporting careplan: ${err.message}`);
return new Response(err.message);
}
};
21 changes: 20 additions & 1 deletion src/routes/api/services/form-template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BACKEND_API_URL } from '$env/static/private';
import { BACKEND_API_URL, INTERNAL_API_KEY, TOKEN } from '$env/static/private';
import { delete_, get_, post_, put_ } from './common';

//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -133,3 +133,22 @@ export const deleteFormTemplate = async (formTemplateId: string) => {
const url = BACKEND_API_URL + `/form-templates/${formTemplateId}`;
return await delete_(url);
};

export const exportFormTemplates = async (formTemplateIdId: string) => {
const url = BACKEND_API_URL + `/form-templates/${formTemplateIdId}/export`;
// const session = await SessionManager.getSession(sessionId);
// const accessToken = session.accessToken;
const headers = {};
headers['Content-Type'] = 'application/json';
headers['x-api-key'] = INTERNAL_API_KEY;
headers['Authorization'] = `Bearer ${TOKEN}`;
const res = await fetch(url, {
method: 'GET',
headers
});
if (!res.ok) {
throw new Error(`Failed to export care plan: ${res.statusText}`);
}
const blob = await res.blob();
return blob;
};