2022-04-28 18:44:40 +00:00
|
|
|
import { ITriggerAction, IUserInteraction, ModalActions } from '../../containers/UIKit/interfaces';
|
|
|
|
import { TRocketChat } from '../../definitions/IRocketChat';
|
2020-02-11 14:01:35 +00:00
|
|
|
import EventEmitter from '../../utils/events';
|
2020-02-20 12:58:13 +00:00
|
|
|
import fetch from '../../utils/fetch';
|
2022-04-28 18:44:40 +00:00
|
|
|
import random from '../../utils/random';
|
|
|
|
import Navigation from '../navigation/appNavigation';
|
|
|
|
import sdk from '../services/sdk';
|
2020-02-11 14:01:35 +00:00
|
|
|
|
|
|
|
const triggersId = new Map();
|
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
const invalidateTriggerId = (id: string) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
const appId = triggersId.get(id);
|
|
|
|
triggersId.delete(id);
|
|
|
|
return appId;
|
|
|
|
};
|
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
export const generateTriggerId = (appId?: string): string => {
|
2020-02-11 14:01:35 +00:00
|
|
|
const triggerId = random(17);
|
|
|
|
triggersId.set(triggerId, appId);
|
2020-02-18 15:56:02 +00:00
|
|
|
|
2020-02-11 14:01:35 +00:00
|
|
|
return triggerId;
|
|
|
|
};
|
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
export const handlePayloadUserInteraction = (
|
|
|
|
type: ModalActions,
|
|
|
|
{ triggerId, ...data }: IUserInteraction
|
|
|
|
): ModalActions | undefined => {
|
2020-02-11 14:01:35 +00:00
|
|
|
if (!triggersId.has(triggerId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const appId = invalidateTriggerId(triggerId);
|
|
|
|
if (!appId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { view } = data;
|
|
|
|
let { viewId } = data;
|
|
|
|
|
|
|
|
if (view && view.id) {
|
|
|
|
viewId = view.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!viewId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
if ([ModalActions.ERRORS].includes(type)) {
|
2020-02-11 14:01:35 +00:00
|
|
|
EventEmitter.emit(viewId, {
|
|
|
|
type,
|
|
|
|
triggerId,
|
|
|
|
viewId,
|
|
|
|
appId,
|
|
|
|
...data
|
|
|
|
});
|
2022-03-29 18:53:27 +00:00
|
|
|
return ModalActions.ERRORS;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
if ([ModalActions.UPDATE].includes(type)) {
|
2020-02-11 14:01:35 +00:00
|
|
|
EventEmitter.emit(viewId, {
|
|
|
|
type,
|
|
|
|
triggerId,
|
|
|
|
viewId,
|
|
|
|
appId,
|
|
|
|
...data
|
|
|
|
});
|
2022-03-29 18:53:27 +00:00
|
|
|
return ModalActions.UPDATE;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
if ([ModalActions.OPEN].includes(type) || [ModalActions.MODAL].includes(type)) {
|
2020-02-11 14:01:35 +00:00
|
|
|
Navigation.navigate('ModalBlockView', {
|
|
|
|
data: {
|
|
|
|
triggerId,
|
|
|
|
viewId,
|
|
|
|
appId,
|
|
|
|
...data
|
|
|
|
}
|
|
|
|
});
|
2022-03-29 18:53:27 +00:00
|
|
|
return ModalActions.OPEN;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
return ModalActions.CLOSE;
|
2020-02-11 14:01:35 +00:00
|
|
|
};
|
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
export function triggerAction(
|
|
|
|
this: TRocketChat,
|
|
|
|
{ type, actionId, appId, rid, mid, viewId, container, ...rest }: ITriggerAction
|
|
|
|
) {
|
|
|
|
return new Promise<ModalActions | undefined | void>(async (resolve, reject) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
const triggerId = generateTriggerId(appId);
|
|
|
|
|
|
|
|
const payload = rest.payload || rest;
|
|
|
|
|
|
|
|
try {
|
2022-03-29 18:53:27 +00:00
|
|
|
const { userId, authToken } = sdk.current.currentLogin;
|
|
|
|
const { host } = sdk.current.client;
|
2020-02-18 15:56:02 +00:00
|
|
|
|
2020-02-11 16:00:58 +00:00
|
|
|
// we need to use fetch because this.sdk.post add /v1 to url
|
2021-10-01 18:12:19 +00:00
|
|
|
const result = await fetch(`${host}/api/apps/ui.interaction/${appId}/`, {
|
2020-02-11 16:00:58 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Auth-Token': authToken,
|
|
|
|
'X-User-Id': userId
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
type,
|
|
|
|
actionId,
|
|
|
|
payload,
|
|
|
|
container,
|
|
|
|
mid,
|
|
|
|
rid,
|
|
|
|
triggerId,
|
|
|
|
viewId
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { type: interactionType, ...data } = await result.json();
|
2020-02-18 15:56:02 +00:00
|
|
|
return resolve(handlePayloadUserInteraction(interactionType, data));
|
2020-02-11 16:00:58 +00:00
|
|
|
} catch (e) {
|
|
|
|
// modal.close has no body, so result.json will fail
|
|
|
|
// but it returns ok status
|
|
|
|
if (result.ok) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2020-02-11 16:00:58 +00:00
|
|
|
// do nothing
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
2020-02-11 16:00:58 +00:00
|
|
|
return reject();
|
2020-02-11 14:01:35 +00:00
|
|
|
});
|
|
|
|
}
|