Skip to content

Commit

Permalink
Merge pull request #23 from trixtateam/feature/hooks_check_action_rea…
Browse files Browse the repository at this point in the history
…ction_exists

feat: include loading state for reaction and action hooks
  • Loading branch information
jacqueswho authored Jun 22, 2021
2 parents 5fe8c86 + 8c5e56d commit 49accb8
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trixta/trixta-js",
"version": "4.0.5",
"version": "4.0.6",
"description": "Javascript library to integrate Trixta",
"source": "src/index.ts",
"main": "dist/@trixta/trixta-js.cjs.js",
Expand Down
6 changes: 4 additions & 2 deletions src/React/hooks/use-trixta-action-reaction/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ describe('useTrixtaActionReaction', () => {
},
);

expect(result.error).toEqual(Error('Please provide actionName parameter.'));
expect(result.error).toEqual(
Error('Please provide reactionName parameter.'),
);
});

it('should throw error if no reactionName parameter', () => {
Expand Down Expand Up @@ -249,7 +251,7 @@ describe('useTrixtaActionReaction', () => {

expect(result.current.actionResponse).toBeUndefined();
expect(result.current.hasResponse).toBe(false);
expect(result.current.isInProgress).toBe(false);
expect(result.current.loading).toBe(true);

act(() => {
result.current.submitTrixtaActionResponse({ data: {} });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ export const useTrixtaActionReaction = <
TReactionResponseType,
TReactionErrorType
> => {
const { submitTrixtaAction, ...action } = useTrixtaAction<
TActionResponseType,
TActionErrorType
>({ ...actionProps, options: { autoSubmit }, actionParameters });
const reaction = useTrixtaReaction<
TInitialData,
TReactionResponseType,
Expand All @@ -56,14 +52,19 @@ export const useTrixtaActionReaction = <
requestForEffect: reactionProps.requestForEffect,
});

const { submitTrixtaAction, ...action } = useTrixtaAction<
TActionResponseType,
TActionErrorType
>({ ...actionProps, options: { autoSubmit }, actionParameters });

const hasActionResponse = action.hasResponse;
const hasReactionResponse = reaction.hasResponse;

return {
initialData: reaction.initialData,
hasRoleAccess: reaction.hasRoleAccess,
isInProgress: action.isInProgress || reaction.isInProgress,
loading: reaction.loading,
loading: reaction.loading || action.loading,
actionResponse: action.response,
hasActionResponse,
hasReactionResponse,
Expand Down
6 changes: 5 additions & 1 deletion src/React/hooks/use-trixta-action/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TrixtaActionBaseProps } from '../../../React/types/actions';
import {
DefaultUnknownType,
submitTrixtaFunctionParameters,
TrixtaInstance,
TrixtaInstanceResponse,
submitTrixtaFunctionParameters,
} from '../../types/common';
export interface UseTrixtaActionProps extends TrixtaActionBaseProps {
options?: {
Expand Down Expand Up @@ -59,4 +59,8 @@ export interface UseTrixtaActionHookReturn<
* If 'true', there is a Trixta action instance response
*/
hasResponse: boolean;
/**
* If 'true',Trixta action is waiting to be loaded
*/
loading: boolean;
}
29 changes: 22 additions & 7 deletions src/React/hooks/use-trixta-action/use-trixta-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
clearTrixtaActionResponse,
submitTrixtaActionResponse,
} from '../../reduxActions/trixtaActions';
import { makeSelectHasTrixtaRoleAccess } from '../../selectors/common';
import {
makeSelectHasTrixtaRoleAccess,
makeSelectIsTrixtaActionReadyForRole,
makeSelectTrixtaActionRequestStatus,
makeSelectTrixtaActionResponseInstancesForRole,
} from '../../selectors';
} from '../../selectors/trixtaActions';
import {
trixtaDebugger,
TrixtaDebugType,
Expand Down Expand Up @@ -70,6 +71,14 @@ export const useTrixtaAction = <
selectHasRoleAccess(state, { roleName }),
);
const roleActionProps = { roleName, actionName } as TrixtaActionBaseProps;

const selectIsTrixtaActionReady = useMemo(
makeSelectIsTrixtaActionReadyForRole,
[],
);
const isTrixtaActionReady = useSelector<{ trixta: TrixtaState }, boolean>(
(state) => selectIsTrixtaActionReady(state, roleActionProps),
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const selectActionResponses: any = useMemo(
makeSelectTrixtaActionResponseInstancesForRole,
Expand Down Expand Up @@ -119,7 +128,7 @@ export const useTrixtaAction = <
requestEvent,
errorEvent,
}: submitTrixtaFunctionParameters) => {
if (!hasRoleAccess) return;
if (!hasRoleAccess || !isTrixtaActionReady) return;

dispatch(
submitTrixtaActionResponse({
Expand All @@ -132,11 +141,11 @@ export const useTrixtaAction = <
}),
);
},
[dispatch, roleName, actionName, hasRoleAccess],
[dispatch, isTrixtaActionReady, roleName, actionName, hasRoleAccess],
);

useEffect(() => {
if (options.autoSubmit) {
if (options.autoSubmit && isTrixtaActionReady) {
if (
!deequal(actionParamatersRef.current, actionParameters) ||
actionParamatersRef.current === undefined
Expand All @@ -145,7 +154,12 @@ export const useTrixtaAction = <
submitTrixtaAction(actionParameters ?? { data: {} });
}
}
}, [options.autoSubmit, submitTrixtaAction, actionParameters]);
}, [
options.autoSubmit,
submitTrixtaAction,
isTrixtaActionReady,
actionParameters,
]);

const success = latestInstance ? latestInstance.response.success : false;
const error = latestInstance ? latestInstance.response.error : false;
Expand All @@ -162,7 +176,8 @@ export const useTrixtaAction = <
return {
latestInstance,
hasRoleAccess,
isInProgress,
isInProgress: isTrixtaActionReady ? isInProgress : true,
loading: !isTrixtaActionReady,
hasResponse: !!latestInstance,
clearActionResponses,
response: latestInstance?.response,
Expand Down
6 changes: 3 additions & 3 deletions src/React/hooks/use-trixta-reaction/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('useTrixtaReaction', () => {
expect(result.current.latestResponse).toBeUndefined();
expect(result.current.latestInstance).toBeUndefined();
expect(result.current.hasResponse).toBe(false);
expect(result.current.isInProgress).toBe(false);
expect(result.current.isInProgress).toBe(true);

act(() => {
store.dispatch(
Expand Down Expand Up @@ -230,7 +230,7 @@ describe('useTrixtaReaction', () => {
expect(result.current.latestResponse).toBeUndefined();
expect(result.current.latestInstance).toBeUndefined();
expect(result.current.hasResponse).toBe(false);
expect(result.current.isInProgress).toBe(false);
expect(result.current.isInProgress).toBe(true);

act(() => {
store.dispatch(
Expand Down Expand Up @@ -281,7 +281,7 @@ describe('useTrixtaReaction', () => {
expect(result.current.latestResponse).toBeUndefined();
expect(result.current.latestInstance).toBeUndefined();
expect(result.current.hasResponse).toBe(false);
expect(result.current.isInProgress).toBe(false);
expect(result.current.isInProgress).toBe(true);

act(() => {
store.dispatch(
Expand Down
28 changes: 22 additions & 6 deletions src/React/hooks/use-trixta-reaction/use-trixta-reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {
clearTrixtaReactionResponse,
submitTrixtaReactionResponse,
} from '../../reduxActions/trixtaReactions';
import { makeSelectHasTrixtaRoleAccess } from '../../selectors/common';
import {
makeSelectHasTrixtaRoleAccess,
makeSelectIsTrixtaReactionLoading,
makeSelectTrixtaReactionRequestStatus,
makeSelectTrixtaReactionResponseInstancesForRole,
} from '../../selectors';
makesSelectIsTrixtaReactionReadyForRole,
} from '../../selectors/trixtaReactions';
import {
trixtaDebugger,
TrixtaDebugType,
Expand Down Expand Up @@ -75,6 +76,14 @@ export const useTrixtaReaction = <
reactionName,
} as TrixtaReactionBaseProps;
// eslint-disable-next-line @typescript-eslint/no-explicit-any

const selectIsTrixtaReactionReadyForRole: any = useMemo(
makesSelectIsTrixtaReactionReadyForRole,
[],
);
const isTrixtaReactionReady = useSelector<{ trixta: TrixtaState }, boolean>(
(state) => selectIsTrixtaReactionReadyForRole(state, reactionRoleProps),
);
const selectReactionResponses: any = useMemo(
makeSelectTrixtaReactionResponseInstancesForRole,
[],
Expand Down Expand Up @@ -124,7 +133,7 @@ export const useTrixtaReaction = <
ref,
requestEvent,
}: submitTrixtaFunctionParameters) => {
if (!hasRoleAccess) return;
if (!hasRoleAccess || !isTrixtaReactionReady) return;

dispatch(
submitTrixtaReactionResponse({
Expand All @@ -138,7 +147,14 @@ export const useTrixtaReaction = <
}),
);
},
[dispatch, roleName, reactionName, hasRoleAccess, latestInstance],
[
dispatch,
roleName,
reactionName,
isTrixtaReactionReady,
hasRoleAccess,
latestInstance,
],
);

const success = latestInstance ? latestInstance.response.success : false;
Expand Down Expand Up @@ -177,8 +193,8 @@ export const useTrixtaReaction = <
hasRoleAccess,
hasResponse: !!latestInstance,
instances,
isInProgress,
loading,
isInProgress: isTrixtaReactionReady ? isInProgress : true,
loading: isTrixtaReactionReady ? loading : true,
clearReactionResponses,
submitTrixtaReaction,
};
Expand Down
25 changes: 25 additions & 0 deletions src/React/selectors/tests/actions/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ describe('Trixta Selectors', () => {
expect(selector(mockedState, props)).toEqual(expectedResult);
});

describe('makeSelectIsTrixtaActionReadyForRole', () => {
it('makeSelectIsTrixtaActionReadyForRole should return true if exists', () => {
const selector = trixtaActionSelectors.makeSelectIsTrixtaActionReadyForRole();
const selectedAction = trixtaActionSelectors.selectTrixtActionStateSelector(
mockedState,
props,
);
const expectedResult = selectedAction !== undefined;
expect(selector(mockedState, props)).toEqual(expectedResult);
expect(expectedResult).toEqual(true);
});

it('makeSelectIsTrixtaActionReadyForRole should return false if does not exist', () => {
const selector = trixtaActionSelectors.makeSelectIsTrixtaActionReadyForRole();
props.actionName = 'nonense';
const selectedAction = trixtaActionSelectors.selectTrixtActionStateSelector(
mockedState,
props,
);
const expectedResult = selectedAction !== undefined;
expect(selector(mockedState, props)).toEqual(expectedResult);
expect(expectedResult).toEqual(false);
});
});

describe('selectors for TrixtaAction loading status For Role', () => {
it('makeSelectIsTrixtaActionInProgress for existing role', () => {
const selector = trixtaActionSelectors.makeSelectIsTrixtaActionInProgress();
Expand Down
25 changes: 25 additions & 0 deletions src/React/selectors/tests/reactions/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ describe('Trixta Selectors', () => {
expect(selector(mockedState, props)).toEqual(expectedResult);
});

describe('makesSelectIsTrixtaReactionReadyForRole', () => {
it('makesSelectIsTrixtaReactionReadyForRole should return true if exists', () => {
const selector = trixtaReactionSelectors.makesSelectIsTrixtaReactionReadyForRole();
const selectedReaction = trixtaReactionSelectors.selectTrixtReactionStateSelector(
mockedState,
props,
);
const expectedResult = selectedReaction !== undefined;
expect(selector(mockedState, props)).toEqual(expectedResult);
expect(expectedResult).toEqual(true);
});

it('makesSelectIsTrixtaReactionReadyForRole should return false if does not exist', () => {
const selector = trixtaReactionSelectors.makesSelectIsTrixtaReactionReadyForRole();
props.reactionName = 'nonense';
const selectedReaction = trixtaReactionSelectors.selectTrixtReactionStateSelector(
mockedState,
props,
);
const expectedResult = selectedReaction !== undefined;
expect(selector(mockedState, props)).toEqual(expectedResult);
expect(expectedResult).toEqual(false);
});
});

it('makeSelectTrixtaReactionCommonForRole', () => {
const selector = trixtaReactionSelectors.makeSelectTrixtaReactionCommonForRole();
const selectedReaction = trixtaReactionSelectors.selectTrixtReactionStateSelector(
Expand Down
16 changes: 16 additions & 0 deletions src/React/selectors/trixtaActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ export const makeSelectTrixtaActionCommonForRole = (): OutputParametricSelector<
return get<TrixtaCommon>(selectedAction, `common`, undefined);
});

/**
* Selects the actions[props.roleName:props.actionName]
* for the given props.roleName, props.actionName and returns true or false if it exists
*/
export const makeSelectIsTrixtaActionReadyForRole = (): OutputParametricSelector<
{
trixta: TrixtaState;
},
TrixtaActionBaseProps,
boolean,
(res: TrixtaAction | undefined) => boolean
> =>
createSelector([selectTrixtActionStateSelector], (selectedAction) => {
return selectedAction !== undefined;
});

/**
* Selects the actions[props.roleName:props.actionName].instances for the given props.roleName,props.actionName and returns the action instances
*/
Expand Down
16 changes: 16 additions & 0 deletions src/React/selectors/trixtaReactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ export const makesSelectTrixtaReactionForRole = (): OutputParametricSelector<
return selectedReaction;
});

/**
* Selects the reactions[props.roleName:props.actionName]
* for the given props.roleName , props.reactionName and returns true or false if it exists
*/
export const makesSelectIsTrixtaReactionReadyForRole = (): OutputParametricSelector<
{
trixta: TrixtaState;
},
TrixtaReactionBaseProps,
boolean,
(res: TrixtaReaction | undefined) => boolean
> =>
createSelector([selectTrixtReactionStateSelector], (selectedReaction) => {
return selectedReaction !== undefined;
});

/**
* Returns the common for the props.reactionName
*/
Expand Down

0 comments on commit 49accb8

Please sign in to comment.