Skip to content

Commit 69ac95d

Browse files
authored
Merge pull request #187 from CivicDataLab/186-fix-analytics-page-on-deselecting-all-dates-selected
fix empty date dropdown handling and fallbacks in all views
2 parents 2904255 + 07cccec commit 69ac95d

File tree

3 files changed

+90
-59
lines changed

3 files changed

+90
-59
lines changed

app/[locale]/[state]/analytics/components/analytics-layout.tsx

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ export function AnalyticsMainLayout() {
3838
const searchParams = useSearchParams();
3939
const indicator = searchParams.get('indicator') || '';
4040

41-
const timePeriod = getLatestDate(
42-
searchParams.getAll('time-period') || process.env.NEXT_PUBLIC_TIME_PERIOD
43-
)?.split('-');
41+
const timePeriod = searchParams.get('time-period')
42+
? getLatestDate(searchParams.get('time-period')?.split(',') || [])?.split(
43+
'-'
44+
) || process.env.NEXT_PUBLIC_TIME_PERIOD
45+
: null;
4446

4547
const timePeriodSelected = timePeriod
4648
? `${timePeriod[0]}_${timePeriod[1]}`
@@ -342,52 +344,69 @@ export function AnalyticsMainLayout() {
342344
/>
343345
</div>
344346

345-
{mapData?.isFetching && revenueMapData?.isFetching && (
346-
<div className="flex h-full flex-col place-content-center items-center">
347-
<Spinner color="highlight" />
348-
<Text>Loading...</Text>
347+
{!timePeriod ? (
348+
<div className="flex h-[calc(100dvh_-_400px)] flex-col place-content-center items-center">
349+
<Text>Please select a time period</Text>
349350
</div>
351+
) : (
352+
<>
353+
{mapData?.isFetching && revenueMapData?.isFetching && (
354+
<div className="flex h-full flex-col place-content-center items-center">
355+
<Spinner color="highlight" />
356+
<Text>Loading...</Text>
357+
</div>
358+
)}
359+
360+
{revenueMapData?.data && mapData?.data && (
361+
<MapComponent
362+
indicator={indicator}
363+
mapDataloading={mapData?.isFetching}
364+
revenueMapDataLoading={revenueMapData?.isFetching}
365+
indicatorsData={indicatorsData?.data?.indicators}
366+
setRegion={setDistrictCode}
367+
setRevenueRegion={setRevenueCode}
368+
revenueMapData={revenueMapData?.data?.revCircleMapData}
369+
mapData={mapData?.data?.districtMapData}
370+
currentSelectedState={currentSelectedState}
371+
/>
372+
)}
373+
374+
{region !== null && region.length > 0 && view === 'map' && (
375+
<OutputWindowComponent
376+
currentState={currentSelectedState}
377+
time_period={timePeriodSelected}
378+
/>
379+
)}
380+
</>
350381
)}
351-
352-
{revenueMapData?.data && mapData?.data && (
353-
<MapComponent
354-
indicator={indicator}
355-
mapDataloading={mapData?.isFetching}
356-
revenueMapDataLoading={revenueMapData?.isFetching}
357-
indicatorsData={indicatorsData?.data?.indicators}
358-
setRegion={setDistrictCode}
359-
setRevenueRegion={setRevenueCode}
360-
revenueMapData={revenueMapData?.data?.revCircleMapData}
361-
mapData={mapData?.data?.districtMapData}
382+
</div>
383+
</TabPanel>
384+
<TabPanel value="table">
385+
<div className=" mt-2 h-[calc(100dvh_-_140px)]">
386+
<div>
387+
<FilterDropdownOptions
362388
currentSelectedState={currentSelectedState}
389+
RevCircleDropdownOptions={RevCircleDropdownOptions}
390+
DistrictDropDownOption={DistrictDropDownOption}
391+
timeLimits={timePeriods}
363392
/>
364-
)}
365-
{region !== null && region.length > 0 && view === 'map' && (
366-
<OutputWindowComponent
367-
currentState={currentSelectedState}
368-
time_period={timePeriodSelected}
393+
</div>
394+
{!timePeriod ? (
395+
<div className="flex h-[calc(100dvh_-_400px)] flex-col place-content-center items-center">
396+
<Text>Please select a time period</Text>
397+
</div>
398+
) : (
399+
<TableComponent
400+
data={
401+
filteredTableData?.length > 0
402+
? filteredTableData
403+
: tableData.data?.tableData
404+
}
405+
isLoading={tableData.isLoading}
369406
/>
370407
)}
371408
</div>
372409
</TabPanel>
373-
<TabPanel value="table">
374-
<div>
375-
<FilterDropdownOptions
376-
currentSelectedState={currentSelectedState}
377-
RevCircleDropdownOptions={RevCircleDropdownOptions}
378-
DistrictDropDownOption={DistrictDropDownOption}
379-
timeLimits={timePeriods}
380-
/>
381-
</div>
382-
<TableComponent
383-
data={
384-
filteredTableData?.length > 0
385-
? filteredTableData
386-
: tableData.data?.tableData
387-
}
388-
isLoading={tableData.isLoading}
389-
/>
390-
</TabPanel>
391410
<TabPanel value="chart">
392411
<div className=" mt-2 h-[calc(100dvh_-_140px)]">
393412
<ChartView

app/[locale]/[state]/analytics/components/chart-view.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,20 @@ export const ChartView = ({
160160
};
161161

162162
return (
163-
<div>
164-
<div className=" mt-2 h-[calc(100dvh_-_140px)]">
165-
<FilterDropdownOptions
166-
currentSelectedState={currentSelectedState}
167-
RevCircleDropdownOptions={RevCircleDropdownOptions}
168-
DistrictDropDownOption={DistrictDropDownOption}
169-
timeLimits={timeLimits}
170-
monthMulti={true}
171-
/>
163+
<>
164+
<FilterDropdownOptions
165+
currentSelectedState={currentSelectedState}
166+
RevCircleDropdownOptions={RevCircleDropdownOptions}
167+
DistrictDropDownOption={DistrictDropDownOption}
168+
timeLimits={timeLimits}
169+
monthMulti={true}
170+
/>
172171

172+
{timePeriod.length === 0 ? (
173+
<div className="flex h-[calc(100dvh_-_400px)] flex-col place-content-center items-center">
174+
<Text>Please select a time period</Text>
175+
</div>
176+
) : (
173177
<div className="mt-2 w-full bg-surfaceDefault p-4 pb-0 pt-8 max-sm:p-2">
174178
<Text variant="headingLg" fontWeight="semibold">
175179
{`${
@@ -183,6 +187,7 @@ export const ChartView = ({
183187
{districtCode &&
184188
`${DistrictDropDownOption.find((option) => option.value === districtCode)?.label} District`}
185189
</Text>
190+
186191
{loading ? (
187192
<div className="flex h-[calc(100dvh_-_400px)] flex-col place-content-center items-center">
188193
<Spinner color="highlight" />
@@ -213,7 +218,7 @@ export const ChartView = ({
213218
</div>
214219
)}
215220
</div>
216-
</div>
217-
</div>
221+
)}
222+
</>
218223
);
219224
};

app/[locale]/[state]/analytics/components/filter-dropdown-options.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ export default function FilterDropdownOptions({
105105
// name="time-period-select"
106106
// className="flex-1"
107107
selectedValues={
108-
selectedTimePeriod?.map((timePeriod: string) => {
109-
const [year, month] = timePeriod.split('_');
110-
return parseDate(`${year}-${month.padStart(2, '0')}-01`);
111-
}) || []
108+
selectedTimePeriod
109+
?.filter(Boolean)
110+
?.map((timePeriod: string) => {
111+
const [year, month] = timePeriod.split('_');
112+
return parseDate(`${year}-${month.padStart(2, '0')}-01`);
113+
}) || []
112114
}
113115
label="Select Months"
114116
// minValue={parseDate(minDate || '2023-01-04')}
@@ -125,9 +127,14 @@ export default function FilterDropdownOptions({
125127
) : (
126128
<MonthPicker
127129
name="time-period-select"
128-
defaultValue={parseDate(
129-
getLatestDate(selectedTimePeriod || []) || '2023-08-01'
130-
)}
130+
defaultValue={
131+
selectedTimePeriod &&
132+
selectedTimePeriod?.filter(Boolean)?.length > 0
133+
? parseDate(
134+
getLatestDate(selectedTimePeriod || []) || '2023-08-01'
135+
)
136+
: null
137+
}
131138
label="Select Month"
132139
minValue={parseDate(minDate || '2023-01-04')}
133140
maxValue={parseDate(maxDate || '2023-01-04')}

0 commit comments

Comments
 (0)