bx-payment.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use client'
  2. import { useEffect } from 'react'
  3. import { useFormContext, useWatch } from 'react-hook-form'
  4. import { View } from 'app/design/view'
  5. import { getFormFieldByData, getHiddenFields } from 'app/lib/form/form-helpers'
  6. import { appSetting } from 'app/lib/util'
  7. import PaymentItems from 'app/components/form-fields/payment-items'
  8. /**
  9. * Syncs initial_members `client` selection into hidden `client_id`
  10. * (UNA classic JS: oPaymentOrders.showPopup).
  11. */
  12. function ClientIdSync() {
  13. const formContext = useFormContext()
  14. const client = useWatch({ control: formContext.control, name: 'client' })
  15. useEffect(() => {
  16. if (client == null || client === '') {
  17. formContext.setValue('client_id', '')
  18. return
  19. }
  20. const id = Array.isArray(client) ? client[0] : client
  21. if (id != null && id !== '') {
  22. formContext.setValue('client_id', String(id))
  23. }
  24. }, [client, formContext])
  25. return null
  26. }
  27. export default function FormBxPayment(props) {
  28. const { data, handleSubmit, name } = props
  29. const inputs = data?.inputs
  30. if (!inputs) return null
  31. const useCaptionAsPlaceholder = appSetting('forms', 'without_captions').includes(name)
  32. const visibleKeys = Object.keys(inputs).filter(
  33. (key) => inputs[key] && inputs[key].type !== 'hidden'
  34. )
  35. const lastKey = visibleKeys[visibleKeys.length - 1]
  36. const moduleRequestUrl = inputs.module_id?.request_url
  37. return (
  38. <View className={appSetting('forms', 'form_container')}>
  39. <ClientIdSync />
  40. {getHiddenFields(inputs, handleSubmit)}
  41. <View className="w-full gap-4">
  42. {visibleKeys.map((key) => {
  43. const field = inputs[key]
  44. const fieldProps = {
  45. form_name: name,
  46. noPadding: key === lastKey,
  47. use_caption_as_placeholder: useCaptionAsPlaceholder,
  48. }
  49. // Replace empty UNA custom HTML with interactive items picker
  50. if (key === 'items' || field?.name === 'items') {
  51. return (
  52. <PaymentItems
  53. key="payment-items"
  54. {...field}
  55. {...fieldProps}
  56. module_request_url={moduleRequestUrl}
  57. request_url={moduleRequestUrl}
  58. />
  59. )
  60. }
  61. // Skip cancel (modal close handles it); keep submit via input_set
  62. if (field?.type === 'input_set' && field?.[1]?.name === 'do_cancel') {
  63. const submitOnly = { ...field, 1: undefined }
  64. return getFormFieldByData(
  65. submitOnly,
  66. handleSubmit,
  67. 'default',
  68. fieldProps,
  69. key
  70. )
  71. }
  72. return getFormFieldByData(
  73. field,
  74. handleSubmit,
  75. 'default',
  76. fieldProps,
  77. key
  78. )
  79. })}
  80. </View>
  81. </View>
  82. )
  83. }