labor-entry-page.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const app = require('../app')
  2. const moment = require('moment-immutable')
  3. const { editIcon } = require('../assets')
  4. app.component('appLaborEntryPage', {
  5. template: html`
  6. <app-user-area>
  7. <app-breadcrumb links="[
  8. { text: 'Home', link: '/dashboard' },
  9. { text: ctrl.locationKey + ' Labor', link: '/labor/' + ctrl.locationKey },
  10. { text: ctrl.startDate.format('L') + ' - ' + ctrl.endDate.format('L'), link: '/labor/' + ctrl.locationKey + '/' + ctrl.startDate.format('YYYY-MM-DD') }
  11. ]"></app-breadcrumb>
  12. <h1>Labor 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>Staff Member</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.staffMemberLabor track by sfl.id">
  32. <td md-cell>
  33. {{::ctrl.staffMembers[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.hours" type="number" min="0" step="0.01">
  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.staffMemberDictionary().then(staffMembers => {
  56. this.staffMembers = staffMembers
  57. })
  58. this.promise = api.get(`/api/labor/${$routeParams.location}/${$routeParams.week}`).then(workdays => {
  59. this.model = workdays
  60. const staffMemberIds = workdays[0].labor.map(x => x.staffMemberId)
  61. this.staffMemberLabor = staffMemberIds.map((id, i) => ({
  62. id,
  63. days: this.model.map(wd => wd.labor[i])
  64. }))
  65. })
  66. this.submit = async () => {
  67. const model = this.model.map(workday => ({
  68. labor: workday.labor.map(labor => ({
  69. staffMemberId: labor.staffMemberId,
  70. hours: labor.hours || null
  71. }))
  72. }))
  73. try {
  74. await api.patch(`/api/labor/${$routeParams.location}/${$routeParams.week}`, model)
  75. $mdToast.showSimple('Labor saved.')
  76. } catch (err) {
  77. window.err = err
  78. console.error(err)
  79. $mdToast.showSimple(`Could not save Labor: ${err.message || err.statusText || err}`)
  80. }
  81. }
  82. }
  83. })