importer stuff

This commit is contained in:
vabene1111
2024-12-12 17:26:01 +01:00
parent 37761bf6e7
commit 325ee16d39
12 changed files with 205 additions and 1913 deletions

View File

@@ -1,3 +1,7 @@
import {getCookie} from "@/utils/cookie";
import {Recipe, RecipeFromJSON, RecipeImageFromJSON, UserFileFromJSON} from "@/openapi";
import {ErrorMessageType, PreparedMessage, useMessageStore} from "@/stores/MessageStore";
/**
* Gets a nested property of an object given a dot-notation path.
*
@@ -6,13 +10,42 @@
* @returns The value of the nested property, or `undefined` if not found.
*/
export function getNestedProperty(object: any, path: string): any {
const pathParts = path.split('.');
const pathParts = path.split('.');
return pathParts.reduce((obj, key) => {
if (obj && typeof obj === 'object') {
return obj[key]
} else {
return undefined;
}
}, object);
}
//TODO just some partial code
/**
* I currently don't know how to do this properly through the API client so this
* helper function uploads files for now
*/
export function uploadRecipeImage(recipeId: number, file: File) {
let formData = new FormData()
formData.append('image', file)
//TODO proper URL finding (sub path setups)
// TODO maybe better use existing URL clients response functions for parsing
fetch('/api/recipe/' + recipeId + '/image/', {
method: 'PUT',
headers: {'X-CSRFToken': getCookie('csrftoken')},
body: formData
}).then(r => {
r.json().then(r => {
return RecipeImageFromJSON(r)
})
}).catch(err => {
useMessageStore().addError(ErrorMessageType.UPDATE_ERROR, err)
}).finally(() => {
})
return pathParts.reduce((obj, key) => {
if (obj && typeof obj === 'object') {
return obj[key]
} else {
return undefined;
}
}, object);
}