check-mp-pl-plan-detail-actions.mjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Mirrors web-admin/src/utils/mp-pl-plan-detail-actions.ts (no vitest in web-admin).
  3. */
  4. function splitMpPlanDetailActions(input) {
  5. const primary = []
  6. const more = []
  7. if (input.canEdit) primary.push({ kind: 'edit', label: '编辑' })
  8. if (input.canArrange) {
  9. primary.push({ kind: 'arrange', label: input.arrangeLabel })
  10. }
  11. if (input.canCopyDriver) more.push({ kind: 'copy-driver', label: '复制给司机' })
  12. if (input.canCopySite) more.push({ kind: 'copy-site', label: '复制给现场' })
  13. if (input.canCopyPoundQrcode) more.push({ kind: 'copy-pound-qrcode', label: '复制磅单二维码' })
  14. more.push({ kind: 'copy', label: '复制' })
  15. if (input.canWait) more.push({ kind: 'wait', label: '等客户通知' })
  16. if (input.canCancelWait) more.push({ kind: 'cancel-wait', label: '取消等通知' })
  17. if (input.canVoid) more.push({ kind: 'void', label: '作废', danger: true })
  18. while (primary.length > 2) more.unshift(primary.pop())
  19. if (primary.length === 0) {
  20. const preferKinds = ['copy', 'wait', 'cancel-wait', 'void']
  21. for (const kind of preferKinds) {
  22. if (primary.length >= 2) break
  23. const idx = more.findIndex((a) => a.kind === kind)
  24. if (idx < 0) continue
  25. primary.push(more.splice(idx, 1)[0])
  26. }
  27. while (primary.length < 2 && more.length) {
  28. primary.push(more.shift())
  29. }
  30. }
  31. return { primary, more }
  32. }
  33. function splitMpPlanFormFooterActions(input) {
  34. const more = [{ key: 'cancel', label: '取消' }]
  35. const primary = []
  36. const hasStrongPrimary = input.showSaveAndArrange || input.showRearrange
  37. if (input.showDraft) primary.push({ key: 'draft', label: '暂存' })
  38. if (input.showSave) {
  39. primary.push({
  40. key: 'save',
  41. label: input.saveLabel,
  42. type: hasStrongPrimary ? 'default' : 'primary',
  43. ghost: hasStrongPrimary
  44. })
  45. }
  46. if (input.showRearrange) {
  47. primary.push({ key: 'rearrange', label: '调整运输', type: 'primary' })
  48. } else if (input.showSaveAndArrange) {
  49. primary.push({
  50. key: 'saveAndArrange',
  51. label: input.saveAndArrangeLabel,
  52. type: 'primary'
  53. })
  54. }
  55. while (primary.length > 3) {
  56. const overflow = primary.pop()
  57. more.splice(1, 0, overflow)
  58. }
  59. if (primary.length === 0 && more.length > 1) {
  60. const first = more.find((a) => a.key !== 'cancel') || more[0]
  61. if (first) {
  62. more.splice(more.indexOf(first), 1)
  63. primary.push({ ...first, type: first.type || 'primary' })
  64. }
  65. }
  66. return { primary, more }
  67. }
  68. function assert(cond, msg) {
  69. if (!cond) {
  70. console.error('FAIL:', msg)
  71. process.exit(1)
  72. }
  73. }
  74. const both = splitMpPlanDetailActions({
  75. canEdit: true,
  76. canArrange: true,
  77. arrangeLabel: '安排运输',
  78. canWait: true,
  79. canCancelWait: false,
  80. canVoid: true
  81. })
  82. assert(
  83. both.primary.map((a) => a.kind).join(',') === 'edit,arrange',
  84. 'primary should be edit,arrange'
  85. )
  86. assert(
  87. both.more.map((a) => a.kind).join(',') === 'copy,wait,void',
  88. 'more should be copy,wait,void'
  89. )
  90. const withArrangeCopy = splitMpPlanDetailActions({
  91. canEdit: true,
  92. canArrange: true,
  93. arrangeLabel: '调整运输',
  94. canCopyDriver: true,
  95. canCopySite: true,
  96. canCopyPoundQrcode: true,
  97. canWait: false,
  98. canCancelWait: false,
  99. canVoid: true
  100. })
  101. assert(
  102. withArrangeCopy.more.map((a) => a.kind).join(',') ===
  103. 'copy-driver,copy-site,copy-pound-qrcode,copy,void',
  104. 'arrange copy actions go first in more'
  105. )
  106. const onlyEdit = splitMpPlanDetailActions({
  107. canEdit: true,
  108. canArrange: false,
  109. arrangeLabel: '安排运输',
  110. canWait: true,
  111. canCancelWait: false,
  112. canVoid: true
  113. })
  114. assert(onlyEdit.primary.map((a) => a.kind).join(',') === 'edit', 'only edit stays primary')
  115. assert(
  116. onlyEdit.more.map((a) => a.kind).join(',') === 'copy,wait,void',
  117. 'rest stay in more when primary already has items'
  118. )
  119. const onlyVoid = splitMpPlanDetailActions({
  120. canEdit: false,
  121. canArrange: false,
  122. arrangeLabel: '安排运输',
  123. canWait: false,
  124. canCancelWait: false,
  125. canVoid: true
  126. })
  127. assert(onlyVoid.primary.length === 2, 'promote more into primary only when empty')
  128. assert(onlyVoid.primary[0].kind === 'copy', 'first promoted is copy')
  129. assert(onlyVoid.primary[1].kind === 'void', 'second promoted is void')
  130. assert(onlyVoid.more.map((a) => a.kind).join(',') === '', 'more empty after promote')
  131. const onlyArrangeCopy = splitMpPlanDetailActions({
  132. canEdit: false,
  133. canArrange: false,
  134. arrangeLabel: '安排运输',
  135. canCopyDriver: true,
  136. canCopySite: false,
  137. canCopyPoundQrcode: false,
  138. canWait: false,
  139. canCancelWait: false,
  140. canVoid: false
  141. })
  142. assert(
  143. onlyArrangeCopy.primary.map((a) => a.kind).join(',') === 'copy,copy-driver',
  144. 'prefer copy over arrange-copy when promoting'
  145. )
  146. assert(
  147. onlyArrangeCopy.more.map((a) => a.kind).join(',') === '',
  148. 'more empty after promote with only copy actions'
  149. )
  150. const formNew = splitMpPlanFormFooterActions({
  151. showDraft: true,
  152. showSave: true,
  153. showSaveAndArrange: true,
  154. showRearrange: false,
  155. saveLabel: '创建',
  156. saveAndArrangeLabel: '创建并安排运输'
  157. })
  158. assert(
  159. formNew.primary.map((a) => a.key).join(',') === 'draft,save,saveAndArrange',
  160. 'form primary shows draft,save,saveAndArrange'
  161. )
  162. assert(formNew.more.map((a) => a.key).join(',') === 'cancel', 'form more only cancel')
  163. console.log('check-mp-pl-plan-detail-actions: ok')