Advertisement
ebleach7

HULIO

Jul 2nd, 2024
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { useCallback, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { AntForm } from '@src/features/AntForm';
  4. import { AntFormSelectProps } from '@src/features/AntForm/fields';
  5. import { LanguageQueryKey } from '@shared/components/LanguageSelect';
  6. import { useQueryFetch } from '@shared/hooks/useQueryFetch';
  7.  
  8. const RouteExpeditedArea = ({ ...props }: AntFormSelectProps) => {
  9.     const { t } = useTranslation(['translation']);
  10.     const form = AntForm.useFormInstance();
  11.  
  12.     const stationFromId = AntForm.useWatch('stationFromId', form);
  13.     const stationToId = AntForm.useWatch('stationToId', form);
  14.     const routeTypeId = AntForm.useWatch('routeTypeId', form);
  15.     const expeditionAreas = AntForm.useWatch('expeditionAreas', form);
  16.     const [substitutedCountries, setSubstitutedCountries] = useState<
  17.         { value: string; label: string; disabled: string; color: string }[]
  18.     >([]);
  19.  
  20.     useQueryFetch<
  21.         {
  22.             value: string;
  23.             label: string;
  24.             disabled: string;
  25.             color: string;
  26.         }[]
  27.     >(
  28.         `Dictionaries/Get/Countries?StationId=${stationFromId}&StationId=${stationToId}`,
  29.         {
  30.             queryKey: [LanguageQueryKey, stationFromId, stationToId],
  31.             enabled: !!stationFromId || !!stationToId,
  32.             onSuccess: (data) => {
  33.                 if (!data.data && !Array.isArray(data.data)) return;
  34.  
  35.                 setSubstitutedCountries(data.data);
  36.  
  37.                 const newExpeditionAreas = data.data.map(
  38.                     (country) => country.value,
  39.                 );
  40.  
  41.                 const mapExpeditionAreas =
  42.                     expeditionAreas === undefined
  43.                         ? []
  44.                         : expeditionAreas.map(
  45.                               (
  46.                                   item:
  47.                                       | {
  48.                                             value: string;
  49.                                         }
  50.                                       | string,
  51.                               ) =>
  52.                                   typeof item === 'string' ? item : item.value,
  53.                           );
  54.  
  55.                 const updatedExpeditionAreas = Array.from(
  56.                     new Set([...mapExpeditionAreas, ...newExpeditionAreas]),
  57.                 );
  58.  
  59.                 form.setFieldsValue({
  60.                     expeditionAreas: updatedExpeditionAreas,
  61.                 });
  62.             },
  63.         },
  64.     );
  65.  
  66.     const getLabel = useCallback(
  67.         (value: string): string => {
  68.             const findItem = substitutedCountries.find((item) => {
  69.                 return item.value === value;
  70.             });
  71.  
  72.             if (findItem) return findItem.label;
  73.  
  74.             return value;
  75.         },
  76.         [substitutedCountries],
  77.     );
  78.  
  79.     if (routeTypeId === 'e11d6969-e50e-48bf-afb9-d37e65e7eea6') return null;
  80.  
  81.     console.log('expeditionAreas', expeditionAreas);
  82.  
  83.     return (
  84.         <AntForm.Select
  85.             allowClear
  86.             label={t('translation:expeditedArea')}
  87.             mode='multiple'
  88.             name='expeditionAreas'
  89.             placeholder={t('translation:expeditedArea')}
  90.             rules={[{ required: true }]}
  91.             fieldProps={{
  92.                 labelRender: (value) => {
  93.                     if (value.label) return value.label;
  94.  
  95.                     // eslint-disable-next-line @typescript-eslint/no-explicit-any
  96.                     return getLabel(value.value as any);
  97.                 },
  98.             }}
  99.             query={{
  100.                 url: 'Dictionaries/Get/Countries',
  101.                 queryKey: [LanguageQueryKey],
  102.             }}
  103.             {...props}
  104.         />
  105.     );
  106. };
  107.  
  108. export default RouteExpeditedArea;
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement