2020-02-11 14:01:35 +00:00
|
|
|
import random from '../../utils/random';
|
|
|
|
import EventEmitter from '../../utils/events';
|
2020-02-20 12:58:13 +00:00
|
|
|
import fetch from '../../utils/fetch';
|
2020-02-11 14:01:35 +00:00
|
|
|
import Navigation from '../Navigation';
|
|
|
|
|
|
|
|
const ACTION_TYPES = {
|
|
|
|
ACTION: 'blockAction',
|
|
|
|
SUBMIT: 'viewSubmit',
|
|
|
|
CLOSED: 'viewClosed'
|
|
|
|
};
|
|
|
|
|
|
|
|
export const MODAL_ACTIONS = {
|
|
|
|
MODAL: 'modal',
|
|
|
|
OPEN: 'modal.open',
|
|
|
|
CLOSE: 'modal.close',
|
|
|
|
UPDATE: 'modal.update',
|
|
|
|
ERRORS: 'errors'
|
|
|
|
};
|
|
|
|
|
|
|
|
export const CONTAINER_TYPES = {
|
|
|
|
VIEW: 'view',
|
|
|
|
MESSAGE: 'message'
|
|
|
|
};
|
|
|
|
|
|
|
|
const triggersId = new Map();
|
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
const invalidateTriggerId = id => {
|
2020-02-11 14:01:35 +00:00
|
|
|
const appId = triggersId.get(id);
|
|
|
|
triggersId.delete(id);
|
|
|
|
return appId;
|
|
|
|
};
|
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
export const generateTriggerId = appId => {
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const handlePayloadUserInteraction = (type, { triggerId, ...data }) => {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([MODAL_ACTIONS.ERRORS].includes(type)) {
|
|
|
|
EventEmitter.emit(viewId, {
|
|
|
|
type,
|
|
|
|
triggerId,
|
|
|
|
viewId,
|
|
|
|
appId,
|
|
|
|
...data
|
|
|
|
});
|
|
|
|
return MODAL_ACTIONS.ERRORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([MODAL_ACTIONS.UPDATE].includes(type)) {
|
|
|
|
EventEmitter.emit(viewId, {
|
|
|
|
type,
|
|
|
|
triggerId,
|
|
|
|
viewId,
|
|
|
|
appId,
|
|
|
|
...data
|
|
|
|
});
|
|
|
|
return MODAL_ACTIONS.UPDATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([MODAL_ACTIONS.OPEN].includes(type) || [MODAL_ACTIONS.MODAL].includes(type)) {
|
|
|
|
Navigation.navigate('ModalBlockView', {
|
|
|
|
data: {
|
|
|
|
triggerId,
|
|
|
|
viewId,
|
|
|
|
appId,
|
|
|
|
...data
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return MODAL_ACTIONS.OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MODAL_ACTIONS.CLOSE;
|
|
|
|
};
|
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
export function triggerAction({ type, actionId, appId, rid, mid, viewId, container, ...rest }) {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
const triggerId = generateTriggerId(appId);
|
|
|
|
|
|
|
|
const payload = rest.payload || rest;
|
|
|
|
|
|
|
|
try {
|
2020-02-18 15:56:02 +00:00
|
|
|
const { userId, authToken } = this.sdk.currentLogin;
|
|
|
|
const { host } = this.sdk.client;
|
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function triggerBlockAction(options) {
|
|
|
|
return triggerAction.call(this, { type: ACTION_TYPES.ACTION, ...options });
|
|
|
|
}
|
|
|
|
|
2020-02-18 15:56:02 +00:00
|
|
|
export async function triggerSubmitView({ viewId, ...options }) {
|
|
|
|
const result = await triggerAction.call(this, { type: ACTION_TYPES.SUBMIT, viewId, ...options });
|
|
|
|
if (!result || MODAL_ACTIONS.CLOSE === result) {
|
|
|
|
Navigation.back();
|
|
|
|
}
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function triggerCancel({ view, ...options }) {
|
|
|
|
return triggerAction.call(this, { type: ACTION_TYPES.CLOSED, view, ...options });
|
|
|
|
}
|