diff --git a/hw7/Claudio_Maggioni/public/js/views.js b/hw7/Claudio_Maggioni/public/js/views.js index 9abe443..3ffca32 100644 --- a/hw7/Claudio_Maggioni/public/js/views.js +++ b/hw7/Claudio_Maggioni/public/js/views.js @@ -1,6 +1,6 @@ (function(dust){dust.register("500",body_0);function body_0(chk,ctx){return chk.w("

Error 500

").f(ctx.get(["err"], false),ctx,"h").w("
");}body_0.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("favourite",body_0);function body_0(chk,ctx){return chk.w("").f(ctx.get(["name"], false),ctx,"h").w("").p("favourite_partial",ctx,ctx,{}).w("Favourites list");}body_0.__dustBody=!0;return body_0}(dust)); -(function(dust){dust.register("favourite_partial",body_0);function body_0(chk,ctx){return chk.w("

").f(ctx.get(["name"], false),ctx,"h").w("

\"").f(ctx.get(["name"],").x(ctx.get(["b"], false),ctx,{"block":body_1},{}).x(ctx.get(["details"], false),ctx,{"else":body_2,"block":body_5},{});}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("

Bookmarked

");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("




").x(ctx.get(["bookmarked"], false),ctx,{"else":body_3,"block":body_4},{}).w("



");}body_2.__dustBody=!0;function body_3(chk,ctx){return chk.w("");}body_3.__dustBody=!0;function body_4(chk,ctx){return chk.w("");}body_4.__dustBody=!0;function body_5(chk,ctx){return chk.w("Details");}body_5.__dustBody=!0;return body_0}(dust)); +(function(dust){dust.register("favourite_partial",body_0);function body_0(chk,ctx){return chk.w("

").f(ctx.get(["name"], false),ctx,"h").w("

\"").f(ctx.get(["name"],").x(ctx.get(["b"], false),ctx,{"block":body_1},{}).x(ctx.get(["details"], false),ctx,{"else":body_2,"block":body_5},{});}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("

Bookmarked

");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("




").x(ctx.get(["bookmarked"], false),ctx,{"else":body_3,"block":body_4},{}).w("






");}body_2.__dustBody=!0;function body_3(chk,ctx){return chk.w("");}body_3.__dustBody=!0;function body_4(chk,ctx){return chk.w("");}body_4.__dustBody=!0;function body_5(chk,ctx){return chk.w("Details");}body_5.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("favourites",body_0);function body_0(chk,ctx){return chk.w("").x(ctx.get(["bookmarked"], false),ctx,{"else":body_1,"block":body_2},{}).w("").x(ctx.get(["bookmarked"], false),ctx,{"else":body_3,"block":body_4},{}).s(ctx.get(["favs"], false),ctx,{"else":body_5,"block":body_6},{}).w("");}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("Favourites");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("Bookmarked");}body_2.__dustBody=!0;function body_3(chk,ctx){return chk.w("

Favourites

");}body_3.__dustBody=!0;function body_4(chk,ctx){return chk.w("

Bookmarked

");}body_4.__dustBody=!0;function body_5(chk,ctx){return chk.w("No favourites.");}body_5.__dustBody=!0;function body_6(chk,ctx){return chk.w("
").p("favourite_partial",ctx,ctx,{"name":ctx.get(["name"], false),"dataURL":ctx.get(["dataURL"], false),"_id":ctx.get(["_id"], false),"bookmarked":ctx.get(["bookmarked"], false),"details":"true"}).w("
");}body_6.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("index",body_0);function body_0(chk,ctx){return chk.w("OO-JS Exercise - Web Atelier 2017

OO-JS Exercise: Canvas



");}body_0.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("server_side",body_0);function body_0(chk,ctx){return chk.s(ctx.get(["favs"], false),ctx,{"block":body_1},{});}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("
").p("favourite_partial",ctx,ctx,{"name":ctx.get(["name"], false),"dataURL":ctx.get(["dataURL"], false),"_id":ctx.get(["_id"], false),"bookmarked":ctx.get(["bookmarked"], false)}).w("
");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("bookmarked");}body_2.__dustBody=!0;return body_0}(dust)); diff --git a/hw7/Claudio_Maggioni/public/scripts/app.js b/hw7/Claudio_Maggioni/public/scripts/app.js index bd16af5..fd72d00 100644 --- a/hw7/Claudio_Maggioni/public/scripts/app.js +++ b/hw7/Claudio_Maggioni/public/scripts/app.js @@ -17,6 +17,7 @@ class App { const imgurForm = dom.querySelector('form.favorites'); const replaceToggle = dom.querySelector('input[name=replace]'); const albumInput = imgurForm.querySelector('input[name=album]'); + const oldNameInput = imgurForm.querySelector('input[name=old_name]'); const replacePanel = imgurForm.querySelector('.replace'); dom.querySelector('button.imgur').onclick = () => { @@ -27,9 +28,11 @@ class App { if (replaceToggle.checked) { replacePanel.classList.remove('hidden'); albumInput.setAttribute('disabled', 'disabled'); + oldNameInput.setAttribute('required', 'required'); } else { replacePanel.classList.add('hidden'); albumInput.removeAttribute('disabled'); + oldNameInput.removeAttribute('required'); } }; @@ -49,7 +52,6 @@ class App { favorites: data.get('favorites') }; - doJSONRequest('POST', '/imgur/ordeal', {}, obj) .then(e => { if (e.ordealSuccess === true) { diff --git a/hw7/Claudio_Maggioni/routes/root/router.js b/hw7/Claudio_Maggioni/routes/root/router.js index 27a3a17..69174f4 100644 --- a/hw7/Claudio_Maggioni/routes/root/router.js +++ b/hw7/Claudio_Maggioni/routes/root/router.js @@ -68,6 +68,7 @@ router.post('/imgur/ordeal', async (req, res) => { const uploadData = { image: req.body.dataURL.substring('data:image/png;base64,'.length), type: 'base64', + title: req.body.name, name: req.body.name + '.png', }; @@ -127,7 +128,9 @@ router.post('/imgur/ordeal', async (req, res) => { } const image = - images.data.filter(e => e.name.match(req.body.oldName))[0]; + images.data.filter(e => e.title.match(req.body.oldName))[0]; + + console.log('image', image); if (!image || reject) { rej(); @@ -139,12 +142,25 @@ router.post('/imgur/ordeal', async (req, res) => { })); } - const result = await promiseAny(promises); + let result; + try { + result = await promiseAny(promises); + } catch(_) { + res.status(404).json({ error: "No image in gallery found" }); + return; + } + console.log('result', result); albumJson = { data: result.album }; ordealResponse.views = result.image.views; ordealResponse.votes = await fetchImgur(req, res, 'GET', '/gallery/' - + result.image.id + '/votes'); + + result.album.id + '/votes'); + + if (!ordealResponse.votes) { + return; + } + + console.log('votes', ordealResponse.votes); if (!await fetchImgur(req, res, 'DELETE', '/image/' + result.image.id)) { return; diff --git a/hw7/Claudio_Maggioni/views/favourite_partial.dust b/hw7/Claudio_Maggioni/views/favourite_partial.dust index d6596f8..92cc359 100644 --- a/hw7/Claudio_Maggioni/views/favourite_partial.dust +++ b/hw7/Claudio_Maggioni/views/favourite_partial.dust @@ -35,8 +35,8 @@ formaction="/favorites/{_id}/bookmarked?_method=PUT">Add bookmark
- + {/details}