Skip to content

Commit

Permalink
feat: PeriodStat marathon
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-29 committed Nov 26, 2023
1 parent 60b4375 commit ac30acf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/components/LocationStat/PeriodStat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { IS_CHINESE } from '@/utils/const';
import { titleForType } from '@/utils/utils';

const PeriodStat = ({ onClick }: { onClick: (_period: string) => void }) => {
const { workoutsCounts } = useActivities();
const periodArr = Object.entries(workoutsCounts);
const { runPeriod } = useActivities();
const periodArr = Object.entries(runPeriod);
periodArr.sort((a, b) => b[1] - a[1]);
return (
<div style={{ cursor: 'pointer' }}>
Expand Down
9 changes: 2 additions & 7 deletions src/hooks/useActivities.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { locationForRun, titleForRun } from '@/utils/utils';
import {locationForRun, typeForRun} from '@/utils/utils';
import activities from '@/static/activities.json';

const useActivities = () => {
const cities: Record<string, number> = {};
const runPeriod: Record<string, number> = {};
const workoutsCounts: Record<string, number> = {};
const provinces: Set<string> = new Set();
const countries: Set<string> = new Set();
let years: Set<string> = new Set();
Expand All @@ -13,16 +12,13 @@ const useActivities = () => {
activities.forEach((run) => {
const location = locationForRun(run);

const periodName = titleForRun(run);
const periodName = typeForRun(run);
if (periodName) {
runPeriod[periodName] = runPeriod[periodName]
? runPeriod[periodName] + 1
: 1;
}

workoutsCounts[run.type] = workoutsCounts[run.type]
? workoutsCounts[run.type] + 1
: 1;

const { city, province, country } = location;
// drop only one char city
Expand All @@ -45,7 +41,6 @@ const useActivities = () => {
provinces: [...provinces],
cities,
runPeriod,
workoutsCounts,
thisYear,
};
};
Expand Down
46 changes: 44 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface Activity {
name: string;
distance: number;
moving_time: string;
type: 'Run';
type: string;
start_date: string;
start_date_local: string;
location_country: string;
Expand Down Expand Up @@ -225,7 +225,48 @@ const titleForType = (type: string): string => {
}
}

const titleForRun = (run: string): string => {
const typeForRun = (run: Activity): string => {
const type = run.type
switch (type) {
case 'Run':
var runDistance = run.distance / 1000;
if (runDistance >= 40) {
return 'Full Marathon';
}
else if (runDistance > 20) {
return 'Half Marathon';
}
return 'Run';
case 'Trail Run':
return 'Trail Run';
case 'Ride':
return 'Ride';
case 'Indoor Ride':
return 'Indoor Ride';
case 'VirtualRide':
return 'Virtual Ride';
case 'Hike':
return 'Hike';
case 'Rowing':
return 'Rowing';
case 'Swim':
return 'Swim';
case 'RoadTrip':
return 'RoadTrip';
case 'Flight':
return 'Flight';
case 'Kayaking':
return 'Kayaking';
case 'Snowboard':
return 'Snowboard';
case 'Ski':
return 'Ski';
default:
return 'Run';
}
}

const titleForRun = (run: Activity): string => {
const type = run.type;
if (type == 'Run'){
const runDistance = run.distance / 1000;
Expand Down Expand Up @@ -359,6 +400,7 @@ export {
geoJsonForRuns,
geoJsonForMap,
titleForRun,
typeForRun,
titleForType,
filterYearRuns,
filterCityRuns,
Expand Down

0 comments on commit ac30acf

Please sign in to comment.