From f4c07c8baee5d9bb0c89610425d92c66314c63b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Sun, 1 Mar 2020 19:17:08 +0100 Subject: meals submission fix (now sends all meals) --- js/meals.js | 106 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 75 insertions(+), 31 deletions(-) (limited to 'js') diff --git a/js/meals.js b/js/meals.js index d04bb6c..e2d4024 100644 --- a/js/meals.js +++ b/js/meals.js @@ -278,41 +278,85 @@ async function setMenu(date, menu) { setLoading(false); localforage.settItem("logged_in_lopolis", false).then(()=>{checkLogin();}) } else { - $.ajax({ - url: API_ENDPOINT+"setmenus", - crossDomain: true, - contentType: "application/json", - headers: { - "Authorization": "Bearer "+dataauth.data - }, - data: JSON.stringify({"choices": {[date]: menu}}), - dataType: "json", - cache: false, - type: "POST", - success: (data) => { - if(data == null) { - M.toast({ html: "Request for Setting the menu failed!" }); - setLoading(false); - } else if(data.error == true) { - M.toast({html:"Setting the menu errored out"}); - setLoading(false); - } else { - M.toast({html:"Success? It looks like one. Refresh the menus to be sure."}); - setLoading(false); + d = new Date(); + $.ajax({ + url: API_ENDPOINT+"getmenus", + crossDomain: true, + contentType: "application/json", + data: JSON.stringify({"month": d.getMonth()+1, "year": d.getFullYear()}), + headers: { + "Authorization": "Bearer "+dataauth.data + }, + dataType: "json", + cache: false, + type: "POST", + success: (data) => { + if(data == null) { + M.toast({ html: "Request to get menus failed!" }); + setLoading(false); + } else if(data.error == true) { + M.toast({html:"Lopolis refused to serve menus"}); + setLoading(false); + } else { + localforage.setItem("meals", data).then((value) => { + othermeals = value; + var choices = new Object(); + // tale for loop je zelo C-jevski approach ... + for(const [mealzzdate, mealzz] of Object.entries(othermeals.data)) { + if (mealzzdate == date) { + choices[mealzzdate] = menu; + } else { + for (const [mealid, mealdata] of Object.entries(mealzz.menu_options)) { + if(mealdata.selected == true) { + choices[mealzzdate] = mealdata.value; + break; + } + } + } } - }, - error: () => { - M.toast({html:"No internet connection! Are you fucking kidding me??!?!?!"}); - setLoading(false); + // console.log(choices); // debug + $.ajax({ + url: API_ENDPOINT+"setmenus", + crossDomain: true, + contentType: "application/json", + headers: { + "Authorization": "Bearer "+dataauth.data + }, + data: JSON.stringify({"choices": choices}), + dataType: "json", + cache: false, + type: "POST", + success: (data) => { + if(data == null) { + M.toast({ html: "Request for Setting the menu failed!" }); + setLoading(false); + } else if(data.error == true) { + M.toast({html:"Setting the menu errored out"}); + setLoading(false); + } else { + M.toast({html:"Success? It looks like one. Refresh the menus to be sure."}); + setLoading(false); + } + }, + error: () => { + M.toast({html:"No internet connection! Are you fucking kidding me??!?!?!"}); + setLoading(false); + } + }); + }); } - }); - - } - }, - error: () => { + }, + error: () => { + M.toast({html:"No internet connection! (-:"}); + setLoading(false); + } + }); + } + }, + error: () => { M.toast({html:"No internet connection!"}); setLoading(false); - } + } }); } -- cgit v1.2.3