Auto permit: searchable investor list with names, only active investors in the chosen area

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Samer Salameh 2026-11-22 15:12:23 +03:00
parent cbef75ea14
commit da8d6bf59a
2 changed files with 26 additions and 6 deletions

View File

@ -2,7 +2,7 @@ import { useState } from 'react';
import { useMutation, useQuery } from '@tanstack/react-query';
import { SectionTitle, sectionFrame } from '../../components/SectionTitle';
import DOMPurify from 'dompurify';
import { Box, Button, Checkbox, FormControlLabel, MenuItem, Paper, Stack, TextField, Typography } from '@mui/material';
import { Autocomplete, Box, Button, Checkbox, FormControlLabel, MenuItem, Paper, Stack, TextField, Typography } from '@mui/material';
import { useAuth } from '../../auth/AuthContext';
import { useNotify } from '../../notify/NotifyProvider';
import { useLabel, useT, useUi } from '../../i18n';
@ -100,11 +100,21 @@ export function SubscriptionDetails({ data, label, id, actions, onChanged }: { d
staleTime: 5 * 60_000,
});
const autoInvestors = useQuery({
queryKey: ['sub-investors', autoEstate],
queryFn: () => getOptions('getInvestorsInLocation', { locationId: autoEstate }),
queryKey: ['sub-active-investors', autoEstate],
// the area's investor list does not look at the investor's status, so it is narrowed to the investors the backend still
// lists as registered (deactivated, blocked and deleted ones are not in that list); both lists use the same investor ids
queryFn: async () => {
const [inArea, registered] = await Promise.all([
getOptions('getInvestorsInLocation', { locationId: autoEstate }),
getOptions('getStakeHoldersList', { STAKEHOLDER_ID: '7', STAKEHOLDER_NAME: 'INVESTORS' }),
]);
const ok = new Set(registered.map((o) => String(o.CODE)));
return inArea.filter((o) => ok.has(String(o.CODE)));
},
enabled: canEditAuto && autoOn && !!autoEstate,
staleTime: 5 * 60_000,
});
const investorOptions = (autoInvestors.data ?? []).map((o) => ({ code: String(o.CODE), label: String(o.NAME || t(o.CAPTION_CODE ?? String(o.CODE))) }));
const saveAuto = useMutation({
meta: { loading: 'saving' },
mutationFn: () => {
@ -196,9 +206,18 @@ export function SubscriptionDetails({ data, label, id, actions, onChanged }: { d
<TextField select size="small" label={lab('INDUSTRIAL_ESTATE')} value={autoEstate} onChange={(e) => { setAutoEstate(e.target.value); setAutoInvestor(''); }}>
{(estates.data ?? []).map((o) => <MenuItem key={String(o.CODE)} value={String(o.CODE)}>{o.NAME || t(o.CAPTION_CODE ?? String(o.CODE))}</MenuItem>)}
</TextField>
<TextField select size="small" label={lab('INDUSTRIAL_ESTATE_STAKEHOLDER_ID')} value={autoInvestor} onChange={(e) => setAutoInvestor(e.target.value)}>
{(autoInvestors.data ?? []).map((o) => <MenuItem key={String(o.CODE)} value={String(o.CODE)}>{o.NAME || t(o.CAPTION_CODE ?? String(o.CODE))}</MenuItem>)}
</TextField>
{/* only the investors located in the chosen industrial area (reloaded when it changes), searchable by name */}
<Autocomplete
size="small"
options={investorOptions}
value={investorOptions.find((o) => o.code === autoInvestor) ?? null}
onChange={(_, o) => setAutoInvestor(o?.code ?? '')}
isOptionEqualToValue={(a, b) => a.code === b.code}
getOptionLabel={(o) => o.label}
loading={autoInvestors.isFetching}
noOptionsText={ui('noResults')}
renderInput={(p) => <TextField {...p} label={ui('investorName')} />}
/>
</Box>
)}
<Box sx={{ mt: 1.5 }}>

View File

@ -43,6 +43,7 @@ export const UI = {
exitFullscreen: { en: 'Exit full screen', ar: 'شاشة مصغرة' },
copy: { en: 'Copy', ar: 'نسخ' },
copied: { en: 'Copied', ar: 'تم النسخ' },
investorName: { en: 'Investor', ar: 'المستثمر' },
close: { en: 'Close', ar: 'إغلاق' },
cachingTitle: { en: 'System cache', ar: 'ذاكرة النظام المؤقتة' },
cachingNote: {