services-entry-page.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const app = require('../app')
  2. const moment = require('moment-immutable')
  3. const { editIcon } = require('../assets')
  4. const _ = require('lodash')
  5. app.component('appServicesEntryPage', {
  6. template: html`
  7. <app-user-area title-text="{{ctrl.terminalKey}} Service Entry">
  8. <app-breadcrumb links="[
  9. { text: 'Home', link: '/dashboard' },
  10. { text: ctrl.terminalKey + ' Services', link: '/services/' + ctrl.terminalKey },
  11. { text: ctrl.startDate.format('L') + ' - ' + ctrl.endDate.format('L'), link: '/services/' + ctrl.terminalKey + '/' + ctrl.startDate.format('YYYY-MM-DD') }
  12. ]"></app-breadcrumb>
  13. <p>For week of {{::ctrl.startDate.format('LL')}} to {{::ctrl.endDate.format('LL')}}</p>
  14. <form name="form" ng-submit="ctrl.submit()">
  15. <table md-table md-progress="ctrl.promise">
  16. <colgroup>
  17. <col style="width: 15%" />
  18. <col ng-repeat="weekday in ::ctrl.weekdays" style="width: 12%" />
  19. </colgroup>
  20. <thead md-head>
  21. <tr md-row>
  22. <th md-column>Client</th>
  23. <th md-column ng-repeat="weekday in ::ctrl.weekdays">
  24. <span hide show-xs>{{::weekday.min}} {{::weekday.date.format('M/DD')}}</span>
  25. <span hide show-sm>{{::weekday.short}} {{::weekday.date.format('M/DD')}}</span>
  26. <span hide show-gt-sm>{{::weekday.name}}<br />{{::weekday.date.format('L')}}</span>
  27. </th>
  28. </tr>
  29. </thead>
  30. <tbody md-body>
  31. <tr md-row ng-repeat="sfl in ::ctrl.clientServices track by sfl.id" ng-init="$sflIndex = $index">
  32. <td md-cell>
  33. {{::ctrl.clients[sfl.id].name}}
  34. </td>
  35. <td md-cell ng-repeat="day in ::sfl.days track by $index" ng-init="$dayIndex = $index">
  36. <input
  37. class="hour-input md-button"
  38. ng-model="day.inbound"
  39. tabindex="{{$index * ctrl.clientServices.length + ($parent.$index) + 1}}"
  40. type="number" min="0" step="1"
  41. ng-keypress="ctrl.keypress($event)"
  42. md-whiteframe="{{(day.inbound == ctrl.saved[$sflIndex].days[$dayIndex].inbound) ? 1 : 8}}">
  43. </td>
  44. </tr>
  45. </tbody>
  46. </table>
  47. <div>
  48. <md-button type="submit" class="md-raised md-primary">Submit</md-button>
  49. </div>
  50. </form>
  51. </app-user-area>
  52. `,
  53. controllerAs: 'ctrl',
  54. controller: function(api, $routeParams, weekdays, $mdToast) {
  55. this.terminalKey = $routeParams.terminal
  56. const week = moment($routeParams.week)
  57. if (!week.isSame(week.startOf('week'))) throw new Error('Date is not start of week')
  58. this.startDate = week
  59. this.endDate = week.endOf('week')
  60. this.weekdays = weekdays.map(d => Object.assign({}, d, {date: week.add(d.value, 'days')}))
  61. api.clientDictionary().then(clients => {
  62. this.clients = clients
  63. })
  64. this.promise = api.get(`/api/services/${$routeParams.terminal}/${$routeParams.week}`).then(({workdays}) => {
  65. this.model = workdays
  66. const clientIds = workdays[0].services.map(x => x.clientId)
  67. this.clientServices = clientIds.map((id, i) => ({
  68. id,
  69. days: this.model.map(wd => wd.services[i])
  70. }))
  71. this.saved = _.cloneDeep(this.clientServices)
  72. })
  73. let saveTimer = null
  74. this.save = async (delay) => {
  75. if (saveTimer) {
  76. clearTimeout(saveTimer)
  77. }
  78. const model = {
  79. workdays: this.model.map(workday => ({
  80. services: workday.services.map(service => ({
  81. clientId: service.clientId,
  82. inbound: service.inbound || null
  83. }))
  84. }))
  85. }
  86. const saved = _.cloneDeep(this.clientServices)
  87. saveTimer = setTimeout(async () => {
  88. saveTimer = null
  89. try {
  90. await api.patch(`/api/services/${$routeParams.terminal}/${$routeParams.week}`, model)
  91. $mdToast.showSimple('Services saved.')
  92. this.saved = saved
  93. } catch (err) {
  94. window.err = err
  95. console.error(err)
  96. $mdToast.showSimple(`Could not save Services: ${err.message || err.statusText || err}`)
  97. }
  98. }, delay || 0)
  99. }
  100. this.submit = () => this.save(0)
  101. this.keypress = async ($event) => {
  102. if ($event.key === 'Enter') {
  103. const tabIndex = +$event.srcElement.getAttribute('tabindex') + 1
  104. const nextElement = document.querySelector(`[tabindex="${tabIndex}"]`)
  105. if (nextElement) {
  106. $event.returnValue = false
  107. nextElement.focus()
  108. nextElement.select && nextElement.select()
  109. }
  110. await this.save(2000)
  111. }
  112. }
  113. }
  114. })