Skip to content

Commit

Permalink
Merge pull request #39 from trixtateam/feature/trixta_repsonse_time_s…
Browse files Browse the repository at this point in the history
…tamp

feat: response instances with timeStamp
  • Loading branch information
jacqueswho authored Nov 25, 2021
2 parents 06a976e + e9536d7 commit 4615afa
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 163 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ module.exports = {
moduleFileExtensions: ['js', 'jsx', 'json', 'ts', 'tsx'],
watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname'],
resetMocks: true,
timers : 'modern'
};
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const FormAsProps: ComponentStory<typeof TrixtaReactionComponent> = (
schema={schema}
formData={formData}
uiSchema={uiSchema}
onSubmit={({ formData }: { formData: any }) => {
onSubmit={({ formData }: { formData: unknown }) => {
props.submit(formData);
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ConnectProps & TrixtaReactionComponentProps & Record<string, any>) {
<>
{instances.map((value, index) => (
<TrixtaReactionInstanceComponent
key={`${reactionName}-${value.details.ref}-instance`}
key={`${reactionName}-${value.instanceKey}-instance`}
reactionName={reactionName}
requestForEffect={requestForEffect}
common={common}
Expand All @@ -59,7 +59,7 @@ ConnectProps & TrixtaReactionComponentProps & Record<string, any>) {
requestEvent={requestEvent}
roleName={roleName}
instanceIndex={index}
instanceRef={value.details.ref}
instanceRef={value.instanceKey}
debugMode={debugMode}
{...rest}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/React/components/reactions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '../../types';
import {
DefaultUnknownType,
TrixtaInstanceDetails,
TrixtaReactionInstanceDetails,
TrixtaInstanceResponse,
} from './../../types';
export interface TrixtaReactionComponentArgs<
Expand All @@ -20,5 +20,5 @@ export interface TrixtaReactionComponentArgs<
isInProgress: boolean;
loading?: boolean;
response?: TrixtaInstanceResponse<TSuccessType, TErrorType>;
details?: TrixtaInstanceDetails<TInitialData>;
details?: TrixtaReactionInstanceDetails<TInitialData>;
}
38 changes: 29 additions & 9 deletions src/React/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,41 @@ export const REMOVE_TRIXTA_ROLE = `@trixtateam/trixta-js/REMOVE_TRIXTA_ROLE`;
export const SIGN_OUT_TRIXTA = `@trixtateam/trixta-js/SIGN_OUT_TRIXTA`;
export const UPDATE_TRIXTA_ERROR = `@trixtateam/trixta-js/UPDATE_TRIXTA_ERROR`;

export const trixtaActionTypes = {
SIGN_OUT_TRIXTA,
UPDATE_TRIXTA_ROLES,
UPDATE_TRIXTA_ROLE,
REMOVE_TRIXTA_ROLE,
TRIXTA_REACTION_RESPONSE,
SUBMIT_TRIXTA_REACTION_RESPONSE,
SUBMIT_TRIXTA_REACTION_RESPONSE_FAILURE,
SUBMIT_TRIXTA_REACTION_RESPONSE_SUCCESS,
export const emitTrixtaJoinRole = ({
roleName,
}: {
roleName: string;
}): string => `${UPDATE_TRIXTA_ROLE}/${roleName}`;

export const emitTrixtaLeaveRole = ({
roleName,
}: {
roleName: string;
}): string => `${REMOVE_TRIXTA_ROLE}/${roleName}`;

export const trixtaActionResponseTypes = {
SUBMIT_TRIXTA_ACTION_RESPONSE,
SUBMIT_TRIXTA_ACTION_RESPONSE_FAILURE,
SUBMIT_TRIXTA_ACTION_RESPONSE_SUCCESS,
UPDATE_TRIXTA_ACTION_RESPONSE,
UPDATE_TRIXTA_ACTION,
};

export const trixtaReactionResponseTypes = {
TRIXTA_REACTION_RESPONSE,
SUBMIT_TRIXTA_REACTION_RESPONSE,
SUBMIT_TRIXTA_REACTION_RESPONSE_FAILURE,
SUBMIT_TRIXTA_REACTION_RESPONSE_SUCCESS,
UPDATE_TRIXTA_REACTION,
UPDATE_TRIXTA_REACTION_RESPONSE,
};

export const trixtaActionTypes = {
SIGN_OUT_TRIXTA,
UPDATE_TRIXTA_ROLES,
UPDATE_TRIXTA_ROLE,
REMOVE_TRIXTA_ROLE,
...trixtaReactionResponseTypes,
...trixtaActionResponseTypes,
UPDATE_TRIXTA_ERROR,
};
30 changes: 20 additions & 10 deletions src/React/hooks/use-trixta-action/use-trixta-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const useTrixtaAction = <
TErrorType
> => {
const dispatch = useDispatch();
const hasCallbackInvoked = useRef<boolean | undefined>(undefined);
const latestTimeStamp = useRef<number | undefined>(undefined);
if (isNullOrEmpty(roleName)) {
throw Error('Please provide roleName parameter.');
}
Expand Down Expand Up @@ -143,7 +143,7 @@ export const useTrixtaAction = <
extraData,
}: SubmitTrixtaFunctionParameters) => {
if (!hasRoleAccess || !isTrixtaActionReady) return;
hasCallbackInvoked.current = false;

dispatch(
submitTrixtaActionResponse({
extraData: extraData ?? {},
Expand Down Expand Up @@ -191,29 +191,39 @@ export const useTrixtaAction = <

const success = latestInstance ? latestInstance.response.success : false;
const error = latestInstance ? latestInstance.response.error : false;
useEffect(() => {
hasCallbackInvoked.current = undefined;
}, []);
const instanceTimeStamp = latestInstance
? latestInstance.response.timeStamp
: undefined;

useEffect(() => {
if (
requestStatus === RequestStatus.SUCCESS &&
onSuccess &&
hasCallbackInvoked.current === false
instanceTimeStamp &&
instanceTimeStamp !== latestTimeStamp.current
) {
hasCallbackInvoked.current = true;
latestTimeStamp.current = instanceTimeStamp;
onSuccess(success);
}

if (
requestStatus === RequestStatus.FAILURE &&
onError &&
hasCallbackInvoked.current === false
instanceTimeStamp &&
instanceTimeStamp !== latestTimeStamp.current
) {
hasCallbackInvoked.current = true;
latestTimeStamp.current = instanceTimeStamp;
onError(error);
}
}, [success, error, onError, onSuccess, clearActionResponses, requestStatus]);
}, [
success,
error,
onError,
onSuccess,
clearActionResponses,
requestStatus,
instanceTimeStamp,
]);

return {
latestInstance,
Expand Down
20 changes: 11 additions & 9 deletions src/React/hooks/use-trixta-reaction/use-trixta-reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const useTrixtaReaction = <
TErrorType
> => {
const dispatch = useDispatch();
const hasCallbackInvoked = useRef<boolean | undefined>(undefined);
const latestTimeStamp = useRef<number | undefined>(undefined);
if (isNullOrEmpty(roleName)) {
throw Error('Please provide roleName parameter.');
}
Expand Down Expand Up @@ -145,7 +145,6 @@ export const useTrixtaReaction = <
extraData,
}: SubmitTrixtaFunctionParameters) => {
if (!hasRoleAccess || !isTrixtaReactionReady) return;
hasCallbackInvoked.current = false;
dispatch(
submitTrixtaReactionResponse({
extraData: extraData ?? {},
Expand Down Expand Up @@ -176,26 +175,28 @@ export const useTrixtaReaction = <

const success = latestInstance ? latestInstance.response.success : false;
const error = latestInstance ? latestInstance.response.error : false;
useEffect(() => {
hasCallbackInvoked.current = undefined;
}, []);
const instanceTimeStamp = latestInstance
? latestInstance.response.timeStamp
: undefined;

useEffect(() => {
if (
requestStatus === RequestStatus.SUCCESS &&
onSuccess &&
hasCallbackInvoked.current === false
instanceTimeStamp &&
instanceTimeStamp !== latestTimeStamp.current
) {
hasCallbackInvoked.current = true;
latestTimeStamp.current = instanceTimeStamp;
onSuccess(success);
}

if (
requestStatus === RequestStatus.FAILURE &&
onError &&
hasCallbackInvoked.current === false
instanceTimeStamp &&
instanceTimeStamp !== latestTimeStamp.current
) {
hasCallbackInvoked.current = true;
latestTimeStamp.current = instanceTimeStamp;
onError(error);
}
}, [
Expand All @@ -205,6 +206,7 @@ export const useTrixtaReaction = <
requestStatus,
success,
error,
instanceTimeStamp,
]);

trixtaDebugger({
Expand Down
12 changes: 8 additions & 4 deletions src/React/reducers/__mocks__/trixtaState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ export const mockTrixtaReactions: TrixtaState['reactions'] = {
instances: {
requestForEffect: [
{
instanceKey: 'GBlDywXfoN',
details: {
status: 'ok',
ref: '',
type: 'requestForEffect',
initial_data: { data: 'test_data' },
dateCreated: '3/16/2021, 11:37:36 AM',
dateCreated: new Date().toLocaleString(),
},
response: {
success: false,
Expand Down Expand Up @@ -159,11 +160,12 @@ export const mockTrixtaReactions: TrixtaState['reactions'] = {
instances: {
requestForEffect: [
{
instanceKey: 'GBlDywXfoN',
details: {
ref: 'GBlDywXfoN',
type: 'requestForEffect',
initial_data: [],
dateCreated: '3/16/2021, 10:55:32 AM',
dateCreated: new Date().toLocaleString(),
},
response: {
success: false,
Expand Down Expand Up @@ -226,12 +228,13 @@ export const mockTrixtaReactions: TrixtaState['reactions'] = {
requestForEffect: [],
requestForResponse: [
{
instanceKey: 'f3ed1875-1841-4572-81b1-2966e28254a0',
details: {
ref: 'f3ed1875-1841-4572-81b1-2966e28254a0',
status: 'ok',
type: 'requestForResponse',
initial_data: null,
dateCreated: '3/16/2021, 11:25:20 AM',
dateCreated: new Date().toLocaleString(),
},
response: {
success: false,
Expand Down Expand Up @@ -267,12 +270,13 @@ export const mockTrixtaReactions: TrixtaState['reactions'] = {
requestForEffect: [],
requestForResponse: [
{
instanceKey: '4a32ed8d-f47d-4e78-921c-6a4aeb996bd3',
details: {
ref: '4a32ed8d-f47d-4e78-921c-6a4aeb996bd3',
status: 'ok',
type: 'requestForResponse',
initial_data: null,
dateCreated: '3/16/2021, 11:37:36 AM',
dateCreated: new Date().toLocaleString(),
},
response: {
success: false,
Expand Down
Loading

0 comments on commit 4615afa

Please sign in to comment.