From a8059eed4c4a19c4df6460d1916b51611ec0ef7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksi=20Sepp=C3=A4nen?= Date: Mon, 1 May 2023 17:36:43 +0300 Subject: [PATCH] Wait for api results before going to list view --- src/components/EditData.vue | 43 ++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/components/EditData.vue b/src/components/EditData.vue index dfa81bd..c74944c 100644 --- a/src/components/EditData.vue +++ b/src/components/EditData.vue @@ -30,49 +30,40 @@ const getGames = () => { }); } -const postGame = (game) => { - axios.post(`${url}/gamedata`, game, { +const postGame = async (game) => { + return axios.post(`${url}/gamedata`, game, { auth: { username: localStorage.getItem('username'), password: localStorage.getItem('password') } - }) - .then((response) => { - console.log(response) - }).catch((err) => { + }).catch((err) => { console.log(err); alert("Something went wrong, refresh page and see damages") }); } -const deleteGame = (id) => { - axios.delete(`${url}/gamedata/${id}`, { +const deleteGame = async (id) => { + return axios.delete(`${url}/gamedata/${id}`, { auth: { username: localStorage.getItem('username'), password: localStorage.getItem('password') } - }) - .then((response) => { - console.log(response) - }).catch((err) => { + }).catch((err) => { console.log(err); alert("Something went wrong, refresh page and see damages") }); } -const patchGame = (id, game) => { - axios.patch(`${url}/gamedata/${id}`, game, { +const patchGame = async (id, game) => { + return axios.patch(`${url}/gamedata/${id}`, game, { auth: { username: localStorage.getItem('username'), password: localStorage.getItem('password') } - }) - .then((response) => { - console.log(response) - }).catch((err) => { - console.log(err); - alert("Something went wrong, refresh page and see damages") - }); + }).catch((err) => { + console.log(err); + alert("Something went wrong, refresh page and see damages") + }); } const getPlayers = () => { @@ -138,7 +129,7 @@ const changeView = (view) => { mode.value = view } -const changesToProd = () => { +const changesToProd = async () => { const conf = confirm('Do you want these changes to prod?'); if (conf){ @@ -162,11 +153,13 @@ const changesToProd = () => { }) for (const key in changes.value.patch) { - patchGame(key, changes.value.patch[key]) + await patchGame(key, changes.value.patch[key]) } - changes.value.post.map(game => postGame(game)) - changes.value.delete.map(id => deleteGame(id)) + const posts = changes.value.post.map(game => postGame(game)) + const deletes = changes.value.delete.map(id => deleteGame(id)) + + await Promise.all([...posts, ...deletes]); changes.value = { patch: {},