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

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

Client {{::weekday.min}} {{::weekday.date.format('M/DD')}} {{::weekday.short}} {{::weekday.date.format('M/DD')}} {{::weekday.name}}
{{::weekday.date.format('L')}}
{{::ctrl.clients[sfl.id].name}}
Submit
`, controllerAs: 'ctrl', controller: function(api, $routeParams, weekdays, $mdToast) { 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') this.weekdays = weekdays.map(d => Object.assign({}, d, {date: week.add(d.value, 'days')})) 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.saved = _.cloneDeep(this.clientServices) }) let saveTimer = null this.save = async (delay) => { if (saveTimer) { clearTimeout(saveTimer) } const model = { workdays: this.model.map(workday => ({ services: workday.services.map(service => ({ clientId: service.clientId, inbound: service.inbound || null })) })) } const saved = _.cloneDeep(this.clientServices) saveTimer = setTimeout(async () => { saveTimer = null try { await api.patch(`/api/services/${$routeParams.terminal}/${$routeParams.week}`, model) $mdToast.showSimple('Services saved.') this.saved = saved } catch (err) { window.err = err console.error(err) $mdToast.showSimple(`Could not save Services: ${err.message || err.statusText || err}`) } }, delay || 0) } this.submit = () => this.save(0) this.keypress = async ($event) => { if ($event.key === 'Enter') { const tabIndex = +$event.srcElement.getAttribute('tabindex') + 1 const nextElement = document.querySelector(`[tabindex="${tabIndex}"]`) if (nextElement) { $event.returnValue = false nextElement.focus() nextElement.select && nextElement.select() } await this.save(2000) } } } })