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

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

Client {{::weekday.min}} {{::weekday.short}} {{::weekday.name}}
{{::ctrl.clients[sfl.id].name}}
Submit
`, controllerAs: 'ctrl', controller: function(api, $routeParams, weekdays, $mdToast) { this.weekdays = weekdays this.terminalKey = $routeParams.terminal 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.clientDictionary().then(clients => { this.clients = clients }) this.promise = api.get(`/api/services/${$routeParams.terminal}/${$routeParams.week}`).then(({workdays}) => { this.model = workdays const clientIds = workdays[0].services.map(x => x.clientId) this.clientServices = clientIds.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 => ({ clientId: service.clientId, cartons: service.cartons || null })) })) } try { await api.patch(`/api/services/${$routeParams.terminal}/${$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}`) } } } })