services-entry-page.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const app = require('../app')
  2. const moment = require('moment-immutable')
  3. const { editIcon } = require('../assets')
  4. app.component('appServicesEntryPage', {
  5. template: html`
  6. <app-user-area title-text="{{ctrl.terminalKey}} Service Entry">
  7. <app-breadcrumb links="[
  8. { text: 'Home', link: '/dashboard' },
  9. { text: ctrl.terminalKey + ' Services', link: '/services/' + ctrl.terminalKey },
  10. { text: ctrl.startDate.format('L') + ' - ' + ctrl.endDate.format('L'), link: '/services/' + ctrl.terminalKey + '/' + ctrl.startDate.format('YYYY-MM-DD') }
  11. ]"></app-breadcrumb>
  12. <p>For week of {{::ctrl.startDate.format('LL')}} to {{::ctrl.endDate.format('LL')}}</p>
  13. <form name="form" ng-submit="ctrl.submit()">
  14. <table md-table md-progress="ctrl.promise">
  15. <colgroup>
  16. <col style="width: 15%" />
  17. <col ng-repeat="weekday in ::ctrl.weekdays" style="width: 12%" />
  18. </colgroup>
  19. <thead md-head>
  20. <tr md-row>
  21. <th md-column>Client</th>
  22. <th md-column ng-repeat="weekday in ::ctrl.weekdays">
  23. <span hide show-xs>{{::weekday.min}}</span>
  24. <span hide show-sm>{{::weekday.short}}</span>
  25. <span hide show-gt-sm>{{::weekday.name}}</span>
  26. </th>
  27. </tr>
  28. </thead>
  29. <tbody md-body>
  30. <tr md-row ng-repeat="sfl in ::ctrl.clientServices track by sfl.id">
  31. <td md-cell>
  32. {{::ctrl.clients[sfl.id].name}}
  33. </td>
  34. <td md-cell ng-repeat="day in ::sfl.days track by $index">
  35. <input class="hour-input md-button md-raised" ng-model="day.cartons" type="number" min="0" step="1">
  36. </td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. <div>
  41. <md-button type="submit" class="md-raised md-primary">Submit</md-button>
  42. </div>
  43. </form>
  44. </app-user-area>
  45. `,
  46. controllerAs: 'ctrl',
  47. controller: function(api, $routeParams, weekdays, $mdToast) {
  48. this.weekdays = weekdays
  49. this.terminalKey = $routeParams.terminal
  50. const week = moment($routeParams.week)
  51. if (!week.isSame(week.startOf('week'))) throw new Error('Date is not start of week')
  52. this.startDate = week
  53. this.endDate = week.endOf('week')
  54. api.clientDictionary().then(clients => {
  55. this.clients = clients
  56. })
  57. this.promise = api.get(`/api/services/${$routeParams.terminal}/${$routeParams.week}`).then(({workdays}) => {
  58. this.model = workdays
  59. const clientIds = workdays[0].services.map(x => x.clientId)
  60. this.clientServices = clientIds.map((id, i) => ({
  61. id,
  62. days: this.model.map(wd => wd.services[i])
  63. }))
  64. })
  65. this.submit = async () => {
  66. const model = {
  67. workdays: this.model.map(workday => ({
  68. services: workday.services.map(service => ({
  69. clientId: service.clientId,
  70. cartons: service.cartons || null
  71. }))
  72. }))
  73. }
  74. try {
  75. await api.patch(`/api/services/${$routeParams.terminal}/${$routeParams.week}`, model)
  76. $mdToast.showSimple('Services saved.')
  77. } catch (err) {
  78. window.err = err
  79. console.error(err)
  80. $mdToast.showSimple(`Could not save Services: ${err.message || err.statusText || err}`)
  81. }
  82. }
  83. }
  84. })