polls.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import Field, { getValidationRules } from './_field';
  2. import { fetcher } from 'app/lib/fetcher';
  3. import { Modal, NeoButton } from "app/design/controls";
  4. import { useState, useEffect, useCallback } from 'react';
  5. import Form from 'app/components/elements/form';
  6. import useFetchForm from 'app/lib/hooks/use-fetch-form'
  7. import emitter, { EVENTS } from 'app/context/emitter';
  8. import { PollItem } from 'app/components/elements/entity-poll';
  9. import { useFormField } from 'app/lib/form/use-form-field';
  10. import { useTranslation } from 'react-i18next';
  11. export default function FormFieldPolls(props) {
  12. const { t } = useTranslation();
  13. const name = props.name;
  14. const [isModal, setIsModal] = useState(false);
  15. const [pollSource, setPollSource] = useState(props.values || []);
  16. const [dataForm, setDataForm] = useState(false);
  17. const { field } = useFormField(props, {
  18. rules: getValidationRules(props),
  19. syncValue: false,
  20. });
  21. const { data: dynamicData } = useFetchForm(props.request_submit, dataForm);
  22. useEffect(() => {
  23. if (dynamicData?.data?.id) {
  24. const a = String(field.value || '').split(',');
  25. a.push(dynamicData.data.id);
  26. field.onChange([...new Set(a.filter(Boolean))].join(','));
  27. setIsModal(false);
  28. setPollSource((prevPollSource) => ([
  29. ...prevPollSource,
  30. dynamicData.data.item
  31. ]))
  32. }
  33. }, [dynamicData]);
  34. useEffect(() => {
  35. const subscription = emitter.addListener(EVENTS.fieldPolls(name), (data) => {
  36. if (data.action == 'add') {
  37. showSelect()
  38. }
  39. })
  40. return () => {
  41. subscription.remove()
  42. }
  43. }, [])
  44. const showSelect = async () => {
  45. const sResponse = await fetcher(props.request_get);
  46. setIsModal(sResponse.data[0])
  47. }
  48. const onFormSubmit = useCallback((formData) => {
  49. setDataForm(formData);
  50. }, []);
  51. async function deletePoll(url, poll) {
  52. await fetcher(url + '&params[]=' + poll.id);
  53. setPollSource((prevPollSource) =>
  54. prevPollSource.filter(item => item.id !== poll.id)
  55. );
  56. }
  57. const frmData = dynamicData?.data[0] || isModal;
  58. return (
  59. <Field {...props}>
  60. <Modal
  61. title={t('Create a poll')}
  62. onVisible={!!isModal}
  63. transparent={true}
  64. headerBorder={true}
  65. scrollable={true}
  66. onClose={() => { setIsModal(false) }}
  67. >
  68. {!!isModal && <Form {...frmData} onFormSubmit={onFormSubmit} resetOnSubmit={false} />}
  69. </Modal>
  70. {pollSource && pollSource.map((item, index) => {
  71. return <PollItem results_url='/api.php?r=bx_timeline/get_block_poll_results' disabled = {true} onDelete={() => { deletePoll(props.request_remove, item) }} key={"att" + index} data={item} showTitle={true} />
  72. })}
  73. {!props.hide_button && <NeoButton
  74. classNames={{ root: 'mt-4' }}
  75. label={t('Add Poll')}
  76. onPress={() => {
  77. emitter.emit(EVENTS.fieldPolls(name), { action: 'add' });
  78. }}
  79. />}
  80. </Field>
  81. );
  82. }