Skip to content

Commit

Permalink
Merge pull request #31 from glific/feature/update-delay-node
Browse files Browse the repository at this point in the history
Updated delay node format
  • Loading branch information
mdshamoon authored Apr 16, 2021
2 parents 4de7b6f + ef697e9 commit 0c22d3b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/components/flow/routers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
UIConfig,
WebhookExitNames,
CallClassifier,
OpenTicket
OpenTicket,
Delay
} from 'flowTypes';
import { RenderNode } from 'store/flowContext';
import { createUUID, snakify } from 'utils';
Expand All @@ -37,7 +38,7 @@ export const createRenderNode = (
router: Router,
exits: Exit[],
type: Types,
actions: Action[] = [],
actions: Action[] | Delay[] = [],
uiConfig: { [key: string]: any } = {}
): RenderNode => {
const renderNode: RenderNode = {
Expand Down
10 changes: 5 additions & 5 deletions src/components/flow/routers/sequence/SequenceForm.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { react as bindCallbacks } from 'auto-bind';
import * as React from 'react';
import Dialog, { ButtonSet } from 'components/dialog/Dialog';
import { ActionFormProps } from 'components/flow/props';
import { actionToState, stateToAction } from 'components/flow/routers/sequence/helpers';
import { RouterFormProps } from 'components/flow/props';
import { actionToState, stateToNode } from 'components/flow/routers/sequence/helpers';
import TypeList from 'components/nodeeditor/TypeList';
import { FormState } from 'store/nodeEditor';
import i18n from 'config/i18n';
Expand All @@ -15,8 +15,8 @@ export interface SequenceFormState extends FormState {
minutes: string;
}

export default class SequenceForm extends React.Component<ActionFormProps, SequenceFormState> {
constructor(props: ActionFormProps) {
export default class SequenceForm extends React.Component<RouterFormProps, SequenceFormState> {
constructor(props: RouterFormProps) {
super(props);

this.state = actionToState(this.props.nodeSettings);
Expand All @@ -30,7 +30,7 @@ export default class SequenceForm extends React.Component<ActionFormProps, Seque
if (
!isNaN(parseInt(this.state.hours) + parseInt(this.state.minutes) + parseInt(this.state.days))
) {
this.props.updateAction(stateToAction(this.props.nodeSettings, this.state));
this.props.updateRouter(stateToNode(this.props.nodeSettings, this.state));
this.props.onClose(false);
}
}
Expand Down
41 changes: 33 additions & 8 deletions src/components/flow/routers/sequence/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { getActionUUID } from 'components/flow/actions/helpers';
import { Types } from 'config/interfaces';
import { Delay } from 'flowTypes';
import { NodeEditorSettings, StringEntry } from 'store/nodeEditor';
import { NodeEditorSettings } from 'store/nodeEditor';
import { RouterTypes, SwitchRouter } from 'flowTypes';
import { createRenderNode, resolveRoutes } from '../helpers';
import { SequenceFormState } from './SequenceForm';
import { DEFAULT_OPERAND } from 'components/nodeeditor/constants';

export const actionToState = (settings: NodeEditorSettings): SequenceFormState => {
let delayNode: SequenceFormState = {
Expand All @@ -13,7 +16,6 @@ export const actionToState = (settings: NodeEditorSettings): SequenceFormState =
};
if (settings.originalAction && settings.originalAction.type === 'wait_for_time') {
const action = settings.originalAction as Delay;

if (action.delay) {
const delayInSeconds = parseInt(action.delay);
delayNode.days = Math.floor(delayInSeconds / (3600 * 24)).toString();
Expand All @@ -25,16 +27,39 @@ export const actionToState = (settings: NodeEditorSettings): SequenceFormState =
return delayNode;
};

export const stateToAction = (settings: NodeEditorSettings, state: SequenceFormState): any => {
export const stateToNode = (settings: NodeEditorSettings, state: SequenceFormState): any => {
const { days, hours, minutes } = state;

const delayInSeconds = parseInt(days) * 86400 + parseInt(hours) * 3600 + parseInt(minutes) * 60;
const { cases, exits, defaultCategory, caseConfig, categories } = resolveRoutes(
[],
false,
settings.originalNode.node,
'Completed'
);

const result = {
type: Types.wait_for_time,
uuid: getActionUUID(settings, Types.send_msg),
delay: delayInSeconds.toString()
const router: SwitchRouter = {
type: RouterTypes.switch,
default_category_uuid: defaultCategory,
cases,
categories,
operand: DEFAULT_OPERAND
};

return result;
const newRenderNode = createRenderNode(
settings.originalNode.node.uuid,
router,
exits,
Types.wait_for_time,
[
{
type: Types.wait_for_time,
uuid: getActionUUID(settings, Types.send_msg),
delay: delayInSeconds.toString()
}
],
{ cases: caseConfig }
);

return newRenderNode;
};

0 comments on commit 0c22d3b

Please sign in to comment.