script to handle upload working

This commit is contained in:
2025-11-17 16:37:11 +13:00
parent 3c708d4f77
commit c345c005cf

View File

@@ -18,10 +18,44 @@ tags:
` `
--- ---
<script is:inline> <script>
document.querySelector('#btn-save')?.addEventListener('onclick', () => { document.querySelector('#btn-save')!.addEventListener('click', async () => {
console.log("hi") try {
}) const cooklangTextarea = document.querySelector('#cooklang') as HTMLTextAreaElement;
const cooklangContent = cooklangTextarea?.value;
// Get the file input
const photoInput = document.querySelector('#photo') as HTMLInputElement;
const photoFile = photoInput?.files?.[0];
// Create FormData for the upload
const formData = new FormData();
formData.append('cooklang', cooklangContent);
if (photoFile) {
formData.append('images', photoFile);
}
// Upload to PocketBase via /api
const response = await fetch('/api/collections/recipes/records', {
method: 'POST',
body: formData
});
if (response.ok) {
const result = await response.json();
// Redirect to the recipe page
window.location.href = `/recipe/${result.id}`;
} else {
const error = await response.text();
console.error('Save failed:', error);
alert('Failed to save recipe: ' + error);
}
} catch (error) {
console.error('Error saving recipe:', error);
alert('Error saving recipe: ' + error);
}
});
</script> </script>
<Base> <Base>