Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3269 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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