Skip to content

Commit d9fca5b

Browse files
author
Paavan Bhavsar
committedJan 8, 2020
Add documentation
1 parent a998992 commit d9fca5b

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed
 

‎src/library/user-state-manager.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, {useMemo} from 'react'
22
import createPersistedState from 'use-persisted-state';
33
import dayjs from 'dayjs'
44

@@ -9,6 +9,9 @@ import {UserStateActions, UserStateActionsContextProvider} from './user-state-ac
99
const useBirthDateState = createPersistedState('BirthDate', global.sessionStorage);
1010
const useRetireDateState = createPersistedState('RetireDate', global.sessionStorage);
1111

12+
/**
13+
* Helper function to get a Date object equivalent to the start of the date given
14+
*/
1215
function startOfDay(date: Date): Date {
1316
return dayjs(date).startOf('day').toDate()
1417
}
@@ -17,17 +20,21 @@ interface UserStateManagerProps {
1720
children: React.ReactNode
1821
}
1922

23+
/**
24+
* Serve as a multi-context provider for descendent components, providing them with access to
25+
* the user state and a set of actions to mutate that state, while persisting it to session storage.
26+
*/
2027
export default function UserStateManager(props: UserStateManagerProps): JSX.Element {
2128
const {children} = props
2229
const [birthDate, setBirthDate] = useBirthDateState<Date | null>(null)
2330
const [retireDate, setRetireDate] = useRetireDateState<Date | null>(null)
2431

25-
const actions: UserStateActions = {
32+
const actions: UserStateActions = useMemo(() => ({
2633
setBirthDate: date => setBirthDate(startOfDay(date)),
2734
setRetireDate: date => setRetireDate(startOfDay(date)),
28-
}
35+
}), [setBirthDate, setRetireDate])
2936

30-
const userState: UserState = React.useMemo(() => ({
37+
const userState: UserState = useMemo(() => ({
3138
birthDate: birthDate ? new Date(birthDate) : null,
3239
retireDate: retireDate ? new Date(retireDate) : null,
3340
}), [birthDate, retireDate])

0 commit comments

Comments
 (0)
Please sign in to comment.