services-entry-page.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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>
  7. <app-breadcrumb links="[
  8. { text: 'Home', link: '/dashboard' },
  9. { text: ctrl.locationKey + ' Services', link: '/services/' + ctrl.locationKey },
  10. { text: ctrl.startDate.format('L') + ' - ' + ctrl.endDate.format('L'), link: '/services/' + ctrl.locationKey + '/' + ctrl.startDate.format('YYYY-MM-DD') }
  11. ]"></app-breadcrumb>
  12. <h1>Service Entry</h1>
  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}}</span>
  25. <span hide show-sm>{{::weekday.short}}</span>
  26. <span hide show-gt-sm>{{::weekday.name}}</span>
  27. </th>
  28. </tr>
  29. </thead>
  30. <tbody md-body>
  31. <tr md-row ng-repeat="sfl in ::ctrl.retailerServices track by sfl.id">
  32. <td md-cell>
  33. {{::ctrl.retailers[sfl.id].name}}
  34. </td>
  35. <td md-cell ng-repeat="day in ::sfl.days track by $index">
  36. <input class="hour-input md-button md-raised" ng-model="day.cartons" type="number" min="0" step="1">
  37. </td>
  38. </tr>
  39. </tbody>
  40. </table>
  41. <div>
  42. <md-button type="submit" class="md-raised md-primary">Submit</md-button>
  43. </div>
  44. </form>
  45. </app-user-area>
  46. `,
  47. controllerAs: 'ctrl',
  48. controller: function(api, $routeParams, weekdays, $mdToast) {
  49. this.weekdays = weekdays
  50. this.locationKey = $routeParams.location
  51. const week = moment($routeParams.week)
  52. if (!week.isSame(week.startOf('week'))) throw new Error('Date is not start of week')
  53. this.startDate = week
  54. this.endDate = week.endOf('week')
  55. api.retailerDictionary().then(retailers => {
  56. this.retailers = retailers
  57. })
  58. this.promise = api.get(`/api/services/${$routeParams.location}/${$routeParams.week}`).then(({workdays}) => {
  59. this.model = workdays
  60. const retailerIds = workdays[0].services.map(x => x.retailerId)
  61. this.retailerServices = retailerIds.map((id, i) => ({
  62. id,
  63. days: this.model.map(wd => wd.services[i])
  64. }))
  65. })
  66. this.submit = async () => {
  67. const model = {
  68. workdays: this.model.map(workday => ({
  69. services: workday.services.map(service => ({
  70. retailerId: service.retailerId,
  71. cartons: service.cartons || null
  72. }))
  73. }))
  74. }
  75. try {
  76. await api.patch(`/api/services/${$routeParams.location}/${$routeParams.week}`, model)
  77. $mdToast.showSimple('Services saved.')
  78. } catch (err) {
  79. window.err = err
  80. console.error(err)
  81. $mdToast.showSimple(`Could not save Services: ${err.message || err.statusText || err}`)
  82. }
  83. }
  84. }
  85. })