diff --git a/lib/utils/policy-acknowledge-utlis.js b/lib/utils/policy-acknowledge-utlis.js new file mode 100644 index 000000000..44c348be6 --- /dev/null +++ b/lib/utils/policy-acknowledge-utlis.js @@ -0,0 +1,32 @@ +// @flow + +import { policyAcknowledgmentActionTypes } from '../actions/user-actions.js'; +import type { PolicyType } from '../facts/policies.js'; +import type { PolicyAcknowledgmentRequest } from '../types/account-types.js'; +import type { DispatchActionPromise } from './action-utils.js'; + +function acknowledgePolicy( + policy: PolicyType, + dispatchActionPromise: DispatchActionPromise, + acknowledgmentServerCall: ( + request: PolicyAcknowledgmentRequest, + ) => Promise, + onError: (message: string) => void, +) { + const acknowledgePolicyPromise = (async () => { + try { + await acknowledgmentServerCall({ policy }); + return { policy }; + } catch (e) { + console.warn('error acknowledging policy', e); + onError('unknown error'); + throw e; + } + })(); + dispatchActionPromise( + policyAcknowledgmentActionTypes, + acknowledgePolicyPromise, + ); +} + +export { acknowledgePolicy };