Advertisement
PastebinhoLuisinho

Untitled

Aug 12th, 2024
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use client';
  2.  
  3. import { zodResolver } from '@hookform/resolvers/zod';
  4. import { Stack } from '@mui/material';
  5. import { Button, SelectField, TextField } from '@penzack/ui-components';
  6. import { format } from 'date-fns';
  7. import { useSearchParams } from 'next/navigation';
  8. import { useState } from 'react';
  9. import { Controller, useForm } from 'react-hook-form';
  10. import InputMask from 'react-input-mask';
  11. import { z } from 'zod';
  12.  
  13. import {
  14.   optionsLabelBlock,
  15.   optionsLabelStatus,
  16.   optionsLabelWithdrawStatus,
  17.   optionsTypeUser,
  18.   validStatusList,
  19.   validTypeList,
  20. } from '@/consts/tableFilter';
  21. import { theme } from '@/theme';
  22. import { usePathname, useRouter } from '@/utils/intl-navigation';
  23.  
  24. import { DateRangePicker } from '../DateRangePicker';
  25.  
  26. const tableFilterSchema = z.object({
  27.   date: z.object({
  28.     startDate: z.date().nullable(),
  29.     endDate: z.date().nullable(),
  30.   }),
  31.   type: z.string().optional(),
  32.   cpf: z.string().optional(),
  33.   email: z.string().optional(),
  34.   status: z.string().optional(),
  35.   cognitoId: z.string().optional(),
  36.   block: z.string().optional(),
  37. });
  38.  
  39. type TableFilterSchemaType = z.infer<typeof tableFilterSchema>;
  40.  
  41. type TableFilterProps = {
  42.   showStatusField?: boolean;
  43.   showUserType?: boolean;
  44.   showWithdrawalOptions?: boolean;
  45.   showBlockOptions?: boolean;
  46.   tableId: string;
  47. };
  48.  
  49. export const TableFilter = ({
  50.   showStatusField,
  51.   showUserType = true,
  52.   showWithdrawalOptions = false,
  53.   showBlockOptions = false,
  54.   tableId,
  55. }: TableFilterProps) => {
  56.   const [selectedType, setSelectedType] = useState('all');
  57.   const [selectedStatus, setSelectedStatus] = useState('all');
  58.   const [selectedBlock, setSelectedBlock] = useState('all');
  59.  
  60.   const router = useRouter();
  61.   const pathname = usePathname();
  62.   const searchParams = useSearchParams();
  63.  
  64.   const { register, handleSubmit, control } = useForm<TableFilterSchemaType>({
  65.     resolver: zodResolver(tableFilterSchema),
  66.     values: {
  67.       cpf: searchParams.get('cpf') ?? '',
  68.       date: {
  69.         startDate: searchParams.get('startDate') ? new Date(searchParams.get('startDate')!) : null,
  70.         endDate: searchParams.get('endDate') ? new Date(searchParams.get('endDate')!) : null,
  71.       },
  72.       email: searchParams.get('email') ?? '',
  73.       status: searchParams.get('status') ?? '',
  74.       type: searchParams.get('type') ?? '',
  75.       cognitoId: searchParams.get('cognitoId') ?? '',
  76.       block: searchParams.get('block') ?? '',
  77.     },
  78.   });
  79.  
  80.   const handleFilterSubmit = ({
  81.     cpf,
  82.     date,
  83.     email,
  84.     status,
  85.     type,
  86.     cognitoId,
  87.     block,
  88.   }: TableFilterSchemaType) => {
  89.     const params = new URL(document.location.toString()).searchParams;
  90.  
  91.     cpf ? params.set('cpf', cpf) : params.delete('cpf');
  92.  
  93.     date.startDate
  94.       ? params.set('startDate', format(date.startDate, 'yyyy-MM-dd HH:mm:ss'))
  95.       : params.delete('startDate');
  96.     date.endDate
  97.       ? params.set('endDate', format(date.endDate, 'yyyy-MM-dd HH:mm:ss'))
  98.       : params.delete('endDate');
  99.     email ? params.set('email', email) : params.delete('email');
  100.  
  101.     const checkStatus = status && validStatusList.includes(status);
  102.     !!checkStatus ? params.set('status', status) : params.delete('status');
  103.  
  104.     const checkType = type && validTypeList.includes(type);
  105.     !!checkType ? params.set('type', type) : params.delete('type');
  106.  
  107.     cognitoId ? params.set('cognitoId', cognitoId) : params.delete('cognitoId');
  108.  
  109.     const checkBlock = block && selectedBlock.includes(block);
  110.     !!checkBlock ? params.set('block', block) : params.delete('block');
  111.  
  112.     params.set(`${tableId}Page`, '0');
  113.     params.set(`${tableId}PageSize`, params.get(`${tableId}PageSize`) || '10');
  114.  
  115.     router.push(`${pathname}?${params.toString()}`);
  116.   };
  117.  
  118.   const handleSelectType = (value: string) => {
  119.     setSelectedType(value);
  120.     register('type').onChange({ target: { value } });
  121.   };
  122.  
  123.   const handleSelectStatus = (value: string) => {
  124.     setSelectedStatus(value);
  125.     register('status').onChange({ target: { value } });
  126.   };
  127.  
  128.   const handleSelectBlock = (value: string) => {
  129.     setSelectedBlock(value);
  130.     register('block').onChange({ target: { value } });
  131.   };
  132.  
  133.   return (
  134.     <form
  135.       onSubmit={handleSubmit(handleFilterSubmit)}
  136.       style={{
  137.         display: 'flex',
  138.         flexDirection: 'row',
  139.         gap: theme.spacing(4),
  140.         padding: `${theme.spacing(3)}px ${theme.spacing(4)}px`,
  141.       }}
  142.     >
  143.       <Stack direction="row" alignItems="center" gap={2} flex={1} flexWrap="wrap">
  144.         <Controller
  145.           control={control}
  146.           name="date"
  147.           render={({ field }) => <DateRangePicker value={field.value} setValue={field.onChange} />}
  148.         />
  149.         <TextField
  150.           {...register('cognitoId')}
  151.           label=""
  152.           placeholder="Cognito ID"
  153.           name="cognitoId"
  154.           sx={{ width: '100px' }}
  155.         />
  156.         {showUserType && (
  157.           <SelectField
  158.             {...register('type')}
  159.             onChange={(value) => handleSelectType(value)}
  160.             placeholder="Type of user"
  161.             label="Type of user"
  162.             value={selectedType}
  163.             options={optionsTypeUser}
  164.             sx={{ width: '130px' }}
  165.           />
  166.         )}
  167.         <InputMask
  168.           mask={'999.999.999-99'}
  169.           alwaysShowMask={false}
  170.           maskPlaceholder=""
  171.           type={'text'}
  172.           placeholder="000.000.000-00"
  173.           sx={{ width: '160px' }}
  174.           {...register('cpf')}
  175.         >
  176.           {(inputProps) => <TextField {...inputProps} />}
  177.         </InputMask>
  178.         <TextField
  179.           {...register('email')}
  180.           label=""
  181.           placeholder="Email"
  182.           name="email"
  183.           sx={{ width: '200px' }}
  184.         />
  185.         {showStatusField && (
  186.           <SelectField
  187.             {...register('status')}
  188.             onChange={(value) => handleSelectStatus(value)}
  189.             placeholder="Status"
  190.             label="Status"
  191.             value={selectedStatus}
  192.             options={showWithdrawalOptions ? optionsLabelWithdrawStatus : optionsLabelStatus}
  193.             sx={{ width: '140px' }}
  194.           />
  195.         )}
  196.         {showBlockOptions && (
  197.           <SelectField
  198.             {...register('block')}
  199.             onChange={(value) => handleSelectBlock(value)}
  200.             placeholder="Type of block"
  201.             label="Type of block"
  202.             value={selectedBlock}
  203.             options={optionsLabelBlock}
  204.             sx={{ width: '170px' }}
  205.           />
  206.         )}
  207.       </Stack>
  208.       <Button type="submit" variant="outlined" sx={{ minWidth: 105 }}>
  209.         Search
  210.       </Button>
  211.     </form>
  212.   );
  213. };
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement