fix: use authenticated CSRF-aware request for inspection planning
This commit is contained in:
@@ -4,6 +4,7 @@ import { Link, useNavigate } from 'react-router';
|
|||||||
import { Alert, LoadingBlock } from '../components/Feedback';
|
import { Alert, LoadingBlock } from '../components/Feedback';
|
||||||
import { Icon } from '../components/Icon';
|
import { Icon } from '../components/Icon';
|
||||||
import { SearchableSelect } from '../components/SearchableSelect';
|
import { SearchableSelect } from '../components/SearchableSelect';
|
||||||
|
import { apiRequest } from '../lib/api';
|
||||||
|
|
||||||
interface Option {
|
interface Option {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -23,28 +24,6 @@ interface CreatedInspection {
|
|||||||
code: string;
|
code: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function requestJson<T>(url: string, init?: RequestInit): Promise<T> {
|
|
||||||
const response = await fetch(`/api/v3${url}`, {
|
|
||||||
credentials: 'same-origin',
|
|
||||||
...init,
|
|
||||||
headers: {
|
|
||||||
Accept: 'application/json',
|
|
||||||
...(init?.body ? { 'Content-Type': 'application/json' } : {}),
|
|
||||||
...(init?.headers ?? {}),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const payload = await response.json().catch(() => ({}));
|
|
||||||
if (!response.ok) {
|
|
||||||
const message = typeof payload?.message === 'string'
|
|
||||||
? payload.message
|
|
||||||
: typeof payload?.error?.message === 'string'
|
|
||||||
? payload.error.message
|
|
||||||
: `No se pudo completar la operación (${response.status}).`;
|
|
||||||
throw new Error(message);
|
|
||||||
}
|
|
||||||
return payload as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
function inspectorName(person: Inspector): string {
|
function inspectorName(person: Inspector): string {
|
||||||
return `${person.firstName} ${person.lastName}`.trim() || person.username;
|
return `${person.firstName} ${person.lastName}`.trim() || person.username;
|
||||||
}
|
}
|
||||||
@@ -76,8 +55,8 @@ export function InspectionVisitCreateF61Page() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
Promise.all([
|
Promise.all([
|
||||||
requestJson<{ data: Option[] }>('/inspection-visits/planning-context/departments'),
|
apiRequest<{ data: Option[] }>('/inspection-visits/planning-context/departments'),
|
||||||
requestJson<{ data: Inspector[] }>('/inspection-visits/assignees'),
|
apiRequest<{ data: Inspector[] }>('/inspection-visits/assignees'),
|
||||||
]).then(([departmentResponse, inspectorResponse]) => {
|
]).then(([departmentResponse, inspectorResponse]) => {
|
||||||
setDepartments(departmentResponse.data);
|
setDepartments(departmentResponse.data);
|
||||||
setInspectors(inspectorResponse.data);
|
setInspectors(inspectorResponse.data);
|
||||||
@@ -94,7 +73,7 @@ export function InspectionVisitCreateF61Page() {
|
|||||||
setYacimientos([]);
|
setYacimientos([]);
|
||||||
setOperators([]);
|
setOperators([]);
|
||||||
if (!departmentId) return;
|
if (!departmentId) return;
|
||||||
requestJson<{ data: Option[] }>(`/inspection-visits/planning-context/departments/${departmentId}/areas`)
|
apiRequest<{ data: Option[] }>(`/inspection-visits/planning-context/departments/${departmentId}/areas`)
|
||||||
.then((response) => setAreas(response.data))
|
.then((response) => setAreas(response.data))
|
||||||
.catch((cause) => setError(cause instanceof Error ? cause.message : String(cause)));
|
.catch((cause) => setError(cause instanceof Error ? cause.message : String(cause)));
|
||||||
}, [departmentId]);
|
}, [departmentId]);
|
||||||
@@ -105,7 +84,7 @@ export function InspectionVisitCreateF61Page() {
|
|||||||
setYacimientos([]);
|
setYacimientos([]);
|
||||||
setOperators([]);
|
setOperators([]);
|
||||||
if (!areaId) return;
|
if (!areaId) return;
|
||||||
requestJson<{ data: Option[] }>(`/inspection-visits/planning-context/areas/${areaId}/yacimientos`)
|
apiRequest<{ data: Option[] }>(`/inspection-visits/planning-context/areas/${areaId}/yacimientos`)
|
||||||
.then((response) => setYacimientos(response.data))
|
.then((response) => setYacimientos(response.data))
|
||||||
.catch((cause) => setError(cause instanceof Error ? cause.message : String(cause)));
|
.catch((cause) => setError(cause instanceof Error ? cause.message : String(cause)));
|
||||||
}, [areaId]);
|
}, [areaId]);
|
||||||
@@ -118,7 +97,7 @@ export function InspectionVisitCreateF61Page() {
|
|||||||
if (Number.isNaN(parsedStart.getTime())) return;
|
if (Number.isNaN(parsedStart.getTime())) return;
|
||||||
const at = encodeURIComponent(parsedStart.toISOString());
|
const at = encodeURIComponent(parsedStart.toISOString());
|
||||||
setLoadingOperators(true);
|
setLoadingOperators(true);
|
||||||
requestJson<{ data: Option[] }>(`/inspection-visits/planning-context/areas/${areaId}/operators?at=${at}`)
|
apiRequest<{ data: Option[] }>(`/inspection-visits/planning-context/areas/${areaId}/operators?at=${at}`)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
setOperators(response.data);
|
setOperators(response.data);
|
||||||
if (response.data.length === 1 && response.data[0]) setOperatorId(response.data[0].id);
|
if (response.data.length === 1 && response.data[0]) setOperatorId(response.data[0].id);
|
||||||
@@ -142,7 +121,7 @@ export function InspectionVisitCreateF61Page() {
|
|||||||
|
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
try {
|
try {
|
||||||
const created = await requestJson<CreatedInspection>('/inspection-visits', {
|
const created = await apiRequest<CreatedInspection>('/inspection-visits', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
operationalAreaId: areaId,
|
operationalAreaId: areaId,
|
||||||
|
|||||||
Reference in New Issue
Block a user