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 DateTimeInput from "../../../../../../shared/datetime-input/DateTimeInput";
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 LastDateOfApplication = (props) => {
// props destructuring
const { companyId, jobId, lastDateOfApplication, 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 [settedDate, setSettedDate] = useState("");
const [settedDateFormatted, setSettedDateFormatted] = useState(
lastDateOfApplication
);
useEffect(() => {
register("last_date_of_application", {
required: "Por favor seleccione una fecha",
});
}, []);
const handleEdit = async () => {
setLoading(true);
handleModalOpen();
await axios
.get(
`/my-company/${companyId}/job/edit/${jobId}/last-date-of-application`
)
.then((response) => {
const resData = response.data;
if (resData.success) {
setSettedDate(resData.data);
}
});
setLoading(false);
};
const handleModalOpen = (event) => {
event && event.preventDefault();
setIsModalOpen(!isModalOpen);
};
const onSubmitHandler = async (data) => {
setLoading(true);
const formData = new FormData();
Object.entries(data).map(([key, value]) => {
formData.append(key, value);
});
await axios
.post(
`/my-company/${companyId}/job/edit/${jobId}/last-date-of-application`,
formData
)
.then((response) => {
const resData = response.data;
(resData);
if (resData.success) {
setSettedDateFormatted(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);
};
const handleDateChange = (date) => {
clearErrors("last_date_of_application");
setValue("last_date_of_application", date);
};
return (
<React.Fragment>
<div className="user-profile-ov st2">
<h3>
Último día de aplicación
<a
href="#"
title=""
className="btn-last-date-of-application-edit"
onClick={(e) => {
e.preventDefault();
handleEdit();
}}
>
<i className="fa fa-pencil"></i>
</a>
<a href="#" title="" className="esp-bx-open"></a>
</h3>
<span id="overview-last-date-of-application">
{settedDateFormatted}
</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>
<DateTimeInput
onChange={handleDateChange}
settedDate={settedDate}
/>
{errors.last_date_of_application && (
<FormErrorFeedback>
{errors.last_date_of_application.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 LastDateOfApplication;