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 from 'react'
1
import React 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 Input from '@components/UI/inputs/Input'
9
import Input from '@components/UI/inputs/Input';
10
 
10
 
11
export default function WebsiteModal({
11
export default function WebsiteModal({
12
  show = false,
12
  show = false,
13
  website = '',
13
  website = '',
14
  groupId,
14
  groupId,
15
  onClose = () => {},
15
  onClose = () => {},
16
  onConfirm = () => {}
16
  onConfirm = () => {}
17
}) {
17
}) {
18
  const dispatch = useDispatch()
18
  const dispatch = useDispatch();
19
 
19
 
20
  const {
20
  const {
21
    control,
21
    control,
22
    handleSubmit,
22
    handleSubmit,
23
    formState: { errors, isSubmitting }
23
    formState: { errors, isSubmitting }
24
  } = useForm({
24
  } = useForm({
25
    defaultValues: {
25
    defaultValues: {
26
      website: ''
26
      website: ''
27
    },
27
    },
28
    values: {
28
    values: {
29
      website
29
      website
30
    }
30
    }
31
  })
31
  });
32
 
32
 
33
  const onSubmit = handleSubmit(async ({ website }) => {
33
  const onSubmit = handleSubmit(async ({ website }) => {
34
    const url = `/group/my-groups/website/${groupId}`
34
    const url = `/group/my-groups/website/${groupId}`;
35
    const formData = new FormData()
35
    const formData = new FormData();
36
    formData.append('website', website)
36
    formData.append('website', website);
37
 
37
 
38
    try {
38
    try {
39
      const response = await axios.post(url, formData)
39
      const response = await axios.post(url, formData);
40
      const { data, success } = response.data
40
      const { data, success } = response.data;
41
 
41
 
42
      if (!success) {
42
      if (!success) {
43
        throw new Error('Error al editar el tipo de grupo')
43
        throw new Error('Error al editar el tipo de grupo');
44
      }
44
      }
45
 
45
 
46
      onConfirm(data)
46
      onConfirm(data);
47
      onClose()
47
      onClose();
48
    } catch (error) {
48
    } catch (error) {
49
      dispatch(addNotification({ style: 'danger', msg: error.message }))
49
      dispatch(addNotification({ style: 'danger', msg: error.message }));
50
    }
50
    }
51
  })
51
  });
52
 
52
 
53
  return (
53
  return (
54
    <Modal
54
    <Modal
55
      title='Sitio web'
55
      title='Sitio web'
56
      show={show}
56
      show={show}
57
      onClose={onClose}
57
      onClose={onClose}
58
      onAccept={onSubmit}
58
      onAccept={onSubmit}
59
      loading={isSubmitting}
59
      loading={isSubmitting}
60
    >
60
    >
61
      <Input control={control} name='website' error={errors.website?.message} />
61
      <Input control={control} name='website' error={errors.website?.message} />
62
    </Modal>
62
    </Modal>
63
  )
63
  );
64
}
64
}