-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
229 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
import { useRefSync } from './use-ref-sync'; | ||
import type { | ||
LunaticVariablesStore, | ||
LunaticVariablesStoreEvent, | ||
} from '../use-lunatic/commons/variables/lunatic-variables-store'; | ||
import type { LunaticData } from '../use-lunatic/type'; | ||
|
||
/** | ||
* Allow tracking changed while interacting with Lunatic forms | ||
*/ | ||
export function useTrackChanges( | ||
enabled: boolean, | ||
store: LunaticVariablesStore, | ||
getData: (names: string[]) => LunaticData | ||
) { | ||
// Saves the list of changed variable | ||
const changedVariables = useRef(new Set<string>()); | ||
// Use ref to avoid dependencies in useCallback | ||
const enabledRef = useRefSync(enabled); | ||
const getDataRef = useRefSync(getData); | ||
|
||
useEffect(() => { | ||
if (!enabled || !store) { | ||
return; | ||
} | ||
const handleChange = (e: LunaticVariablesStoreEvent<'change'>) => { | ||
changedVariables.current.add(e.detail.name); | ||
}; | ||
store.on('change', handleChange); | ||
return () => store.off('change', handleChange); | ||
}, [enabled, store]); | ||
|
||
// Reset list of changed variables | ||
const resetChangedData = useCallback(() => { | ||
changedVariables.current.clear(); | ||
}, [changedVariables]); | ||
|
||
const getChangedData = useCallback( | ||
(reset: boolean = false) => { | ||
if (!enabledRef.current) { | ||
throw new Error( | ||
'getChangedData() cannot be used without enabling tracking mode, add "trackChanges: true" to useLunatic options' | ||
); | ||
} | ||
const data = getDataRef.current(Array.from(changedVariables.current)); | ||
if (reset) { | ||
resetChangedData(); | ||
} | ||
return data; | ||
}, | ||
[enabledRef, getDataRef, resetChangedData] | ||
); | ||
|
||
return { | ||
getChangedData, | ||
resetChangedData, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters