const app = require('../app') const moment = require('moment-immutable') const { editIcon } = require('../assets') app.component('appServicesEntryPage', { template: html`

Service Entry

For week of {{::ctrl.startDate.format('LL')}} to {{::ctrl.endDate.format('LL')}}

Client {{::weekday.min}} {{::weekday.short}} {{::weekday.name}}
{{::ctrl.retailers[sfl.id].name}}
Submit
`, controllerAs: 'ctrl', controller: function(api, $routeParams, weekdays, $mdToast) { this.weekdays = weekdays this.locationKey = $routeParams.location const week = moment($routeParams.week) if (!week.isSame(week.startOf('week'))) throw new Error('Date is not start of week') this.startDate = week this.endDate = week.endOf('week') api.retailerDictionary().then(retailers => { this.retailers = retailers }) this.promise = api.get(`/api/services/${$routeParams.location}/${$routeParams.week}`).then(({workdays}) => { this.model = workdays const retailerIds = workdays[0].services.map(x => x.retailerId) this.retailerServices = retailerIds.map((id, i) => ({ id, days: this.model.map(wd => wd.services[i]) })) }) this.submit = async () => { const model = { workdays: this.model.map(workday => ({ services: workday.services.map(service => ({ retailerId: service.retailerId, cartons: service.cartons || null })) })) } try { await api.patch(`/api/services/${$routeParams.location}/${$routeParams.week}`, model) $mdToast.showSimple('Services saved.') } catch (err) { window.err = err console.error(err) $mdToast.showSimple(`Could not save Services: ${err.message || err.statusText || err}`) } } } })