Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3269 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3269 Rev 3432
Línea 1... Línea 1...
1
import React, { useEffect } from 'react'
1
import React, { useEffect } from "react";
2
import { useLocation } from 'react-router-dom'
2
import { useLocation } from "react-router-dom";
3
import { useDispatch } from 'react-redux'
3
import { useDispatch } from "react-redux";
4
import { useForm } from 'react-hook-form'
4
import { useForm } from "react-hook-form";
5
 
5
 
6
import { axios } from '../../utils'
6
import { axios } from "../../utils";
7
import { addNotification } from '../../redux/notification/notification.actions'
7
import { addNotification } from "../../redux/notification/notification.actions";
8
 
8
 
9
import CKEditor from '@components/common/ckeditor/Ckeditor'
9
import CKEditor from "@components/common/ckeditor/Ckeditor";
10
import FormErrorFeedback from 'components/UI/form/FormErrorFeedback'
10
import FormErrorFeedback from "components/UI/form/FormErrorFeedback";
11
import Modal from 'components/UI/modal/Modal'
11
import Modal from "components/UI/modal/Modal";
Línea 12... Línea 12...
12
 
12
 
13
const OverviewModal = ({
13
const OverviewModal = ({
14
  isOpen = false,
14
  isOpen = false,
15
  overview = '',
15
  overview = "",
16
  id = '',
16
  id = "",
17
  closeModal = function () {},
17
  closeModal = function () {},
18
  onComplete = function () {}
18
  onComplete = function () {},
19
}) => {
19
}) => {
20
  const { pathname } = useLocation()
20
  const { pathname } = useLocation();
Línea 21... Línea 21...
21
  const dispatch = useDispatch()
21
  const dispatch = useDispatch();
22
 
22
 
23
  const {
23
  const {
24
    register,
24
    register,
25
    handleSubmit,
25
    handleSubmit,
26
    setValue,
26
    setValue,
Línea 27... Línea 27...
27
    formState: { errors }
27
    formState: { errors },
28
  } = useForm()
28
  } = useForm();
29
 
29
 
30
  const onSubmit = handleSubmit(({ description }) => {
30
  const onSubmit = handleSubmit(({ description }) => {
31
    const typesUrl = {
31
    const typesUrl = {
32
      profile: `/profile/my-profiles/extended/${id}`,
32
      profile: `/profile/my-profiles/extended/${id}`,
Línea 33... Línea 33...
33
      group: `/group/my-groups/extended/${id}`
33
      group: `/group/my-groups/extended/${id}`,
34
    }
34
    };
Línea 35... Línea 35...
35
    const type = pathname.split('/')[1]
35
    const type = pathname.split("/")[1];
36
 
36
 
37
    const formData = new FormData()
37
    const formData = new FormData();
38
    formData.append('description', description)
38
    formData.append("description", description);
39
 
39
 
40
    axios
40
    axios
41
      .post(typesUrl[type], formData)
41
      .post(typesUrl[type], formData)
42
      .then(({ data: responseData }) => {
42
      .then((response) => {
43
        const { data, success } = responseData
43
        const { data, success } = response.data;
44
        if (!success) {
44
        if (!success) {
45
          const errorMessage =
45
          const errorMessage =
46
            typeof data === 'string'
46
            typeof data === "string"
47
              ? data
47
              ? data
Línea 48... Línea 48...
48
              : Object.entries(data).map(
48
              : Object.entries(data).map(
49
                  ([key, value]) => `${key}: ${value}`
49
                  ([key, value]) => `${key}: ${value}`
50
                )[0]
50
                )[0];
51
          throw new Error(errorMessage)
51
          throw new Error(errorMessage);
52
        }
52
        }
53
 
53
 
54
        onComplete(data.description || data)
54
        onComplete(data.description || data);
Línea 55... Línea 55...
55
        closeModal()
55
        closeModal();
56
      })
56
      })
57
      .catch((err) => {
57
      .catch((err) => {
Línea 58... Línea 58...
58
        dispatch(addNotification({ style: 'danger', msg: err.message }))
58
        dispatch(addNotification({ style: "danger", msg: err.message }));
59
      })
59
      });
60
  })
60
  });
61
 
61
 
62
  useEffect(() => {
62
  useEffect(() => {
63
    register('description', { required: 'Este campo es requerido' })
63
    register("description", { required: "Este campo es requerido" });
64
  }, [])
64
  }, []);
65
 
65
 
66
  return (
66
  return (
67
    <Modal
67
    <Modal
68
      title='Visión general'
68
      title="Visión general"
69
      show={isOpen}
69
      show={isOpen}
70
      onClose={closeModal}
70
      onClose={closeModal}
71
      onAccept={onSubmit}
71
      onAccept={onSubmit}
72
    >
72
    >
73
      <CKEditor
73
      <CKEditor
74
        onChange={(value) => setValue('description', value)}
74
        onChange={(value) => setValue("description", value)}
Línea 75... Línea 75...
75
        defaultValue={overview}
75
        defaultValue={overview}