@daml/react: Add proper useExerciseByKey hook (#4368)

And remove the old quirky `usePseudoExerciseByKey` hook.

CHANGELOG_BEGIN
CHANGELOG_END
This commit is contained in:
Martin Huschenbett 2020-02-04 15:23:54 +01:00 committed by GitHub
parent 4d7e62de1f
commit 782f7e61d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -112,20 +112,20 @@ export const useExercise = <T extends object, C, R>(choice: Choice<T, C, R>): [(
return [exercise, loading];
}
/// React Hook that returns a function to exercise a choice and a boolean
/// React Hook that returns a function to exercise a choice by key and a boolean
/// indicator whether the exercise is currently running.
export const usePseudoExerciseByKey = <T extends object, C, R>(choice: Choice<T, C, R>): [(key: Query<T>, argument: C) => Promise<R>, boolean] => {
export const useExerciseByKey = <T extends object, C, R, K>(choice: Choice<T, C, R, K>): [(key: K, argument: C) => Promise<R>, boolean] => {
const [loading, setLoading] = useState(false);
const state = useDamlState();
const exercise = async (key: Query<T>, argument: C) => {
const exerciseByKey = async (key: K, argument: C) => {
setLoading(true);
const [result, events] = await state.ledger.exerciseByKey(choice, key, argument);
state.dispatch(addEvents(events));
setLoading(false);
return result;
}
return [exercise, loading];
return [exerciseByKey, loading];
}
/// React Hook to reload all queries currently present in the store.