diff options
author | Anton Luka Šijanec <sijanecantonluka@gmail.com> | 2020-03-01 19:17:08 +0100 |
---|---|---|
committer | Anton Luka Šijanec <sijanecantonluka@gmail.com> | 2020-03-01 19:17:08 +0100 |
commit | f4c07c8baee5d9bb0c89610425d92c66314c63b2 (patch) | |
tree | ab9fe2193d01085522b187b9805a8a66736f2cec /js | |
parent | meals fix and 1.0.11 version bump (diff) | |
download | beziapp-f4c07c8baee5d9bb0c89610425d92c66314c63b2.tar beziapp-f4c07c8baee5d9bb0c89610425d92c66314c63b2.tar.gz beziapp-f4c07c8baee5d9bb0c89610425d92c66314c63b2.tar.bz2 beziapp-f4c07c8baee5d9bb0c89610425d92c66314c63b2.tar.lz beziapp-f4c07c8baee5d9bb0c89610425d92c66314c63b2.tar.xz beziapp-f4c07c8baee5d9bb0c89610425d92c66314c63b2.tar.zst beziapp-f4c07c8baee5d9bb0c89610425d92c66314c63b2.zip |
Diffstat (limited to 'js')
-rw-r--r-- | js/meals.js | 106 |
1 files changed, 75 insertions, 31 deletions
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); - } + } }); } |