diff --git a/src/components/ItemComponents/NewItemForm.vue b/src/components/ItemComponents/NewItemForm.vue index 67f8f3032c3dacdbfc19f2fc29652be86d3c1db5..b87836bdb84528bc1f0eb8240dce30fcd9740f2b 100644 --- a/src/components/ItemComponents/NewItemForm.vue +++ b/src/components/ItemComponents/NewItemForm.vue @@ -213,7 +213,7 @@ <script> import useVuelidate from "@vuelidate/core"; import { parseUserFromToken } from "@/utils/token-utils"; -import { postNewItem, getMyGroups } from "@/utils/apiutil"; +import {postNewItem, getMyGroups, postNewImageCommunity, PostImagesArrayToListing} from "@/utils/apiutil"; import Button from "@/components/BaseComponents/ColoredButton"; import { @@ -300,6 +300,7 @@ export default { userId: -1, selectedGroupId: -1, selectedGroups: [], + imagesToSend: [], }, //Kategorier skal legges inn ved api/hente fra db, her må det endres etterhvert categories: ["Hage", "Kjøkken", "Musikk", "Annet"], @@ -322,6 +323,10 @@ export default { async saveClicked() { if (this.checkValidation()) { this.checkUser(); + + //Not sure works + console.log(this.item.imagesToSend); + const itemInfo = { title: this.item.title, description: this.item.description, @@ -333,6 +338,9 @@ export default { }; await postNewItem(itemInfo); + //Not sure it will work + await PostImagesArrayToListing(this.item.imagesToSend); + this.$router.push("/"); } }, @@ -342,8 +350,26 @@ export default { this.item.userId = parseInt(user.accountId); }, - addImage: function (event) { + //Not sure this method works + addImage: async function (event) { this.item.images.push(URL.createObjectURL(event.target.files[0])); + + var that = this; + let image = event.target.files[0]; + let fileReader = new FileReader(); + fileReader.onloadend = async function () { + const res = fileReader.result; + const id = await postNewImageCommunity(res); + + const API_URL = process.env.VUE_APP_BASEURL; + console.log(id); + console.log(API_URL + "images/" + id); + + //Not sure if this will work + that.item.imagesToSend.push(API_URL + "images/" + id); + + }; + fileReader.readAsArrayBuffer(image); }, getGroups: async function () { diff --git a/src/utils/apiutil.js b/src/utils/apiutil.js index f33d6db9f3ce03cac2a5b9d6dc149c41b1949912..40729766673309b8ce0781eaf66b737a68b9fd85 100644 --- a/src/utils/apiutil.js +++ b/src/utils/apiutil.js @@ -340,3 +340,18 @@ export function postNewImageCommunity(image) { return error; }); } + +//Not sure it will work +export function PostImagesArrayToListing(imagesArray) { + return axios + .post(API_URL + "images/listing", imagesArray, { + headers: tokenHeader(), + }) + .then((response) => { + return response; + }) + .catch((error) => { + console.error(error.response); + return error; + }); +}