Skip to content

Commit

Permalink
refactor(app-project): optimistic updates for YourProjectStats
Browse files Browse the repository at this point in the history
- [x] a new module-scoped function, `mutate`, in the `useYourProjectStats` module. `mutate` is generated each time that stats data is fetched from ERAS, and can be used to update the fetched stats.
- [x] a new export from the `useYourProjectStats` module. `updateYourStats` runs an optimistic update of your stats, incrementing both counts when you press Done or Done & Talk, without revalidating the SWR cache.
  • Loading branch information
eatyourgreens committed Dec 20, 2024
1 parent 51f064e commit d2f8c54
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { useAdminMode } from '@hooks'
import addQueryParams from '@helpers/addQueryParams'
import logToSentry from '@helpers/logger/logToSentry.js'
import ErrorMessage from './components/ErrorMessage'
import useYourProjectStats from '../YourProjectStats/useYourProjectStats.js'
import incrementStats from '../YourProjectStats/helpers/incrementStats.js'
import { updateYourStats } from '../YourProjectStats/useYourProjectStats.js'

function onError(error, errorInfo = {}) {
logToSentry(error, errorInfo)
Expand Down Expand Up @@ -61,18 +60,17 @@ export default function ClassifierWrapper({
Add the recently classified subject to the signed-in user's Recents.
*/
const projectID = project?.id
const { mutate } = useYourProjectStats({ projectID, userID })

const addRecents = recents?.add
const onCompleteClassification = useCallback((classification, subject) => {
personalization.incrementSessionCount()
incrementStats(mutate, projectID, userID)
updateYourStats(projectID, userID)
addRecents({
favorite: subject.favorite,
subjectId: subject.id,
locations: subject.locations
})
}, [addRecents, mutate, projectID, userID])
}, [addRecents, projectID, userID])

/*
If the page URL contains a subject ID, update that ID when the classification subject changes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import logToSentry from '@helpers/logger/logToSentry.js'

import { usePanoptesAuthToken } from '@hooks'
import { getTodayDateString, getNumDaysAgoDateString, getQueryPeriod } from './helpers/dateRangeHelpers.js'
import incrementStats from './helpers/incrementStats.js'

const SWROptions = {
revalidateIfStale: true,
Expand All @@ -24,6 +25,8 @@ function statsHost(env) {

const endpoint = '/classifications/users'

let mutate

/* user.created_at is needed for allTimeQuery, and not always available on the logged in user object */
async function fetchUserCreatedAt({ token, userID }) {
const authorization = `Bearer ${token}`
Expand Down Expand Up @@ -96,10 +99,16 @@ async function fetchStats({ endpoint, projectID, userID, token }) {
return null
}

export function updateYourStats(projectID, userID) {
return incrementStats(mutate, projectID, userID)
}

export default function useYourProjectStats({ projectID, userID }) {
const token = usePanoptesAuthToken()

// only fetch stats when a userID is available. Don't fetch if no user signed in.
const key = token && userID ? { endpoint, projectID, userID, token } : null
return useSWR(key, fetchStats, SWROptions)
const fetchedStats = useSWR(key, fetchStats, SWROptions)
mutate = fetchedStats.mutate
return fetchedStats
}

0 comments on commit d2f8c54

Please sign in to comment.