Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat: updated useEffect dependency and implemented map
Browse files Browse the repository at this point in the history
state of monthlyIncidents is accurately reflecting incident per month
  • Loading branch information
yosephAHMED committed Aug 20, 2020
1 parent d3cdfa2 commit 2b72f7e
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/incidents/visualize/VisualizeIncidents.tsx
Expand Up @@ -9,6 +9,7 @@ const VisualizeIncidents = () => {
const searchFilter = IncidentFilter.reported
const searchRequest: IncidentSearchRequest = { status: searchFilter }
const { data, isLoading } = useIncidents(searchRequest)
const [incident, setIncident] = useState(0)
const [monthlyIncidents, setMonthlyIncidents] = useState([
// monthlyIncidents[0] -> January ... monthlyIncidents[11] -> December
0,
Expand All @@ -29,23 +30,20 @@ const VisualizeIncidents = () => {
// reportedOn: "2020-08-12T19:53:30.153Z"
Number(reportedOn.slice(5, 7)) - 1

const handleUpdate = (incidentMonth: number) => {
const newMonthlyIncidents = [...monthlyIncidents]
newMonthlyIncidents[incidentMonth] += 1
setMonthlyIncidents(newMonthlyIncidents)
}

useEffect(() => {
if (data === undefined || isLoading) {
// const spinner = <Spinner type="DotLoader" loading />
} else {
const totalIncidents: number = data.length
for (let incident = 0; incident < totalIncidents; incident += 1) {
if (totalIncidents > incident) {
const incidentMonth = getIncidentMonth(data[incident].reportedOn)
handleUpdate(incidentMonth)
setMonthlyIncidents((prevIncidents) =>
prevIncidents.map((value, index) => (index === incidentMonth ? value + 1 : value)),
)
setIncident(incident + 1)
}
}
}, [data])
}, [data, monthlyIncidents])

return (
<>
Expand Down

0 comments on commit 2b72f7e

Please sign in to comment.