Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
import React from "react";
2
import { useState, useEffect } from "react";
3
import { Button, Modal } from "react-bootstrap";
4
import { useForm } from "react-hook-form";
5
import styled from "styled-components";
6
import {axios} from "../../../../../utils";
7
import FormErrorFeedback from "../../../../../shared/form-error-feedback/FormErrorFeedback";
8
import Spinner from "../../../../../shared/loading-spinner/Spinner";
9
 
10
const StyledSpinnerContainer = styled.div`
11
  position: absolute;
12
  left: 0;
13
  top: 0;
14
  width: 100%;
15
  height: 100%;
16
  background: rgba(255, 255, 255, 0.4);
17
  display: flex;
18
  justify-content: center;
19
  align-items: center;
20
  z-index: 300;
21
`;
22
 
23
const Industry = (props) => {
24
  // props destructuring
25
  const { companyId, industry, industries, addNotification } = props;
26
  // react hook form
27
  const {
28
    register,
29
    errors,
30
    handleSubmit,
31
    setValue,
32
    clearErrors,
33
    getValues,
34
    setError,
35
  } = useForm();
36
 
37
  // states
38
  const [isModalOpen, setIsModalOpen] = useState(false);
39
  const [loading, setLoading] = useState(false);
40
  const [settedIndustry, setSettedIndustry] = useState(industry);
41
  const [settedIndustryKey, setSettedIndustryKey] = useState("");
42
 
43
  useEffect(async () => {
44
    if (isModalOpen === true) {
45
      setLoading(true);
46
      await axios
47
        .get(`/my-company/${companyId}/profile/industry`)
48
        .then((response) => {
49
          const resData = response.data;
50
           (resData);
51
          if (resData.success) {
52
            if (resData.data.industry_id >= 0)
53
              // setSettedIndustryKey(resData.data.industry_id);
54
              setValue("industry_id", resData.data.industry_id);
55
          }
56
        });
57
      setLoading(false);
58
    }
59
  }, [isModalOpen]);
60
 
61
  const handleModalOpen = (event) => {
62
    event && event.preventDefault();
63
    setIsModalOpen(!isModalOpen);
64
  };
65
 
66
  const onSubmitHandler = async (data) => {
67
    // profile/my-profiles/extended', [ 'id' => $user_profile_id_encrypted]
68
    // https://leaderslinked.com/profile/my-profiles/extended/MzU4NDg3ODcg
69
    setLoading(true);
70
    const formData = new FormData();
71
    Object.entries(data).map(([key, value]) => {
72
      formData.append(key, value);
73
    });
74
    await axios
75
      .post(`/my-company/${companyId}/profile/industry`, formData)
76
      .then((response) => {
77
        const resData = response.data;
78
         (resData);
79
        if (resData.success) {
80
          setSettedIndustry(resData.data);
81
          handleModalOpen();
82
        } else {
83
          const resError = resData.data;
84
          if (resError.constructor.name === "Object") {
85
            Object.entries(resError).map(([key, value]) => {
86
              if (key in getValues()) {
87
                setError(key, {
88
                  type: "manual",
89
                  message: Array.isArray(value) ? value[0] : value,
90
                });
91
              }
92
            });
93
          } else {
94
            addNotification({
95
              style: "danger",
96
              msg: resError,
97
            });
98
          }
99
        }
100
      });
101
    setLoading(false);
102
  };
103
 
104
  return (
105
    <React.Fragment>
106
      <div className="user-profile-extended-ov">
107
        <h3>
108
          Industria
109
          <a
110
            href="#"
111
            title=""
112
            className="btn-industry-edit"
113
            onClick={handleModalOpen}
114
          >
115
            <i className="fa fa-pencil"></i>
116
          </a>
117
        </h3>
118
        <span id="overview-industry">{settedIndustry}</span>
119
      </div>
120
 
121
      {/* modal */}
122
      <Modal
123
        show={isModalOpen}
124
        onHide={handleModalOpen}
125
        style={{ overflowY: "scroll" }}
126
      >
127
        <Modal.Header closeButton>
128
          <Modal.Title>Industria</Modal.Title>
129
        </Modal.Header>
130
        <form onSubmit={handleSubmit(onSubmitHandler)}>
131
          <Modal.Body>
132
            <select
133
              name="industry_id"
134
              id="industry_id"
135
              // defaultValue={settedIndustryKey ? settedIndustryKey : ""}
136
              ref={register({
137
                required: "Por favor eliga una industria",
138
              })}
139
            >
140
              <option value="" hidden>
141
                Industria
142
              </option>
143
              {Object.entries(industries).map(([key, value]) => (
144
                <option value={key} key={key}>
145
                  {value}
146
                </option>
147
              ))}
148
            </select>
149
            {errors.industry_id && (
150
              <FormErrorFeedback>
151
                {errors.industry_id.message}
152
              </FormErrorFeedback>
153
            )}
154
          </Modal.Body>
155
          <Modal.Footer>
156
            <Button type="submit">Enviar</Button>
157
            <Button variant="danger" onClick={handleModalOpen}>
158
              Cancel
159
            </Button>
160
          </Modal.Footer>
161
        </form>
162
        {loading && (
163
          <StyledSpinnerContainer>
164
            <Spinner />
165
          </StyledSpinnerContainer>
166
        )}
167
      </Modal>
168
    </React.Fragment>
169
  );
170
};
171
 
172
export default Industry;