Skip to content

Commit

Permalink
Small logging fix (#710)
Browse files Browse the repository at this point in the history
* Fixing a small bug with accessing undefined

* Fix error with a race condition in logging
  • Loading branch information
jtklein authored Sep 3, 2024
1 parent bd43281 commit 84b3584
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions components/Navigation/hooks/useAppLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import { LogLevels, logToApi } from "../../../utility/apiCalls";
import taxonIds from "../../../utility/dictionaries/taxonDict";
import realmConfig from "../../../models/index";

const runOncePerMonth = async ( callback: () => void ) => {
const executeInNewMonth = async ( callback: () => void, keySubstring: string ) => {
const storageKeyBase = "lastRunMonth";
const storageKey = storageKeyBase + keySubstring;
try {
const lastRun = await AsyncStorage.getItem( "lastRunMonth" );
const lastRun = await AsyncStorage.getItem( storageKey );
const currentMonth = new Date().getMonth();

if ( lastRun === null || parseInt( lastRun, 10 ) !== currentMonth ) {
// Run the function as the month has changed
callback();

// Update the last run month
await AsyncStorage.setItem( "lastRunMonth", currentMonth.toString() );
await AsyncStorage.setItem( storageKey, currentMonth.toString() );
}
} catch ( error ) {
console.error( "Error checking or updating last run month:", error );
Expand Down Expand Up @@ -45,8 +47,8 @@ function useAppLog() {

const levelsEarned = badges.filtered( "iconicTaxonName == null AND earned == true" ).sorted( "count", true );
const level = {
earnedDate: levelsEarned[0].earnedDate,
index: levelsEarned[0].index
earnedDate: levelsEarned[0]?.earnedDate,
index: levelsEarned[0]?.index
};

const achievementsLogState = {
Expand All @@ -56,12 +58,12 @@ function useAppLog() {
await logToApi( {
level: LogLevels.INFO,
message: JSON.stringify( achievementsLogState ),
context: "RootStack achievementsState",
context: "RootStack achievementsState v1",
errorType: "0"
} );
};

runOncePerMonth( logBadgesAppState );
executeInNewMonth( logBadgesAppState, "1" );
}, [] );

useEffect( ( ) => {
Expand All @@ -81,12 +83,12 @@ function useAppLog() {
await logToApi( {
level: LogLevels.INFO,
message: JSON.stringify( challengesLogState ),
context: "RootStack challengesState",
context: "RootStack challengesState v1",
errorType: "0"
} );
};

runOncePerMonth( logChallengesAppState );
executeInNewMonth( logChallengesAppState, "2" );
}, [] );

useEffect( ( ) => {
Expand All @@ -104,12 +106,12 @@ function useAppLog() {
await logToApi( {
level: LogLevels.INFO,
message: JSON.stringify( observationsLogState ),
context: "RootStack observationsState",
context: "RootStack observationsState v1",
errorType: "0"
} );
};

runOncePerMonth( logChallengesAppState );
executeInNewMonth( logChallengesAppState, "3" );
}, [] );
}

Expand Down

0 comments on commit 84b3584

Please sign in to comment.