AutorÃa | Ultima modificación | Ver Log |
import React from "react";
import { useState, useEffect } from "react";
import { Button, Modal } from "react-bootstrap";
import { useForm } from "react-hook-form";
import styled from "styled-components";
import {axios} from "../../../../../utils";
import FormErrorFeedback from "../../../../../shared/form-error-feedback/FormErrorFeedback";
import Spinner from "../../../../../shared/loading-spinner/Spinner";
const StyledSpinnerContainer = styled.div`
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.4);
display: flex;
justify-content: center;
align-items: center;
z-index: 300;
`;
const Industry = (props) => {
// props destructuring
const { companyId, industry, industries, addNotification } = props;
// react hook form
const {
register,
errors,
handleSubmit,
setValue,
clearErrors,
getValues,
setError,
} = useForm();
// states
const [isModalOpen, setIsModalOpen] = useState(false);
const [loading, setLoading] = useState(false);
const [settedIndustry, setSettedIndustry] = useState(industry);
const [settedIndustryKey, setSettedIndustryKey] = useState("");
useEffect(async () => {
if (isModalOpen === true) {
setLoading(true);
await axios
.get(`/my-company/${companyId}/profile/industry`)
.then((response) => {
const resData = response.data;
(resData);
if (resData.success) {
if (resData.data.industry_id >= 0)
// setSettedIndustryKey(resData.data.industry_id);
setValue("industry_id", resData.data.industry_id);
}
});
setLoading(false);
}
}, [isModalOpen]);
const handleModalOpen = (event) => {
event && event.preventDefault();
setIsModalOpen(!isModalOpen);
};
const onSubmitHandler = async (data) => {
// profile/my-profiles/extended', [ 'id' => $user_profile_id_encrypted]
// https://leaderslinked.com/profile/my-profiles/extended/MzU4NDg3ODcg
setLoading(true);
const formData = new FormData();
Object.entries(data).map(([key, value]) => {
formData.append(key, value);
});
await axios
.post(`/my-company/${companyId}/profile/industry`, formData)
.then((response) => {
const resData = response.data;
(resData);
if (resData.success) {
setSettedIndustry(resData.data);
handleModalOpen();
} else {
const resError = resData.data;
if (resError.constructor.name === "Object") {
Object.entries(resError).map(([key, value]) => {
if (key in getValues()) {
setError(key, {
type: "manual",
message: Array.isArray(value) ? value[0] : value,
});
}
});
} else {
addNotification({
style: "danger",
msg: resError,
});
}
}
});
setLoading(false);
};
return (
<React.Fragment>
<div className="user-profile-extended-ov">
<h3>
Industria
<a
href="#"
title=""
className="btn-industry-edit"
onClick={handleModalOpen}
>
<i className="fa fa-pencil"></i>
</a>
</h3>
<span id="overview-industry">{settedIndustry}</span>
</div>
{/* modal */}
<Modal
show={isModalOpen}
onHide={handleModalOpen}
style={{ overflowY: "scroll" }}
>
<Modal.Header closeButton>
<Modal.Title>Industria</Modal.Title>
</Modal.Header>
<form onSubmit={handleSubmit(onSubmitHandler)}>
<Modal.Body>
<select
name="industry_id"
id="industry_id"
// defaultValue={settedIndustryKey ? settedIndustryKey : ""}
ref={register({
required: "Por favor eliga una industria",
})}
>
<option value="" hidden>
Industria
</option>
{Object.entries(industries).map(([key, value]) => (
<option value={key} key={key}>
{value}
</option>
))}
</select>
{errors.industry_id && (
<FormErrorFeedback>
{errors.industry_id.message}
</FormErrorFeedback>
)}
</Modal.Body>
<Modal.Footer>
<Button type="submit">Enviar</Button>
<Button variant="danger" onClick={handleModalOpen}>
Cancel
</Button>
</Modal.Footer>
</form>
{loading && (
<StyledSpinnerContainer>
<Spinner />
</StyledSpinnerContainer>
)}
</Modal>
</React.Fragment>
);
};
export default Industry;