content.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { View } from 'app/design/view'
  2. import { getFormFieldByData, getHiddenFields } from 'app/lib/form/form-helpers'
  3. import { appSetting } from 'app/lib/util'
  4. import MediaAttachments, { getMediaFields } from 'app/components/form-fields/media-attachments'
  5. import { useTranslation } from 'react-i18next'
  6. export default function FormAds(props) {
  7. const { t } = useTranslation()
  8. const { data, handleSubmit, name } = props
  9. const inputs = data.inputs
  10. const visibleKeys = Object.keys(inputs).filter(
  11. (key) => inputs[key] && inputs[key].type !== 'hidden'
  12. )
  13. const mediaFields = getMediaFields(inputs).filter((f) => visibleKeys.includes(f.key))
  14. const firstMediaKey = mediaFields[0]?.key
  15. const renderKeys = visibleKeys.filter(
  16. (key) => !mediaFields.some((f) => f.key === key) || key === firstMediaKey
  17. )
  18. const lastKey = renderKeys[renderKeys.length - 1]
  19. const useCaptionAsPlaceholder = appSetting('forms', 'without_captions').includes(name)
  20. return (
  21. <View className={appSetting('forms', 'form_container')}>
  22. {getHiddenFields(inputs, handleSubmit)}
  23. <View className="w-full gap-4">
  24. {renderKeys.map((key) => {
  25. if (key === firstMediaKey) {
  26. return (
  27. <MediaAttachments
  28. key="media-attachments"
  29. inputs={inputs}
  30. handleSubmit={handleSubmit}
  31. formName={name}
  32. label={t('Attachments')}
  33. />
  34. )
  35. }
  36. return getFormFieldByData(
  37. inputs[key],
  38. handleSubmit,
  39. 'default',
  40. {
  41. form_name: name,
  42. noPadding: key === lastKey,
  43. use_caption_as_placeholder: useCaptionAsPlaceholder,
  44. },
  45. key
  46. )
  47. })}
  48. </View>
  49. </View>
  50. )
  51. }