Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3688 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3688 Rev 3719
Línea 1... Línea 1...
1
import React, { useMemo } from 'react'
1
import React, { useMemo } from 'react';
2
import { useDispatch } from 'react-redux'
2
import { useDispatch } from 'react-redux';
3
import { useForm } from 'react-hook-form'
3
import { useForm } from 'react-hook-form';
4
 
4
 
5
import { axios } from '@utils'
5
import { axios } from '@utils';
6
import { addNotification } from '@store/notification/notification.actions'
6
import { addNotification } from '@store/notification/notification.actions';
7
 
7
 
8
import Modal from '@components/UI/modal/Modal'
8
import Modal from '@components/UI/modal/Modal';
9
import Select from '@components/UI/inputs/Select'
9
import Select from '@components/UI/inputs/Select';
10
 
10
 
11
export default function PrivacyModal({
11
export default function PrivacyModal({
12
  show = false,
12
  show = false,
13
  privacies = {},
13
  privacies = {},
14
  privacy,
14
  privacy,
15
  groupId = '',
15
  groupId = '',
16
  onClose = () => {},
16
  onClose = () => {},
17
  onConfirm = () => {}
17
  onConfirm = () => {}
18
}) {
18
}) {
19
  const privacyId = useMemo(() => {
19
  const privacyId = useMemo(() => {
20
    const privaciesEntries = Object.entries(privacies)
20
    const privaciesEntries = Object.entries(privacies);
21
    const currentPrivacy = privaciesEntries.find((priv) => priv[1] === privacy)
21
    const currentPrivacy = privaciesEntries.find((priv) => priv[1] === privacy);
22
    return currentPrivacy?.[0] || ''
22
    return currentPrivacy?.[0] || '';
23
  }, [privacies, privacy])
23
  }, [privacies, privacy]);
24
 
24
 
25
  const privaciesOptions = useMemo(
25
  const privaciesOptions = useMemo(
26
    () => Object.entries(privacies).map(([value, name]) => ({ name, value })),
26
    () => Object.entries(privacies).map(([value, name]) => ({ name, value })),
27
    [privacies]
27
    [privacies]
28
  )
28
  );
29
  const dispatch = useDispatch()
29
  const dispatch = useDispatch();
30
 
30
 
31
  const {
31
  const {
32
    control,
32
    control,
33
    handleSubmit,
33
    handleSubmit,
34
    formState: { errors, isSubmitting }
34
    formState: { errors, isSubmitting }
35
  } = useForm({
35
  } = useForm({
36
    defaultValues: {
36
    defaultValues: {
37
      privacy: ''
37
      privacy: ''
38
    },
38
    },
39
    values: {
39
    values: {
40
      privacy: privacyId
40
      privacy: privacyId
41
    }
41
    }
42
  })
42
  });
43
 
43
 
44
  const onSubmit = handleSubmit(async ({ privacy }) => {
44
  const onSubmit = handleSubmit(async ({ privacy }) => {
45
    const url = `/group/my-groups/privacy/${groupId}`
45
    const url = `/group/my-groups/privacy/${groupId}`;
46
    const formData = new FormData()
46
    const formData = new FormData();
47
    formData.append('privacy', privacy)
47
    formData.append('privacy', privacy);
48
 
48
 
49
    try {
49
    try {
50
      const response = await axios.post(url, formData)
50
      const response = await axios.post(url, formData);
51
      const { data, success } = response.data
51
      const { data, success } = response.data;
52
 
52
 
53
      if (!success) {
53
      if (!success) {
54
        throw new Error('Error al editar el tipo de grupo')
54
        throw new Error('Error al editar el tipo de grupo');
55
      }
55
      }
56
 
56
 
57
      onConfirm(data)
57
      onConfirm(data);
58
      onClose()
58
      onClose();
59
    } catch (error) {
59
    } catch (error) {
60
      dispatch(addNotification({ style: 'danger', msg: error.message }))
60
      dispatch(addNotification({ style: 'danger', msg: error.message }));
61
    }
61
    }
62
  })
62
  });
63
 
63
 
64
  return (
64
  return (
65
    <Modal
65
    <Modal
66
      title='Privacidad'
66
      title='Privacidad'
67
      show={show}
67
      show={show}
68
      onClose={onClose}
68
      onClose={onClose}
69
      onAccept={onSubmit}
69
      onAccept={onSubmit}
70
      loading={isSubmitting}
70
      loading={isSubmitting}
71
    >
71
    >
72
      <Select
72
      <Select
73
        name='privacy'
73
        name='privacy'
74
        control={control}
74
        control={control}
75
        placeholder='Privacidad'
75
        placeholder='Privacidad'
76
        rules={{
76
        rules={{
77
          required: 'Por favor eliga una privacidad'
77
          required: 'Por favor eliga una privacidad'
78
        }}
78
        }}
79
        error={errors.privacy?.message}
79
        error={errors.privacy?.message}
80
        options={privaciesOptions}
80
        options={privaciesOptions}
81
      />
81
      />
82
    </Modal>
82
    </Modal>
83
  )
83
  );
84
}
84
}