From d7ec10e674c6a96a6ac2fdfb93a78bf8a35adf8d Mon Sep 17 00:00:00 2001 From: j8seangel Date: Fri, 15 Nov 2024 10:11:15 +0100 Subject: [PATCH 1/6] filter soloSinAsignar in server --- src/app/casos-activos/solicitudes/index.tsx | 4 ++- src/app/casos-activos/solicitudes/page.tsx | 38 ++++++++++++++------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/app/casos-activos/solicitudes/index.tsx b/src/app/casos-activos/solicitudes/index.tsx index 914337c..09648fd 100644 --- a/src/app/casos-activos/solicitudes/index.tsx +++ b/src/app/casos-activos/solicitudes/index.tsx @@ -47,7 +47,9 @@ export function Solicitudes({ data, count }: SolicitudesProps) { (filter: FilterType, value: string | number) => { const params = new URLSearchParams(searchParams.toString()); params.set(filter, value.toString()); - router.push(`?${params.toString()}`); + if (filter === 'soloSinAsignar') { + router.push(`?${params.toString()}`); + } }, [searchParams, router], ); diff --git a/src/app/casos-activos/solicitudes/page.tsx b/src/app/casos-activos/solicitudes/page.tsx index ecf0cea..f0c877e 100644 --- a/src/app/casos-activos/solicitudes/page.tsx +++ b/src/app/casos-activos/solicitudes/page.tsx @@ -3,7 +3,8 @@ import { SupabaseClient } from '@supabase/supabase-js'; import { createClient } from '@/lib/supabase/server'; import { Database } from '@/types/database'; import { Solicitudes } from '.'; -import { HelpRequestData } from '@/types/Requests'; +import { helpDataSelectFields, HelpRequestData } from '@/types/Requests'; +import { FiltersData } from './types'; export const dynamic = 'force-dynamic'; @@ -20,12 +21,19 @@ function parseData(data: Database['public']['Tables']['help_requests']['Row'][]) }); } -const getData = async (supabase: SupabaseClient) => { - const { error, data } = await supabase - .from('help_requests') - .select('*') +const getData = async (supabase: SupabaseClient, filters: FiltersData) => { + const query = supabase + .from('help_requests_with_assignment_count') + .select(helpDataSelectFields as '*', { count: 'exact' }) .eq('type', 'necesita') - .order('created_at', { ascending: false }); + .neq('status', 'finished'); + + // Solo agregar filtro si es true + if (filters.soloSinAsignar !== undefined && filters.soloSinAsignar === 'true') { + query.eq('assignments_count', 0); + } + + const { data, error } = await query.order('created_at', { ascending: false }); if (error) { throw new Error('Error fetching solicita:', error); @@ -34,11 +42,16 @@ const getData = async (supabase: SupabaseClient) => { return parseData(data); }; -const getCount = async (supabase: SupabaseClient) => { - const { count: solicitaCount, error: solicitaError } = await supabase - .from('help_requests') +const getCount = async (supabase: SupabaseClient, filters: FiltersData) => { + const query = supabase + .from('help_requests_with_assignment_count') .select('id', { count: 'exact' }) .eq('type', 'necesita'); + // Solo agregar filtro si es true + if (filters.soloSinAsignar !== undefined && filters.soloSinAsignar === 'true') { + query.eq('assignments_count', 0); + } + const { count: solicitaCount, error: solicitaError } = await query; const { count: ofreceCount, error: ofreceError } = await supabase .from('help_requests') @@ -58,10 +71,11 @@ const getCount = async (supabase: SupabaseClient) => { }; }; -export default async function SolicitudesPage() { +export default async function SolicitudesPage(props: { searchParams: Promise> }) { + const searchParams = (await props.searchParams) as FiltersData; const supabase = await createClient(); - const data = await getData(supabase); - const count = await getCount(supabase); + const data = await getData(supabase, searchParams); + const count = await getCount(supabase, searchParams); return ( Date: Fri, 15 Nov 2024 10:21:20 +0100 Subject: [PATCH 2/6] more strict filter search --- src/app/casos-activos/solicitudes/index.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/app/casos-activos/solicitudes/index.tsx b/src/app/casos-activos/solicitudes/index.tsx index 09648fd..369270f 100644 --- a/src/app/casos-activos/solicitudes/index.tsx +++ b/src/app/casos-activos/solicitudes/index.tsx @@ -19,7 +19,11 @@ function getDataFiltered(data: HelpRequestData[], filters: DataFilter[]) { return data; } - return filters.reduceRight((results, { value, keys }) => matchSorter(results, value, { keys }), data); + return filters.reduceRight( + (results, { value, keys }) => + matchSorter(results, value, { keys, threshold: matchSorter.rankings.WORD_STARTS_WITH }), + data, + ); } type SolicitudesProps = { From cb8fb88b7363a5f1114f59afa2828d6c3e3569dc Mon Sep 17 00:00:00 2001 From: j8seangel Date: Fri, 15 Nov 2024 10:33:03 +0100 Subject: [PATCH 3/6] highlight matched search text --- src/components/solicitudes/SolicitudCard.tsx | 27 ++++++++++++++++++-- src/components/solicitudes/SolicitudList.tsx | 8 +++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/components/solicitudes/SolicitudCard.tsx b/src/components/solicitudes/SolicitudCard.tsx index e49c899..01c988d 100644 --- a/src/components/solicitudes/SolicitudCard.tsx +++ b/src/components/solicitudes/SolicitudCard.tsx @@ -10,7 +10,7 @@ import DeleteHelpRequest from '../DeleteHelpRequest'; import { textWithEllipsis } from '@/helpers/utils'; import { useTowns } from '@/context/TownProvider'; import { useRole } from '@/context/RoleProvider'; -import { useState } from 'react'; +import { Fragment, useState } from 'react'; import ChangeUrgencyHelpRequest from '../ChangeUrgencyHelpRequest'; import ChangeStatusButton from '../ChangeStatusButton'; import ChangeCRMStatus from '../ChangeCRMStatus'; @@ -18,11 +18,31 @@ import { UserRoles } from '@/helpers/constants'; import CRMNotes from '@/components/CRMNotes'; import CRMLog from '@/components/CRMLog'; +export const getHighlightedText = (text: string, highlight: string) => { + if (highlight === '') return text; + const regEscape = (v: string) => v.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); + const textChunks = text.split(new RegExp(regEscape(highlight), 'ig')); + let sliceIdx = 0; + return textChunks.map((chunk, index) => { + const currentSliceIdx = sliceIdx + chunk.length; + sliceIdx += chunk.length + highlight.length; + return ( + + {chunk} + {currentSliceIdx < text.length && ( + {text.slice(currentSliceIdx, sliceIdx)} + )} + + ); + }); +}; + type SolicitudCardProps = { caso: SelectedHelpData; showLink?: boolean; showEdit?: boolean; format?: 'small' | 'large'; + highlightedText?: string; }; export default function SolicitudCard({ @@ -30,6 +50,7 @@ export default function SolicitudCard({ showLink = true, showEdit = false, format = 'large', + highlightedText = '', }: SolicitudCardProps) { const session = useSession(); const role = useRole(); @@ -42,6 +63,8 @@ export default function SolicitudCard({ const isMyRequest = session.user?.id && session.user.id === caso.user_id; const [updateUrgency, setUpdateUrgency] = useState(caso.urgency); const [updateStatus, setUpdateStatus] = useState(caso.status); + + const description = format === 'small' ? textWithEllipsis(caso.description, 250) : caso.description; return ( !deleted && (
@@ -92,7 +115,7 @@ export default function SolicitudCard({

- {format === 'small' ? textWithEllipsis(caso.description, 250) : caso.description} + {description && getHighlightedText(description, highlightedText)}

diff --git a/src/components/solicitudes/SolicitudList.tsx b/src/components/solicitudes/SolicitudList.tsx index c0e84c5..4266411 100644 --- a/src/components/solicitudes/SolicitudList.tsx +++ b/src/components/solicitudes/SolicitudList.tsx @@ -126,7 +126,13 @@ export default function SolicitudList({ data, count, filtersData, onDataFilterCh data={data} itemContent={(_, caso) => (
- +
)} /> From 40cc1cb430c34a767917630ab5675401f15654c7 Mon Sep 17 00:00:00 2001 From: j8seangel Date: Fri, 15 Nov 2024 10:41:53 +0100 Subject: [PATCH 4/6] not render points without coordinates --- src/components/map/map.tsx | 1 - src/components/solicitudes/SolicitudMap.tsx | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/components/map/map.tsx b/src/components/map/map.tsx index 250393b..1dae55d 100644 --- a/src/components/map/map.tsx +++ b/src/components/map/map.tsx @@ -29,7 +29,6 @@ const Map: FC = ({ solicitudes, setSelectedMarker }) => { const onClickHandler = (e: MapLayerMouseEvent) => { if (e.features?.[0]) { - console.log('e.features?.[0]:', e.features?.[0].properties); toggleModal(MAP_MODAL_NAME, true); setSelectedMarker(e.features?.[0].properties as HelpRequestData); } diff --git a/src/components/solicitudes/SolicitudMap.tsx b/src/components/solicitudes/SolicitudMap.tsx index 56e8d91..54b50e4 100644 --- a/src/components/solicitudes/SolicitudMap.tsx +++ b/src/components/solicitudes/SolicitudMap.tsx @@ -4,7 +4,10 @@ import Map from '@/components/map/map'; import { HelpRequestData } from '@/types/Requests'; import { Dispatch, SetStateAction, useMemo } from 'react'; -function transformHelpRequestToPointFeature(request: any): GeoJSON.Feature { +function transformHelpRequestToPointFeature(request: any): GeoJSON.Feature | [] { + if (!request.latitude || !request.longitude) { + return []; + } return { type: 'Feature', geometry: { @@ -25,7 +28,7 @@ export default function SolicitudList({ data, setSelectedMarker }: SolicitudList () => ({ type: 'FeatureCollection', - features: data.map(transformHelpRequestToPointFeature), + features: data.flatMap(transformHelpRequestToPointFeature), }) as GeoJSON.FeatureCollection, [data], ); From 6846b55312762135e2d230c42edfc42a7a2dfa9a Mon Sep 17 00:00:00 2001 From: j8seangel Date: Fri, 15 Nov 2024 10:53:50 +0100 Subject: [PATCH 5/6] fix query --- src/app/casos-activos/mapa/actions.tsx | 32 ---------------------- src/app/casos-activos/solicitudes/page.tsx | 3 +- 2 files changed, 2 insertions(+), 33 deletions(-) delete mode 100644 src/app/casos-activos/mapa/actions.tsx diff --git a/src/app/casos-activos/mapa/actions.tsx b/src/app/casos-activos/mapa/actions.tsx deleted file mode 100644 index b94ab38..0000000 --- a/src/app/casos-activos/mapa/actions.tsx +++ /dev/null @@ -1,32 +0,0 @@ -'use server'; - -import { createClient } from '../../../lib/supabase/server'; - -export type FiltroMapa = { - urgencia: string; - tipoAyuda: string; - pueblo: string; - acepta: string; -}; - -export async function getMapPoints(filter: FiltroMapa) { - const supabase = await createClient(); - const query = supabase.from('help_requests').select('*').eq('type', 'necesita'); - if (filter.tipoAyuda !== 'todas') { - query.contains('help_type', [filter.tipoAyuda]); - } - if (filter.urgencia !== 'todas') { - query.eq('urgency', filter.urgencia); - } - - query.neq('status', 'finished'); - - const { data, error } = await query.order('created_at', { ascending: false }); - - const pickupQuery = supabase.from('collection_points').select('*', { count: 'exact' }); - if (filter.acepta !== 'todos') { - query.contains('accepted_items', [filter.acepta]); - } - - return await pickupQuery.order('created_at', { ascending: false }); -} diff --git a/src/app/casos-activos/solicitudes/page.tsx b/src/app/casos-activos/solicitudes/page.tsx index f0c877e..5198aaa 100644 --- a/src/app/casos-activos/solicitudes/page.tsx +++ b/src/app/casos-activos/solicitudes/page.tsx @@ -24,7 +24,7 @@ function parseData(data: Database['public']['Tables']['help_requests']['Row'][]) const getData = async (supabase: SupabaseClient, filters: FiltersData) => { const query = supabase .from('help_requests_with_assignment_count') - .select(helpDataSelectFields as '*', { count: 'exact' }) + .select('*', { count: 'exact' }) .eq('type', 'necesita') .neq('status', 'finished'); @@ -34,6 +34,7 @@ const getData = async (supabase: SupabaseClient, filters: FiltersData) } const { data, error } = await query.order('created_at', { ascending: false }); + console.log('🚀 ~ getData ~ data:', data) if (error) { throw new Error('Error fetching solicita:', error); From 547960af9ac9aae7159b1732e7f5270177f16609 Mon Sep 17 00:00:00 2001 From: j8seangel Date: Fri, 15 Nov 2024 15:50:00 +0100 Subject: [PATCH 6/6] remove log --- src/app/casos-activos/solicitudes/page.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app/casos-activos/solicitudes/page.tsx b/src/app/casos-activos/solicitudes/page.tsx index 5198aaa..4c6aac7 100644 --- a/src/app/casos-activos/solicitudes/page.tsx +++ b/src/app/casos-activos/solicitudes/page.tsx @@ -34,7 +34,6 @@ const getData = async (supabase: SupabaseClient, filters: FiltersData) } const { data, error } = await query.order('created_at', { ascending: false }); - console.log('🚀 ~ getData ~ data:', data) if (error) { throw new Error('Error fetching solicita:', error);