From 6a2c22f24a005ea677e895a51a9f583ffa00265d Mon Sep 17 00:00:00 2001 From: mrfry Date: Wed, 17 Mar 2021 16:39:39 +0100 Subject: [PATCH] Feedback now includes file name if one is uploaded --- src/pages/index.js | 55 +++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/pages/index.js b/src/pages/index.js index 18987f7..1ff39c1 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -61,8 +61,8 @@ function addPost(title, content) { } function postFeedback(content, file) { - return new Promise((resolve) => { - const promises = [ + const postText = (file) => { + return new Promise((resolve) => { fetch(constants.apiUrl + 'postfeedback', { method: 'POST', credentials: 'include', @@ -72,34 +72,44 @@ function postFeedback(content, file) { }, body: JSON.stringify({ content: content, + file: file ? file.name : null, }), - }).then((res) => { - return res.json() - }), - ] + }) + .then((res) => { + return res.json() + }) + .then((res) => { + resolve(res) + }) + }) + } + return new Promise((resolve) => { if (file) { - console.log('FIEEEEEEEEEELE') const formData = new FormData() // eslint-disable-line formData.append('file', file) - promises.push( - fetch(constants.apiUrl + 'postfeedbackfile', { - method: 'POST', - credentials: 'include', - headers: { - Accept: 'application/json', - }, - body: formData, - }).then((res) => { + fetch(constants.apiUrl + 'postfeedbackfile', { + method: 'POST', + credentials: 'include', + headers: { + Accept: 'application/json', + }, + body: formData, + }) + .then((res) => { return res.json() }) - ) + .then((fileres) => { + postText(file).then((textres) => { + resolve({ fileres, textres }) + }) + }) + } else { + postText().then((res) => { + resolve(res) + }) } - - Promise.all(promises).then((res) => { - resolve(res) - }) }) } @@ -252,8 +262,7 @@ export default function Index({ globalData }) { } console.log(type, title, content, file) if (type === 'private') { - postFeedback(content, file).then((res) => { - console.log(res) + postFeedback(content, file).then((/* { fileres, textres } */) => { alert('Privát visszajelzés elküldve!') }) } else {