Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date filter behavior #94

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/filters/DateFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const FilterDate = ({ id, dispatch }: filterProps) => {
const [isDateFilterSelectOpen, setIsDateFilterSelectOpen] = useState(false)

useEffect(() => {
if (!date1) {
return
}
switch (selectedDateFilter) {
case DateFilterOption.IS:
dispatch({ type: 'UPDATE_FILTER', payload: { id: id, operation: (incident) => incident.dateString === date1 } })
Expand All @@ -47,6 +50,9 @@ const FilterDate = ({ id, dispatch }: filterProps) => {
dispatch({ type: 'UPDATE_FILTER', payload: { id: id, operation: (incident) => incident.dateString > date1 } })
break
case DateFilterOption.IS_BETWEEN:
if (!date2) {
return
}
dispatch({
type: 'UPDATE_FILTER',
payload: { id: id, operation: (incident) => date1 <= incident.dateString && incident.dateString <= date2 },
Expand Down
76 changes: 39 additions & 37 deletions src/components/graphs/IncidentsStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@ import { DB, Incident } from '@/types'
import { calculateBounds } from '@/utils'
import { useMemo } from 'react'

export function calculateIncidentStats(data: DB, incidents: [string, Incident][]) {
const totalincidents = incidents.length

const countriesSet = new Set<string>()
const departmentsSet = new Set<string>()
const municipalitiesSet = new Set<string>()
const dates: Date[] = []
const typesSet = new Set<string>()

incidents.forEach(([_, incident]) => {
countriesSet.add(incident.country)
departmentsSet.add(incident.department)
municipalitiesSet.add(incident.municipality)
dates.push(new Date(incident.dateString))
typesSet.add(incident.typeID as string)
})

const earliestDate = new Date(Math.min(...dates.map((date) => date.getTime())))
const latestDate = new Date(Math.max(...dates.map((date) => date.getTime())))

const categoriesSet = new Set<string>()
typesSet.forEach((typeID) => {
const categoryID = data.Types[typeID].categoryID
categoriesSet.add(categoryID as string)
})

return {
totalincidents,
countriesCount: countriesSet.size,
departmentsCount: departmentsSet.size,
municipalitiesCount: municipalitiesSet.size,
earliestDate,
latestDate,
categoriesCount: categoriesSet.size,
typesCount: typesSet.size,
}
}

export default function IncidentsStats({
data,
incidents,
Expand All @@ -11,43 +49,7 @@ export default function IncidentsStats({
incidents: [string, Incident][]
bounds: ReturnType<typeof calculateBounds>
}) {
const stats = useMemo(() => {
const totalincidents = bounds.totalCount

const countriesSet = new Set<string>()
const departmentsSet = new Set<string>()
const municipalitiesSet = new Set<string>()
const dates: Date[] = []
const typesSet = new Set<string>()

incidents.forEach(([_, incident]) => {
countriesSet.add(incident.country)
departmentsSet.add(incident.department)
municipalitiesSet.add(incident.municipality)
dates.push(new Date(incident.dateString))
typesSet.add(incident.typeID as string)
})

const earliestDate = new Date(Math.min(...dates.map((date) => date.getTime())))
const latestDate = new Date(Math.max(...dates.map((date) => date.getTime())))

const categoriesSet = new Set<string>()
typesSet.forEach((typeID) => {
const categoryID = data.Types[typeID].categoryID
categoriesSet.add(categoryID as string)
})

return {
totalincidents,
countriesCount: countriesSet.size,
departmentsCount: departmentsSet.size,
municipalitiesCount: municipalitiesSet.size,
earliestDate,
latestDate,
categoriesCount: categoriesSet.size,
typesCount: typesSet.size,
}
}, [incidents, bounds])
const stats = useMemo(() => calculateIncidentStats(data, incidents), [incidents, data])

return (
<div className="relative aspect-[2/1] min-w-[300px] flex-grow overflow-y-auto rounded-lg bg-neutral-100 p-2">
Expand Down
8 changes: 6 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,9 @@ export function filterIncidents(incidents: DB['Incidents'],
}

export function formatDateString(dateString: string): string {
return new Date(dateString).toLocaleDateString('es-ES', { timeZone: 'UTC' })
}
const date = new Date(dateString)
if (isNaN(date.getTime())) {
return 'Fecha inválida'
}
return date.toLocaleDateString('es-ES', { timeZone: 'UTC' })
}
Loading