Skip to content

Commit

Permalink
Merge pull request #173 from tahmid-saj/dev-ts-calories-burned-context
Browse files Browse the repository at this point in the history
migrating calories burned context to ts
  • Loading branch information
tahmid-saj authored Oct 4, 2024
2 parents 3ca8ffc + 5ad7d34 commit 1efc16f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { createContext, useEffect, useState } from "react";
import { createContext, FC, useEffect, useState } from "react";
import { calculateSummary } from "../../../utils/calculations/calories-burned.calculations";
import { validateSearchActivity, validateAddTrackedActivityDate,
validateFilterActivityDates, validateRemoveActivityDate
} from "../../../utils/validations/calories-burned.validations"

import { getSearchActivity } from "../../../utils/api-requests/calories-burned.requests"

import { AddTrackedActivityInput, CaloriesBurnedContextType, CaloriesBurnedProviderProps, FilterConditions, SearchActivityInput, SearchActivityResult, TrackedCaloriesBurned, TrackedCaloriesBurnedSummary } from "./calories-burned.types"

// TODO: sort the records by date
// helper functions
const searchActivityHelper = async (trackedDayInfo) => {
const searchActivityHelper = async (trackedDayInfo: SearchActivityInput): Promise<SearchActivityResult[]> => {
const resActivityResults = await getSearchActivity(trackedDayInfo)

return resActivityResults
}

const addTrackedActivityDateHelper = (trackedCaloriesBurned, trackedDayInfo, activityId) => {
const addTrackedActivityDateHelper = (trackedCaloriesBurned: TrackedCaloriesBurned[],
trackedDayInfo: AddTrackedActivityInput, activityId: number): TrackedCaloriesBurned[] => {
return [ ...trackedCaloriesBurned,
{
dateTracked: String(trackedDayInfo.dateTracked),
Expand All @@ -27,14 +30,14 @@ const addTrackedActivityDateHelper = (trackedCaloriesBurned, trackedDayInfo, act
]
}

const filterActivityDatesHelper = (trackedCaloriesBurned, filterConditions) => {
const filterActivityDatesHelper = (trackedCaloriesBurned: TrackedCaloriesBurned[], filterConditions: FilterConditions): TrackedCaloriesBurned[] => {


let filteredTrackedCaloriesBurned = []
let filteredTrackedCaloriesBurned: TrackedCaloriesBurned[] = []
trackedCaloriesBurned.map((trackedActivity) => {
if (filterConditions.activity === "" || (trackedActivity.activity.toLowerCase().includes(filterConditions.activity.toLowerCase()))) {
if (filterConditions.dateTracked === "" || (filterConditions.dateTracked === trackedActivity.dateTracked)) {
if (filterConditions.durationMinutes === "" || (Number(filterConditions.durationMinutes) === Number(trackedActivity.durationMinutes))) {
if (filterConditions && (filterConditions.dateTracked === "" || (filterConditions.dateTracked === trackedActivity.dateTracked))) {
if (filterConditions && (filterConditions.durationMinutes === "" || (Number(filterConditions.durationMinutes) === Number(trackedActivity.durationMinutes)))) {
filteredTrackedCaloriesBurned.push(trackedActivity)
}
}
Expand All @@ -44,26 +47,24 @@ const filterActivityDatesHelper = (trackedCaloriesBurned, filterConditions) => {
return filteredTrackedCaloriesBurned
}

const removeActivityDateHelper = (trackedCaloriesBurned, activityId) => {
const removeActivityDateHelper = (trackedCaloriesBurned: TrackedCaloriesBurned[], activityId: number): TrackedCaloriesBurned[] => {
if (validateRemoveActivityDate(activityId)) return trackedCaloriesBurned

return trackedCaloriesBurned.filter(trackedActivity => trackedActivity.activityId !== activityId)
}

const selectScheduledTrackedCaloriesBurnedHelper = (trackedCaloriesBurned, trackedDay) => {


const selectScheduledTrackedCaloriesBurnedHelper = (trackedCaloriesBurned: TrackedCaloriesBurned[], trackedDay: string | Date): TrackedCaloriesBurned[] | undefined => {
const filteredTrackedCaloriesBurned = trackedCaloriesBurned.filter((caloriesTrackedDay) => {
return caloriesTrackedDay.dateTracked === trackedDay
})

if (!filteredTrackedCaloriesBurned) return null
if (!filteredTrackedCaloriesBurned) return undefined

return filteredTrackedCaloriesBurned
}

// initial state
export const CaloriesBurnedContext = createContext({
export const CaloriesBurnedContext = createContext<CaloriesBurnedContextType>({
trackedCaloriesBurned: [],
// trackedCaloriesBurned and trackedCaloriesBurnedView structure:
// [
Expand All @@ -80,9 +81,9 @@ export const CaloriesBurnedContext = createContext({
trackedCaloriesBurnedLength: 0,

// selectedTrackedCaloriesBurned is the selected date from the calendar component
selectedTrackedCaloriesBurned: null,
selectedTrackedCaloriesBurned: undefined,

filterConditions: {},
filterConditions: undefined,
// filterConditions structure:
// {
// dateTracked: "",
Expand All @@ -93,7 +94,7 @@ export const CaloriesBurnedContext = createContext({
trackedCaloriesBurnedView: [],

// scheduledTrackedCaloriesBurnedView is the selected selectedTrackedCaloriesBurned info from the calendar component
scheduledTrackedCaloriesBurnedView: null,
scheduledTrackedCaloriesBurnedView: undefined,

searchActivity: () => {},
addTrackedActivityDate: () => {},
Expand All @@ -109,7 +110,7 @@ export const CaloriesBurnedContext = createContext({
// from api
// ]

trackedCaloriesBurnedSummary: {}
trackedCaloriesBurnedSummary: undefined
// trackedCaloriesBurnedSummary structure:
// {
// dailyAverageCaloriesBurned: 300,
Expand All @@ -124,15 +125,15 @@ export const CaloriesBurnedContext = createContext({
})

// context provider
export const CaloriesBurnedProvider = ({ children }) => {
const [trackedCaloriesBurned, setTrackedCaloriesBurned] = useState([])
const [trackedCaloriesBurnedLength, setTrackedCaloriesBurnedLength] = useState(0)
const [filterConditions, setFilterConditions] = useState(null)
const [selectedTrackedCaloriesBurned, setSelectedTrackedCaloriesBurned] = useState(null)
const [scheduledTrackedCaloriesBurnedView, setScheduledTrackedCaloriesBurnedView] = useState(null)
const [searchActivityResults, setSearchActivityResults] = useState([])
const [trackedCaloriesBurnedView, setTrackedCaloriesBurnedView] = useState(trackedCaloriesBurned)
const [trackedCaloriesBurnedSummary, setTrackedCaloriesBurnedSummary] = useState({})
export const CaloriesBurnedProvider: FC<CaloriesBurnedProviderProps> = ({ children }) => {
const [trackedCaloriesBurned, setTrackedCaloriesBurned] = useState<TrackedCaloriesBurned[]>([])
const [trackedCaloriesBurnedLength, setTrackedCaloriesBurnedLength] = useState<number>(0)
const [filterConditions, setFilterConditions] = useState<FilterConditions | undefined>(undefined)
const [selectedTrackedCaloriesBurned, setSelectedTrackedCaloriesBurned] = useState<string | Date | undefined>(undefined)
const [scheduledTrackedCaloriesBurnedView, setScheduledTrackedCaloriesBurnedView] = useState<TrackedCaloriesBurned[] | undefined>(undefined)
const [searchActivityResults, setSearchActivityResults] = useState<SearchActivityResult[]>([])
const [trackedCaloriesBurnedView, setTrackedCaloriesBurnedView] = useState<TrackedCaloriesBurned[]>(trackedCaloriesBurned)
const [trackedCaloriesBurnedSummary, setTrackedCaloriesBurnedSummary] = useState<TrackedCaloriesBurnedSummary | undefined>(undefined)

// update trackedCaloriesBurnedSummary
useEffect(() => {
Expand All @@ -154,7 +155,7 @@ export const CaloriesBurnedProvider = ({ children }) => {

// update trackedCaloriesBurnedView when trackedCaloriesBurned or filterConditions change
useEffect(() => {
if (filterConditions !== null) {
if (filterConditions) {
setTrackedCaloriesBurnedView(filterActivityDatesHelper(trackedCaloriesBurned, filterConditions))
} else {
setTrackedCaloriesBurnedView(trackedCaloriesBurned)
Expand All @@ -166,11 +167,11 @@ export const CaloriesBurnedProvider = ({ children }) => {
if (selectedTrackedCaloriesBurned) {
setScheduledTrackedCaloriesBurnedView(selectScheduledTrackedCaloriesBurnedHelper(trackedCaloriesBurned, selectedTrackedCaloriesBurned))
} else {
setScheduledTrackedCaloriesBurnedView(null)
setScheduledTrackedCaloriesBurnedView(undefined)
}
}, [trackedCaloriesBurned, selectedTrackedCaloriesBurned])

const searchActivity = async (trackedDayInfo) => {
const searchActivity = async (trackedDayInfo: SearchActivityInput): Promise<void> => {
if (validateSearchActivity(trackedDayInfo)) {
return
} else {
Expand All @@ -179,7 +180,7 @@ export const CaloriesBurnedProvider = ({ children }) => {
}
}

const addTrackedActivityDate = (trackedDayInfo) => {
const addTrackedActivityDate = (trackedDayInfo: AddTrackedActivityInput): void => {
if (validateAddTrackedActivityDate(trackedDayInfo)) {
return
} else {
Expand All @@ -189,7 +190,7 @@ export const CaloriesBurnedProvider = ({ children }) => {
}
}

const filterActivityDates = (filterConditions) => {
const filterActivityDates = (filterConditions: FilterConditions): void => {
if (validateFilterActivityDates(filterConditions)) {

return
Expand All @@ -200,16 +201,16 @@ export const CaloriesBurnedProvider = ({ children }) => {
}
}

const removeActivityDate = (activityId) => {
const removeActivityDate = (activityId: number): void => {
setTrackedCaloriesBurned(removeActivityDateHelper(trackedCaloriesBurned, activityId))
}

const clearActivityDatesFilter = () => {
setFilterConditions(null)
setFilterConditions(undefined)
setTrackedCaloriesBurnedView(trackedCaloriesBurned)
}

const selectScheduledTrackedCaloriesBurned = (dayTracked) => {
const selectScheduledTrackedCaloriesBurned = (dayTracked: string | Date): void => {
setSelectedTrackedCaloriesBurned(dayTracked)
setScheduledTrackedCaloriesBurnedView(selectScheduledTrackedCaloriesBurnedHelper(trackedCaloriesBurned, dayTracked))
}
Expand Down
80 changes: 80 additions & 0 deletions src/contexts/signed-out/calories-burned/calories-burned.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

// calories burned types

import { ReactNode } from "react";

export interface CaloriesBurnedContextType {
trackedCaloriesBurned: TrackedCaloriesBurned[];
trackedCaloriesBurnedLength: number;
selectedTrackedCaloriesBurned: TrackedCaloriesBurned | undefined;
filterConditions: FilterConditions | undefined;
trackedCaloriesBurnedView: TrackedCaloriesBurned[];
scheduledTrackedCaloriesBurnedView: TrackedCaloriesBurned[] | undefined;

searchActivity: (trackedDayInfo: SearchActivityInput) => void;
addTrackedActivityDate: (trackedDayInfo: AddTrackedActivityInput) => void;
filterActivityDates: (filterConditions: FilterConditions) => void;
removeActivityDate: (activityId: number) => void;
clearActivityDatesFilter: () => void;

selectScheduledTrackedCaloriesBurned: (dayTracked: string | Date) => void;

searchActivityResults: SearchActivityResult[]

trackedCaloriesBurnedSummary: TrackedCaloriesBurnedSummary | undefined;
}

export interface CaloriesBurnedProviderProps {
children: ReactNode;
}

export type TrackedCaloriesBurned = {
dateTracked: string | Date;
activity: string;
durationMinutes: number;
caloriesBurnedPerHour: number;
totalCaloriesBurned: number;
activityId: number;
}

export type FilterConditions = {
dateTracked: string | Date;
activity: string;
durationMinutes: string;
}

export type SearchActivityResult = {
activity: string;
searchedActivity: string;
dateTracked: string | Date;
caloriesBurnedPerHour: number;
durationMinutes: number;
totalCaloriesBurned: number;
}

export type TrackedCaloriesBurnedSummary = {
dailyAverageCaloriesBurned?: number;
mostCaloriesBurned?: {
date: string | Date;
caloriesBurned: number;
activity: string;
},
totalTrackedDays?: Set<string | Date>;
totalTrackedActivities?: Set<string>;
}

export type SearchActivityInput = {
activity: string;
dateTracked: string | Date;
weightPounds: string;
durationMinutes: string;
}

export type AddTrackedActivityInput = {
activity: string;
searchActivity: string;
dateTracked: string | Date;
caloriesBurnedPerHour: string;
durationMinutes: string;
totalCaloriesBurned: string;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// calories burned calculations

export const calculateSummary = (trackedCaloriesBurned) => {

import { TrackedCaloriesBurned } from "../../contexts/signed-out/calories-burned/calories-burned.types"

export const calculateSummary = (trackedCaloriesBurned: TrackedCaloriesBurned[]) => {
let dailyAverageCaloriesBurned = 0
let mostBurned = {
date: "",
caloriesBurned: 0,
activity: "",
}
let totalTrackedDays = new Set()
let totalTrackedActivities = new Set()
let totalTrackedDays: Set<string | Date> = new Set()
let totalTrackedActivities: Set<string> = new Set()

trackedCaloriesBurned.map((trackedActivity) => {
dailyAverageCaloriesBurned += trackedActivity.totalCaloriesBurned
Expand Down

0 comments on commit 1efc16f

Please sign in to comment.