post.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { View, Row } from 'app/design/view'
  2. import { useState } from 'react'
  3. import { getFormFieldByData, getHiddenFields } from 'app/lib/form/form-helpers'
  4. import Profile from 'app/ui/molecules/profile/profile';
  5. import { useCurrentUser } from 'app/context/user';
  6. import { Text } from 'app/design/typography'
  7. import MediaAttachments from 'app/components/form-fields/media-attachments'
  8. import { useTranslation } from 'react-i18next'
  9. // Rendered below attachments in this order when the form display includes them.
  10. const OPTION_FIELDS = ['labels', 'cat', 'allow_comments', 'resolvable'];
  11. export default function FormPost(props) {
  12. const { t } = useTranslation()
  13. const { data, handleSubmit } = props;
  14. const inputs = data.inputs;
  15. const [coverSource, setCoverSource] = useState([])
  16. let { currentUser } = useCurrentUser();
  17. function setPlaceHolderCover(name, previews) {
  18. if (JSON.stringify(previews) != JSON.stringify(currentUser[name])) {
  19. setCoverSource((prevImageSource) => ({
  20. ...prevImageSource,
  21. [name]: previews,
  22. }))
  23. }
  24. }
  25. const prevListCover = Object.values(coverSource)
  26. .flat()
  27. .filter((element) => element !== undefined && element !== null)
  28. if (inputs['covers']) {
  29. inputs['covers'].viewClasses =
  30. 'p-4 border-dashed border-border'
  31. inputs['covers'].caption = 'Add cover image'
  32. }
  33. // Prepare author name for the visibility switcher
  34. const authorName = <Text className="font-semibold text-card-foreground">{currentUser.display_name}</Text>;
  35. return (
  36. <View className="w-full p-4">
  37. {getHiddenFields(inputs, handleSubmit)}
  38. <View>
  39. <View className="flex-row flex-auto items-center justify-between gap-x-2 mb-4">
  40. <View className="gap-x-2 mr-2 flex-row flex-auto items-center group">
  41. <Profile {...currentUser} displaySize="lg" displayType="unit_wo_info" />
  42. {getFormFieldByData(
  43. inputs['allow_view_to'],
  44. handleSubmit,
  45. 'nofield',
  46. {
  47. variant: 'text',
  48. size: 'base',
  49. addElement: authorName
  50. }
  51. )}
  52. </View>
  53. <View>
  54. {getFormFieldByData(
  55. inputs['covers'],
  56. handleSubmit,
  57. 'notitle',
  58. { format: 'custom', view: 'button', preview: 'cover', variant: 'text', size: 'sm', previewPlaceHolder: setPlaceHolderCover, }
  59. )}
  60. </View>
  61. </View>
  62. {prevListCover.length > 0 && prevListCover[0]?.key && (
  63. <Row className="flex-wrap">{prevListCover}</Row>
  64. )}
  65. <View className="py-1">
  66. {getFormFieldByData(
  67. inputs['title'],
  68. handleSubmit,
  69. 'notitle',
  70. { placeholder: 'Title...', format: 'custom' }
  71. )}
  72. </View>
  73. <View className="py-1">
  74. {getFormFieldByData(
  75. inputs['text'],
  76. handleSubmit,
  77. 'notitle',
  78. { placeholder: 'Write your text here...', form_name: props.name }
  79. )}
  80. </View>
  81. </View>
  82. <View className="flex-col gap-4 pt-3">
  83. <MediaAttachments
  84. inputs={inputs}
  85. handleSubmit={handleSubmit}
  86. formName={props.name}
  87. label={t('Add to post')}
  88. />
  89. {/* Standard field rendering, as in FormContent. bx_posts and bx_forum
  90. share this layout but not all fields; missing inputs render nothing. */}
  91. {OPTION_FIELDS.map((key) =>
  92. getFormFieldByData(
  93. inputs[key],
  94. handleSubmit,
  95. 'default',
  96. { form_name: props.name },
  97. key
  98. )
  99. )}
  100. {getFormFieldByData(
  101. inputs['do_publish'],
  102. handleSubmit,
  103. 'default',
  104. { noPadding: true }
  105. )}
  106. {getFormFieldByData(
  107. inputs['do_submit'],
  108. handleSubmit,
  109. 'default',
  110. { noPadding: true }
  111. )}
  112. {getFormFieldByData(
  113. inputs['controls'],
  114. handleSubmit,
  115. 'default',
  116. { noPadding: true }
  117. )}
  118. </View>
  119. </View>
  120. )
  121. }