Done: requests
This commit is contained in:
@@ -3,8 +3,6 @@ import { Form, Row, Col, Button } from 'react-bootstrap';
|
||||
import { useDataContext } from '../../hooks/useDataContext';
|
||||
import { Alert } from 'react-bootstrap';
|
||||
import PropTypes from 'prop-types';
|
||||
import { generateSecurePassword } from '../../util/passwordGenerator';
|
||||
|
||||
|
||||
const NewUserForm = ({ onSubmit, userType, plotNumber, errors = {} }) => {
|
||||
const { getData } = useDataContext();
|
||||
@@ -12,7 +10,6 @@ const NewUserForm = ({ onSubmit, userType, plotNumber, errors = {} }) => {
|
||||
|
||||
const [form, setForm] = useState({
|
||||
username: '',
|
||||
password: generateSecurePassword(8),
|
||||
displayName: '',
|
||||
dni: '',
|
||||
phone: '',
|
||||
@@ -22,9 +19,7 @@ const NewUserForm = ({ onSubmit, userType, plotNumber, errors = {} }) => {
|
||||
city: '',
|
||||
memberNumber: '',
|
||||
plotNumber: plotNumber,
|
||||
type: userType,
|
||||
status: 1,
|
||||
role: 0
|
||||
type: userType
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -49,7 +49,7 @@ const renderDescripcionSolicitud = (data, onProfile) => {
|
||||
case 1:
|
||||
return onProfile
|
||||
? 'Has solicitado darte de baja.'
|
||||
: `${m?.displayName ?? 'Alguien'} quiere darse de baja.`;
|
||||
: `${data.name ?? 'Alguien'} quiere darse de baja.`;
|
||||
|
||||
case 2:
|
||||
if (onProfile) {
|
||||
@@ -60,19 +60,19 @@ const renderDescripcionSolicitud = (data, onProfile) => {
|
||||
][data.status] ?? 'Solicitud de colaborador.';
|
||||
}
|
||||
return data.status === 0
|
||||
? `${m?.displayName ?? 'Alguien'} quiere añadir un colaborador.`
|
||||
? `${data.name ?? 'Alguien'} quiere añadir un colaborador.`
|
||||
: `La solicitud de colaborador ha sido ${
|
||||
data.status === 1 ? 'aceptada' : 'rechazada'
|
||||
}.`;
|
||||
|
||||
case 3:
|
||||
return `${m?.displayName ?? 'Alguien'} quiere quitar su colaborador.`;
|
||||
return `${data.name ?? 'Alguien'} quiere quitar su colaborador.`;
|
||||
|
||||
case 4:
|
||||
return `${m?.displayName ?? 'Alguien'} quiere una parcela en el invernadero.`;
|
||||
return `${data.name ?? 'Alguien'} quiere una parcela en el invernadero.`;
|
||||
|
||||
case 5:
|
||||
return `${m?.displayName ?? 'Alguien'} quiere dejar su parcela del invernadero.`;
|
||||
return `${data.name ?? 'Alguien'} quiere dejar su parcela del invernadero.`;
|
||||
|
||||
default:
|
||||
return 'Tipo de solicitud desconocido.';
|
||||
@@ -201,7 +201,7 @@ const SolicitudCard = ({
|
||||
|
||||
<ListGroup.Item>
|
||||
<FontAwesomeIcon icon={faSeedling} className="me-2" />
|
||||
Tipo: <strong>{['Lista de espera', 'Hortelano', 'Hortelano + Invernadero', 'Colaborador'][m.type]}</strong>
|
||||
Tipo: <strong>{['Lista de espera', 'Hortelano', 'Hortelano + Invernadero', 'Colaborador', 'Subvenciones', 'Desarrollador'][m.type]}</strong>
|
||||
</ListGroup.Item>
|
||||
</ListGroup>
|
||||
</>
|
||||
|
||||
@@ -62,24 +62,6 @@ export const useData = (config) => {
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const postDataValidated = async (endpoint, payload) => {
|
||||
try {
|
||||
const response = await axios.post(endpoint, payload, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${localStorage.getItem("token")}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
return { ok: true };
|
||||
} catch (err) {
|
||||
return {
|
||||
ok: false,
|
||||
errors: err.response?.data?.errors || {}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const putData = async (endpoint, payload) => {
|
||||
const response = await axios.put(endpoint, payload, {
|
||||
headers: getAuthHeaders(),
|
||||
@@ -111,7 +93,6 @@ export const useData = (config) => {
|
||||
dataError,
|
||||
getData,
|
||||
postData,
|
||||
postDataValidated,
|
||||
putData,
|
||||
deleteData,
|
||||
deleteDataWithBody,
|
||||
|
||||
@@ -26,10 +26,7 @@ const Anuncios = () => {
|
||||
|
||||
const reqConfig = {
|
||||
baseUrl: `${config.apiConfig.baseUrl}${config.apiConfig.endpoints.announcements.all}`,
|
||||
params: {
|
||||
_sort: 'createdAt',
|
||||
_order: 'desc',
|
||||
},
|
||||
params: {},
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -19,10 +19,7 @@ const Documentacion = () => {
|
||||
|
||||
const reqConfig = {
|
||||
baseUrl: config.apiConfig.coreUrl + config.apiConfig.endpoints.files.all,
|
||||
params: {
|
||||
_sort: 'uploadedAt',
|
||||
_order: 'desc'
|
||||
}
|
||||
params: {}
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -29,10 +29,7 @@ const Gastos = () => {
|
||||
|
||||
const reqConfig = {
|
||||
baseUrl: `${config.apiConfig.baseUrl}${config.apiConfig.endpoints.expenses.all}`,
|
||||
params: {
|
||||
_sort: 'createdAt',
|
||||
_order: 'desc',
|
||||
},
|
||||
params: {},
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -32,10 +32,7 @@ const Ingresos = () => {
|
||||
baseUrl: config.apiConfig.baseUrl + config.apiConfig.endpoints.incomes.withInfo,
|
||||
rawUrl: config.apiConfig.baseUrl + config.apiConfig.endpoints.incomes.all,
|
||||
usersUrl: config.apiConfig.baseUrl + config.apiConfig.endpoints.users.all,
|
||||
params: {
|
||||
_sort: 'createdAt',
|
||||
_order: 'desc'
|
||||
}
|
||||
params: {}
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -37,7 +37,7 @@ const ListaEspera = () => {
|
||||
|
||||
const ListaEsperaContent = ({ reqConfig }) => {
|
||||
const { authStatus } = useAuth();
|
||||
const { data, dataLoading, dataError, postData, postDataValidated } = useDataContext();
|
||||
const { data, dataLoading, dataError, postData } = useDataContext();
|
||||
|
||||
const [showWelcomeModal, setShowWelcomeModal] = useState(false);
|
||||
const [showNewUserFormModal, setShowNewUserFormModal] = useState(false);
|
||||
@@ -62,29 +62,34 @@ const ListaEsperaContent = ({ reqConfig }) => {
|
||||
const handleRegisterSubmit = async (formData) => {
|
||||
setValidationErrors({});
|
||||
|
||||
const validation = await postDataValidated(
|
||||
reqConfig.preUserValidationUrl,
|
||||
formData
|
||||
);
|
||||
|
||||
if (!validation.ok) {
|
||||
setValidationErrors(validation.errors);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const request = await postData(reqConfig.requestUrl, { type: 0, status: 0 });
|
||||
const request = await postData(
|
||||
reqConfig.requestUrl,
|
||||
{
|
||||
type: 0,
|
||||
status: 0,
|
||||
metadata: {
|
||||
displayName: formData.displayName,
|
||||
username: formData.username,
|
||||
dni: formData.dni,
|
||||
phone: formData.phone,
|
||||
email: formData.email,
|
||||
memberNumber: formData.memberNumber,
|
||||
plotNumber: formData.plotNumber,
|
||||
type: formData.type,
|
||||
address: formData.address,
|
||||
zipCode: formData.zipCode,
|
||||
city: formData.city
|
||||
}
|
||||
}
|
||||
);
|
||||
const requestId = request?.requestId;
|
||||
if (!requestId) throw new Error("No se pudo registrar la solicitud.");
|
||||
|
||||
await postData(reqConfig.preUsersUrl, {
|
||||
...formData,
|
||||
requestId
|
||||
});
|
||||
|
||||
setShowNewUserFormModal(false);
|
||||
setShowConfirmationModal(true);
|
||||
} catch (err) {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
} catch (_err) {
|
||||
setValidationErrors({ general: "Error inesperado al enviar la solicitud" });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -82,7 +82,7 @@ const Perfil = () => {
|
||||
};
|
||||
|
||||
const PerfilContent = ({ config }) => {
|
||||
const { data, dataLoading, dataError, postData, postDataValidated } = useDataContext();
|
||||
const { data, dataLoading, dataError, postData } = useDataContext();
|
||||
const { logout } = useAuth();
|
||||
|
||||
const identity = JSON.parse(localStorage.getItem("identity"));
|
||||
@@ -102,6 +102,7 @@ const PerfilContent = ({ config }) => {
|
||||
|
||||
const baseMetadata = {
|
||||
displayName: identity.user.displayName,
|
||||
username: identity.account.username,
|
||||
dni: identity.metadata.dni,
|
||||
phone: identity.metadata.phone,
|
||||
email: identity.account.email,
|
||||
@@ -120,6 +121,7 @@ const PerfilContent = ({ config }) => {
|
||||
type,
|
||||
status: CONSTANTS.REQUEST_PENDING,
|
||||
userId: identity.user.userId,
|
||||
name: identity.user.displayName,
|
||||
metadata: baseMetadata
|
||||
});
|
||||
setFeedbackModal({
|
||||
@@ -419,29 +421,38 @@ const PerfilContent = ({ config }) => {
|
||||
plotNumber={identity.metadata.plotNumber}
|
||||
errors={validationErrors}
|
||||
onSubmit={async (formData) => {
|
||||
await postData(config.requestUrl, {
|
||||
type: CONSTANTS.REQUEST_TYPE_ADD_COLLABORATOR,
|
||||
status: CONSTANTS.REQUEST_PENDING,
|
||||
userId: identity.user.userId,
|
||||
metadata: {
|
||||
displayName: formData.displayName,
|
||||
dni: formData.dni,
|
||||
phone: formData.phone,
|
||||
email: formData.email,
|
||||
memberNumber: formData.memberNumber,
|
||||
plotNumber: formData.plotNumber,
|
||||
type: formData.type,
|
||||
role: formData.role
|
||||
}
|
||||
});
|
||||
console.log("🚀 Enviando al backend...", formData); // Debug
|
||||
try {
|
||||
setValidationErrors({});
|
||||
|
||||
setShowAddCollaboratorModal(false);
|
||||
setFeedbackModal({
|
||||
title: 'Solicitud enviada',
|
||||
message: 'El colaborador ha sido solicitado correctamente.',
|
||||
variant: 'success',
|
||||
onClick: closeFeedback
|
||||
});
|
||||
await postData(config.requestUrl, {
|
||||
type: CONSTANTS.REQUEST_TYPE_ADD_COLLABORATOR,
|
||||
status: CONSTANTS.REQUEST_PENDING,
|
||||
userId: identity.user.userId,
|
||||
metadata: {
|
||||
displayName: formData.displayName,
|
||||
username: formData.username,
|
||||
dni: formData.dni,
|
||||
phone: formData.phone,
|
||||
email: formData.email,
|
||||
memberNumber: formData.memberNumber,
|
||||
plotNumber: formData.plotNumber,
|
||||
type: formData.type
|
||||
}
|
||||
});
|
||||
|
||||
setShowAddCollaboratorModal(false);
|
||||
setFeedbackModal({
|
||||
title: 'Solicitud enviada',
|
||||
message: 'El colaborador ha sido solicitado correctamente.',
|
||||
variant: 'success',
|
||||
onClick: closeFeedback
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error("💥 Error al añadir:", error);
|
||||
setValidationErrors({ general: error.message || "Ha ocurrido un error al procesar la solicitud." });
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</CustomModal>
|
||||
|
||||
@@ -31,10 +31,7 @@ const Socios = () => {
|
||||
baseUrl: `${config.apiConfig.baseUrl}${config.apiConfig.endpoints.users.all}`,
|
||||
incomesUrl: `${config.apiConfig.baseUrl}${config.apiConfig.endpoints.users.incomesPreview}`,
|
||||
rawIncomesUrl: `${config.apiConfig.baseUrl}${config.apiConfig.endpoints.incomes.all}`,
|
||||
params: {
|
||||
_sort: "memberNumber",
|
||||
_order: "asc"
|
||||
}
|
||||
params: {}
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -62,6 +62,7 @@ const SolicitudesContent = ({ reqConfig }) => {
|
||||
const url = reqConfig.acceptUrl.replace(":requestId", entry.requestId);
|
||||
try {
|
||||
await putData(url, {});
|
||||
setSearchTerm("");
|
||||
} catch (err) {
|
||||
console.error("❌ Error al aceptar solicitud:", err.message);
|
||||
}
|
||||
@@ -71,11 +72,13 @@ const SolicitudesContent = ({ reqConfig }) => {
|
||||
const url = reqConfig.rejectUrl.replace(":requestId", entry.requestId);
|
||||
try {
|
||||
await putData(url, {});
|
||||
setSearchTerm("");
|
||||
} catch (err) {
|
||||
console.error("❌ Error al rechazar solicitud:", err.message);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const handleDelete = async (id) => {
|
||||
setDeleteTargetId(id);
|
||||
}
|
||||
@@ -88,7 +91,7 @@ const SolicitudesContent = ({ reqConfig }) => {
|
||||
<ContentWrapper>
|
||||
<div className="d-flex flex-column align-items-center m-0 p-0">
|
||||
<h1 className='section-title me-auto'>Panel de Solicitudes</h1>
|
||||
<h5 className='me-auto'>Es necesario asignarle manualmente una contraseña al socio en caso de
|
||||
<h5 className='me-auto'>Es necesario asignarle manualmente una contraseña al socio en caso de
|
||||
aceptar su solicitud tanto de alta como de nuevo colaborador.</h5>
|
||||
</div>
|
||||
|
||||
@@ -106,7 +109,7 @@ const SolicitudesContent = ({ reqConfig }) => {
|
||||
renderCard={(entry, idx) => (
|
||||
<SolicitudCard
|
||||
key={entry.requestId}
|
||||
data={{...entry, idx}}
|
||||
data={{ ...entry, idx }}
|
||||
onAccept={() => handleAccept(entry)}
|
||||
onReject={() => handleReject(entry)}
|
||||
onDelete={handleDelete}
|
||||
|
||||
@@ -6,7 +6,7 @@ import cleanPlugin from 'vite-plugin-clean';
|
||||
export default defineConfig({
|
||||
plugins: [react(), cleanPlugin()],
|
||||
server: {
|
||||
host: "localhost",
|
||||
host: "0.0.0.0",
|
||||
port: 3000,
|
||||
},
|
||||
build: {
|
||||
|
||||
Reference in New Issue
Block a user