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, useRef } 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 UbicationInput from "../../../../../../shared/ubication-input/UbicationInput";
8
import FormErrorFeedback from "../../../../../../shared/form-error-feedback/FormErrorFeedback";
9
import Spinner from "../../../../../../shared/loading-spinner/Spinner";
10
 
11
const StyledSpinnerContainer = styled.div`
12
  position: absolute;
13
  left: 0;
14
  top: 0;
15
  width: 100%;
16
  height: 100%;
17
  background: rgba(255, 255, 255, 0.4);
18
  display: flex;
19
  justify-content: center;
20
  align-items: center;
21
  z-index: 300;
22
`;
23
 
24
const Location = (props) => {
25
  // props destructuring
26
  const { formattedAddress, companyId, jobId, addNotification } = props;
27
 
28
  // React hook form
29
  const {
30
    register,
31
    errors,
32
    handleSubmit,
33
    setValue,
34
    clearErrors,
35
    getValues,
36
    watch,
37
    setError,
38
  } = useForm();
39
 
40
  // states
41
  const [isModalOpen, setIsModalOpen] = useState(false);
42
  const [modalLoading, setModalLoading] = useState(false);
43
  const [settedAddress, setSettedAddress] = useState(formattedAddress);
44
  // refs
45
  const isAddressEmpty = useRef(true);
46
  const addressKeys = useRef([
47
    "address1",
48
    "address2",
49
    "country",
50
    "state",
51
    "city1",
52
    "city2",
53
    "postal_code",
54
    "latitude",
55
    "longitude",
56
  ]);
57
 
58
  useEffect(() => {
59
    addressKeys.current.map((addressKey) => {
60
      register(addressKey);
61
    });
62
    register("formatted_address", {
63
      required: "por favor seleccione una ubicación correcta",
64
    });
65
  }, []);
66
 
67
  const handleModalOpen = (event) => {
68
    event && event.preventDefault();
69
    Object.entries(getValues()).map(([key, value]) => {
70
      setValue(key, "");
71
    });
72
    setIsModalOpen(!isModalOpen);
73
  };
74
 
75
  const handleEdit = async (e) => {
76
    e.preventDefault();
77
    handleModalOpen();
78
    // if (formattedAddress) {
79
    //   await axios
80
    //     .get(`/my-company/${companyId}/job/edit/${jobId}/location`)
81
    //     .then((response) => {
82
    //       const resData = response.data;
83
    //        (resData);
84
    //       if (resData.success) {
85
    //         Object.entries(resData.data).map(async ([key, value]) => {
86
    //           if (value && value !== "n") {
87
    //             setValue(key, value);
88
    //           }
89
    //         });
90
    //         isAddressEmpty.current = false;
91
    //       }
92
    //     });
93
    // }
94
  };
95
 
96
  const getAddressHandler = (addresObject) => {
97
    addressKeys.current.map((addressKey) => {
98
      setValue(addressKey, "");
99
    });
100
    const { address_components } = addresObject;
101
    if (address_components) {
102
      address_components.map((address_component) => {
103
        const address_component_name = address_component.long_name;
104
        const address_component_type = address_component.types[0];
105
        switch (address_component_type) {
106
          case "route":
107
            setValue("address1", address_component_name);
108
            break;
109
          case "sublocality":
110
            setValue("address2", address_component_name);
111
            break;
112
          case "locality":
113
            setValue("city1", address_component_name);
114
            break;
115
          case "administrative_area_level_2":
116
            setValue("city2", address_component_name);
117
            break;
118
          case "administrative_area_level_1":
119
            setValue("state", address_component_name);
120
            break;
121
          case "country":
122
            setValue("country", address_component_name);
123
            break;
124
          case "postal_code":
125
            setValue("postal_code", address_component_name);
126
            break;
127
          case "geometry":
128
            setValue("latitude", address_component.latitude);
129
            setValue("longitude", address_component.longitude);
130
            break;
131
          default:
132
            break;
133
        }
134
      });
135
      isAddressEmpty.current = false;
136
      clearErrors("formatted_address");
137
    } else {
138
      isAddressEmpty.current = true;
139
    }
140
    setValue("formatted_address", addresObject.formatted_address);
141
  };
142
 
143
  const onSubmitHandler = async (data) => {
144
    setModalLoading(true);
145
    const formData = new FormData();
146
    Object.entries(data).map(([key, value]) => {
147
      formData.append(key, value);
148
    });
149
    await axios
150
      .post(`/my-company/${companyId}/job/edit/${jobId}/location`, formData)
151
      .then((response) => {
152
        const resData = response.data;
153
         (resData);
154
        if (resData.success) {
155
          setSettedAddress(resData.data);
156
          handleModalOpen();
157
        } else {
158
          const resError = resData.data;
159
          if (resError.constructor.name === "Object") {
160
            Object.entries(resError).map(([key, value]) => {
161
              if (key in getValues()) {
162
                setError(key, {
163
                  type: "manual",
164
                  message: Array.isArray(value) ? value[0] : value,
165
                });
166
              }
167
            });
168
          } else {
169
            addNotification({
170
              style: "danger",
171
              msg: resError,
172
            });
173
          }
174
        }
175
      });
176
    setModalLoading(false);
177
  };
178
  // https://leaderslinked.com/profile/my-profiles/location/MTI3NjkxNTAg
179
  return (
180
    <React.Fragment>
181
      <div className="user-profile-ov">
182
        <h3 style={{ display: "flex" }}>
183
          Ubicación
184
          <div>
185
            <a
186
              href="#"
187
              title=""
188
              className="btn-location-edit"
189
              onClick={handleEdit}
190
            >
191
              <i className="fa fa-pencil"></i>
192
            </a>
193
          </div>
194
        </h3>
195
        <p id="location-formatted_address">{settedAddress}</p>
196
      </div>
197
 
198
      {/* modal */}
199
      <Modal
200
        show={isModalOpen}
201
        onHide={handleModalOpen}
202
        style={{ overflowY: "scroll" }}
203
      >
204
        <Modal.Header closeButton>
205
          <Modal.Title>Ubicación</Modal.Title>
206
        </Modal.Header>
207
        <form onSubmit={handleSubmit(onSubmitHandler)}>
208
          <Modal.Body>
209
            <div className="form-group datefm">
210
              <UbicationInput
211
                onGetAddress={getAddressHandler}
212
                settedQuery={watch("formatted_address")}
213
              />
214
              <i className="fa fa-map-marker"></i>
215
              {errors.formatted_address && (
216
                <FormErrorFeedback>
217
                  {errors.formatted_address.message}
218
                </FormErrorFeedback>
219
              )}
220
            </div>
221
          </Modal.Body>
222
          <Modal.Footer>
223
            <Button type="submit">Enviar</Button>
224
            <Button variant="danger" onClick={handleModalOpen}>
225
              Cancel
226
            </Button>
227
          </Modal.Footer>
228
        </form>
229
        {modalLoading && (
230
          <StyledSpinnerContainer>
231
            <Spinner />
232
          </StyledSpinnerContainer>
233
        )}
234
      </Modal>
235
    </React.Fragment>
236
  );
237
};
238
 
239
export default Location;