-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1200 from nyaruka/feature/request-option
Add request optin action
- Loading branch information
Showing
16 changed files
with
252 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export const optins = { | ||
next: null, | ||
previous: null, | ||
results: [ | ||
{ | ||
uuid: '8fc583a0-0700-434d-b238-8053af1d040e', | ||
name: 'Newsletter', | ||
created_on: '2023-09-25T04:43:19.103443Z' | ||
}, | ||
{ | ||
uuid: '806f52e7-ad6a-4ede-8793-6f51b60a30ec', | ||
name: 'U-Report Polls', | ||
created_on: '2023-09-23T00:18:41.572795Z' | ||
} | ||
] | ||
}; | ||
const { getOpts } = require('./utils'); | ||
|
||
exports.handler = (evt, ctx, cb) => cb(null, getOpts({ body: JSON.stringify(optins) })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { RequestOptIn } from 'flowTypes'; | ||
import * as React from 'react'; | ||
|
||
const RequestOptInComp: React.SFC<RequestOptIn> = (action: RequestOptIn): JSX.Element => { | ||
return <div className="optin">{action.optin.name}</div>; | ||
}; | ||
|
||
export default RequestOptInComp; |
107 changes: 107 additions & 0 deletions
107
src/components/flow/actions/requestoptin/RequestOptInForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { react as bindCallbacks } from 'auto-bind'; | ||
import Dialog, { ButtonSet } from 'components/dialog/Dialog'; | ||
import { ActionFormProps } from 'components/flow/props'; | ||
import AssetSelector from 'components/form/assetselector/AssetSelector'; | ||
import TypeList from 'components/nodeeditor/TypeList'; | ||
import { fakePropType } from 'config/ConfigProvider'; | ||
import * as React from 'react'; | ||
import { Asset } from 'store/flowContext'; | ||
import { FormState, mergeForm } from 'store/nodeEditor'; | ||
import { shouldRequireIf, validate } from 'store/validators'; | ||
|
||
import i18n from 'config/i18n'; | ||
import { Trans } from 'react-i18next'; | ||
import { renderIssues } from '../helpers'; | ||
import { initializeForm, stateToAction } from './helpers'; | ||
|
||
export interface RequestOptInFormState extends FormState { | ||
optin: any; | ||
} | ||
|
||
export const controlLabelSpecId = 'label'; | ||
|
||
export default class AddLabelsForm extends React.PureComponent< | ||
ActionFormProps, | ||
RequestOptInFormState | ||
> { | ||
public static contextTypes = { | ||
assetService: fakePropType | ||
}; | ||
|
||
constructor(props: ActionFormProps) { | ||
super(props); | ||
|
||
this.state = initializeForm(this.props.nodeSettings); | ||
bindCallbacks(this, { | ||
include: [/^on/, /^handle/] | ||
}); | ||
} | ||
|
||
public handleSave(): void { | ||
const valid = this.handleOptInChanged(this.state.optin.value!, true); | ||
|
||
if (valid) { | ||
const newAction = stateToAction(this.props.nodeSettings, this.state); | ||
this.props.updateAction(newAction); | ||
this.props.onClose(false); | ||
} | ||
} | ||
|
||
public handleOptInChanged(selected: Asset[], submitting: boolean = false): boolean { | ||
const updates: Partial<RequestOptInFormState> = { | ||
optin: validate(i18n.t('forms.title', 'Name'), selected, [shouldRequireIf(submitting)]) | ||
}; | ||
|
||
const updated = mergeForm(this.state, updates); | ||
this.setState(updated); | ||
return updated.valid; | ||
} | ||
|
||
private getButtons(): ButtonSet { | ||
return { | ||
primary: { name: i18n.t('buttons.ok', 'Ok'), onClick: this.handleSave }, | ||
secondary: { | ||
name: i18n.t('buttons.cancel', 'Cancel'), | ||
onClick: () => this.props.onClose(true) | ||
} | ||
}; | ||
} | ||
|
||
public handleCreateAssetFromInput(input: string): any { | ||
return { name: input }; | ||
} | ||
|
||
public handleOptInCreated(optin: Asset): void { | ||
console.log(optin); | ||
this.handleOptInChanged([optin]); | ||
} | ||
|
||
public render(): JSX.Element { | ||
const typeConfig = this.props.typeConfig; | ||
return ( | ||
<Dialog title={typeConfig.name} headerClass={typeConfig.type} buttons={this.getButtons()}> | ||
<TypeList __className="" initialType={typeConfig} onChange={this.props.onTypeChange} /> | ||
<p data-spec={controlLabelSpecId}> | ||
<Trans i18nKey="forms.request_optin_summary">Select the Opt-In to request</Trans> | ||
</p> | ||
|
||
<AssetSelector | ||
name={i18n.t('forms.request_optin.title', 'Request Opt-In')} | ||
placeholder={i18n.t( | ||
'enter_to_create_optin', | ||
'Enter the name of an existing Opt-In or create a new one' | ||
)} | ||
assets={this.props.assetStore.optins} | ||
entry={this.state.optin} | ||
searchable={true} | ||
expressions={true} | ||
onChange={this.handleOptInChanged} | ||
createPrefix={i18n.t('create_optin', 'Create Opt-In') + ': '} | ||
createAssetFromInput={this.handleCreateAssetFromInput} | ||
onAssetCreated={this.handleOptInCreated} | ||
/> | ||
{renderIssues(this.props)} | ||
</Dialog> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { getActionUUID } from 'components/flow/actions/helpers'; | ||
import { Types } from 'config/interfaces'; | ||
import { RequestOptIn } from 'flowTypes'; | ||
import { NodeEditorSettings } from 'store/nodeEditor'; | ||
import { RequestOptInFormState } from './RequestOptInForm'; | ||
|
||
export const initializeForm = (settings: NodeEditorSettings): RequestOptInFormState => { | ||
if (settings.originalAction && settings.originalAction.type === Types.request_optin) { | ||
const action = settings.originalAction as RequestOptIn; | ||
return { | ||
optin: { value: action.optin }, | ||
valid: true | ||
}; | ||
} | ||
|
||
return { | ||
optin: { value: null }, | ||
valid: false | ||
}; | ||
}; | ||
|
||
export const stateToAction = ( | ||
settings: NodeEditorSettings, | ||
formState: RequestOptInFormState | ||
): RequestOptIn => { | ||
const result = { | ||
type: Types.request_optin, | ||
optin: formState.optin.value[0], | ||
uuid: getActionUUID(settings, Types.add_input_labels) | ||
}; | ||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.