const config = 'dev'; const backend_api = { 'prod': '/zetikettes/srv/', 'dev': 'http://scrawny.local:3000', }[config]; let token = localStorage.getItem('token'); async function getBandz() { const uri = `${backend_api}/bandz`; const res = await $.ajax(uri, {headers: {Authorization: `Bearer ${token}`}}); // if (res.statusCode !== 200) { // throw res; // } return res.bandz; } async function wrapAChikin(req) { const uri = `${backend_api}/wrap`; const res = await $.ajax(uri, { type: 'POST', contentType: 'application/json', data: JSON.stringify(req), headers: {Authorization: `Bearer ${token}`}, }); return res.chikinId; } async function authenticate(password) { const uri = `${backend_api}/toktok`; const res = await $.ajax(uri, { type: 'POST', contentType: 'application/json', data: JSON.stringify({password}), }); return res; } function printLabel(chikinId) { const uri = `${backend_api}/print`; $.ajax(uri, { type: 'POST', contentType: 'application/json', data: JSON.stringify({chikinId}), headers: {Authorization: `Bearer ${token}`}, }); } $(document).ready(() => { const appbody = $("#appbody"); const bandSelect = $('
') .append( $('') .append('') ); const loader = $('
') .hide(); const action = $('
') .append( $('wrap-a-chikinsend') .click(async () => { loader.show(); $('#send').addClass("disabled"); const req = { bandId: $('select').val(), wrapDate: $('.datepicker').val(), weight: $('#weight').val(), wounded: $('#wounded').prop('checked'), }; try { const chikinId = await wrapAChikin(req); M.toast({html: `Wrapped chikin ID ${chikinId}`}); printLabel(chikinId); } finally { loader.hide(); $('#weight').val(''); $('#wounded').prop('checked', false); $('#send').removeClass('disabled'); } }) ); block .append( $('
') .append('') .append('') ) .append( $('
') .append('') ) .append( $('
') .append(action) .append(loader) ) ; appbody.append(block); getBandz().then(bandz => { $('select').append(bandz.map(band => ``)); $('select').formSelect(); $('#send').removeClass("disabled"); }).catch(xhr => { console.log(xhr); console.log('blarg'); if (xhr.statusCode === 403) { M.toast({html: 'Authentication failed.'}); } }); const authBlock = $('
').hide() .append( $('
') .append('') .append('') ) .append( $('
').append( $('authenticate') .click(async () => { const {token:tok} = await authenticate($('#password').val()); localStorage.setItem('token', tok); token = tok; $('#authBlock').hide(); }) ) ); appbody.append(authBlock); let tapped = 0; $('.page-footer').click(() => { tapped += 1; if (tapped >= 7) { tapped = 0; $('#authBlock').show(); } }); $('select').formSelect(); $('.datepicker').datepicker({setDefaultDate: true, defaultDate: new Date()}); $('#weight').keypress(e => { if (e.which == 13) { $('#send').trigger('click'); } }); });