Rev 1181 | Ir a la última revisión | Autoría | Comparar con el anterior | 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 destructuringconst { groupId, industry, industries, addNotification } = props;// react hook formconst {register,errors,handleSubmit,setValue,clearErrors,getValues,setError,} = useForm();// statesconst [isModalOpen, setIsModalOpen] = useState(false);const [loading, setLoading] = useState(false);const [settedIndustry, setSettedIndustry] = useState(industry);const [settedIndustryKey, setSettedIndustryKey] = useState("");useEffect(() => {axios.get(`/group/my-groups/industry/${groupId}`).then((response) => {const resData = response.data;(resData);if (resData.success) {if (resData.data >= 0) setSettedIndustryKey(resData.data);}});}, [settedIndustry]);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/MzU4NDg3ODcgsetLoading(true);const formData = new FormData();Object.entries(data).map(([key, value]) => {formData.append(key, value);});await axios.post(`/group/my-groups/industry/${groupId}`, 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<ahref="#"title=""className="btn-industry-edit"onClick={handleModalOpen}><i className="fa fa-pencil"></i></a></h3><span id="overview-industry">{settedIndustry}</span></div>{/* modal */}<Modalshow={isModalOpen}onHide={handleModalOpen}style={{ overflowY: "scroll" }}><Modal.Header closeButton><Modal.Title>Visión general</Modal.Title></Modal.Header><form onSubmit={handleSubmit(onSubmitHandler)}><Modal.Body><selectname="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;