Skip to content

Commit

Permalink
Upload workouts once the Activity screen is mounted
Browse files Browse the repository at this point in the history
To guarantee that all workourts in the state tree are in the DB
  • Loading branch information
dmitryashutov committed Nov 23, 2023
1 parent 7b15688 commit 74731b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/screens/ActivityFeed/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { Activity, describeAcitivitySummary, makeActivitiesCalendar } from './ut
import { StateTree } from 'models';
import { ActivitySummary, Workout } from 'models/activity';
import { ServiceStatus } from 'models/service-status';
import { useService } from 'fhir-react/src/hooks/service';
import { getUserIdentity } from 'services/auth';
import { uploadWorkoutHistory } from 'services/datastream';

export type ActivityFeedItem = Activity;

Expand All @@ -17,6 +20,13 @@ export interface ActivityFeedSection {
}

export function useActivityFeed(activity: StateTree['activity'], serviceStatus: StateTree['serviceStatus']) {
useService(async () => {
const identity = await getUserIdentity();
const uploadWorkoutsResponse = await uploadWorkoutHistory(identity?.jwt, activity.workouts);

return uploadWorkoutsResponse;
}, []);

return {
activities: convertToActivitySections(activity.workouts, activity.summary),
isRunning: serviceStatus.healthkit === ServiceStatus.Running ? true : false,
Expand Down
2 changes: 1 addition & 1 deletion src/services/datastream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function attachActivityHistoryDataStream() {
});
}

async function uploadWorkoutHistory(token: string | undefined, workouts: Workout[]) {
export async function uploadWorkoutHistory(token: string | undefined, workouts: Workout[]) {
return fetchService(DATASTREAM_API_URL, {
method: 'POST',
headers: {
Expand Down

0 comments on commit 74731b0

Please sign in to comment.