Advertisement
PastebinhoLuisinho

Untitled

Aug 12th, 2024
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { zodResolver } from '@hookform/resolvers/zod';
  2. import { format } from 'date-fns';
  3. import { useSearchParams } from 'next/navigation';
  4. import { useState } from 'react';
  5. import { useForm } from 'react-hook-form';
  6. import { usePathname, useRouter } from '@/utils/intl-navigation';
  7.  
  8. import { TableFilterSchemaType, tableFilterSchema } from './TableFilter.types'
  9.  
  10. export const actions = (tableId) => {
  11.   const [selectedType, setSelectedType] = useState('all');
  12.   const [selectedStatus, setSelectedStatus] = useState('all');
  13.   const [selectedBlock, setSelectedBlock] = useState('all');
  14.  
  15.   const router = useRouter();
  16.   const pathname = usePathname();
  17.   const searchParams = useSearchParams();
  18.  
  19.   const { register, handleSubmit, control } = useForm<TableFilterSchemaType>({
  20.     resolver: zodResolver(tableFilterSchema),
  21.     values: {
  22.       cpf: searchParams.get('cpf') ?? '',
  23.       date: {
  24.         startDate: searchParams.get('startDate') ? new Date(searchParams.get('startDate')!) : null,
  25.         endDate: searchParams.get('endDate') ? new Date(searchParams.get('endDate')!) : null,
  26.       },
  27.       email: searchParams.get('email') ?? '',
  28.       status: searchParams.get('status') ?? '',
  29.       type: searchParams.get('type') ?? '',
  30.       cognitoId: searchParams.get('cognitoId') ?? '',
  31.       block: searchParams.get('block') ?? '',
  32.     },
  33.   });
  34.  
  35.   const handleSelectFactory = (value, key, callback) => {
  36.     callback(value);
  37.     register(key).onChange({ target: { value } });
  38.   }
  39.  
  40.   const handleFilterSubmit = ({
  41.     cpf,
  42.     date,
  43.     email,
  44.     status,
  45.     type,
  46.     cognitoId,
  47.     block,
  48.   }: TableFilterSchemaType) => {
  49.     const params = new URL(document.location.toString()).searchParams;
  50.     const setParam = (key, value) => {
  51.       value ? params.set(key, value) : params.delete(key);
  52.     }
  53.  
  54.     setParam('cpf', cpf);
  55.     setParam('startDate', date.startDate ? format(date.startDate, 'yyyy-MM-dd HH:mm:ss') : null);
  56.     setParam('endDate', date.endDate ? format(date.endDate, 'yyyy-MM-dd HH:mm:ss') : null);
  57.     setParam('email', email);
  58.     setParam('status', status);
  59.     setParam('type', type);
  60.     setParam('cognitoId', cognitoId);
  61.     setParam('block', block);
  62.  
  63.     params.set(`${tableId}Page`, '0');
  64.     params.set(`${tableId}PageSize`, params.get(`${tableId}PageSize`) || '10');
  65.  
  66.     router.push(`${pathname}?${params.toString()}`);
  67.   };
  68.  
  69.   const handleSelectType = (value: string) => handleSelectFactory(value, 'type', setSelectedType)
  70.   const handleSelectStatus = (value: string) => handleSelectFactory(value, 'status', setSelectedStatus)
  71.   const handleSelectBlock = (value: string) => handleSelectFactory(value, 'block', setSelectedBlock)
  72.  
  73.   return {
  74.     handleFilterSubmit,
  75.     handleSelectType,
  76.     handleSelectStatus,
  77.     handleSelectBlock,
  78.     selectedType,
  79.     selectedStatus,
  80.     selectedBlock,
  81.     handleSubmit,
  82.     control,
  83.     register,
  84.   }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement