mattermost-mobile/app/screens/apps_form/apps_form_container.tsx
Michael Kochell 6fd9b6de71
[MM-33989] Show bindings in all threads and post menus (#5232)
* Apps bindings support (#5012)

* Add redux related information

* Add binding loading on channel refresh

* Add channel header and post option bindings

* Fix test

* Minor fixes

* Fix snapshots

* Handle errors and show bindings only on main channel

* Update Expand Levels keys and values to match latest changes

* Add NAVIGATE call response handling.

* Add more isolation to apps related code

* Add defaults to send ephemeral

* Update variable naming

* Rename shouldProcessApps by a more meaningful appsEnabled

* Embedded forms (#5169)

* Add redux related information

* Add binding loading on channel refresh

* Add channel header and post option bindings

* Fix test

* Minor fixes

* Fix snapshots

* Handle errors and show bindings only on main channel

* Update Expand Levels keys and values to match latest changes

* Add NAVIGATE call response handling.

* Add more isolation to apps related code

* Add Embedded Forms

* Fix snapshots

* Add Embedded Forms

* Improve naming, change the root element to be a binding and improve binding handling

* Get post down on the buttons, remove unneeded variables and minor fixes from the review

* Allow undefined bindings to fillBindingsInformation and add logging for error.

* Address review feedback

* Add App Forms (#5177)

* Add App Forms

* Address feedback and self review

* Add dynamic select

* Fixes and improvements

* Add the ability to refresh on submit.

* Use AppFormValue instead of redoing the type

* Address feedback

* [MM-31508] Rename URL to Path in Call (#5186)

* Add user agent to call context (#5193)

* Remove unneeded state reference (#5196)

* Change user agent query on fetch bindings (#5201)

* Add refresh websocket event to refetch bindings (#5194)

* Add refresh websocket event to refetch bindings

* Add missing changes

* Declare socket event constant and separate the handler to a different function

* Add binding validation on binding fetch (#5200)

* Apps commands (#5107)

* Add redux related information

* Add binding loading on channel refresh

* Add channel header and post option bindings

* Fix test

* Minor fixes

* Fix snapshots

* apps modals draft

* Handle errors and show bindings only on main channel

* Update Expand Levels keys and values to match latest changes

* Add NAVIGATE call response handling.

* Add more isolation to apps related code

* reuse command parser throughout slash_suggestion component's lifecycle

* fix prop and lint

* using alert to show error message. need another way

* duplicate import

* types

* types

* types

* rename file

* copy webapp parser and test

* dependencies moved. tests pass

* move app command parser into its own folder

* converted to typescript, all tests are passing

* automated and manual tests work

* lint

* lint

* remove fall throughs with blocks

* types

* doAppCall type

* extract displayError to deps file

* test types

* lint

* fix tests

* unused import

* PR feedback

* fix imports and show errors

* types

* remove execute suggestion for mobile

* watch feature flag

* fix tests

* change form text arugment behavior to show user input and not hint

* return call response error in doAppCall

* update tests to remove hint from text field suggestions

* lint

* use new base command structure

* fix tests

* wrap appsEnabled

* typescript actions/command.ts

* update app constants

* PR feedback

* fix error handling from doAppCall action

* Use App CallRequest structure (#5212)

* error handling

* move test files

* remove unused import

Co-authored-by: Daniel Espino García <larkox@gmail.com>

* Add feature flag (#5207)

* Add feature flag

* Simplify return

* Add localization, call validation and use call type on subpath (#5221)

* Add localization, call validation and use call type on subpath

* Add more localization to the parser and bring fixes from webapp

* Fix ephemerals and channel header / post options crashes

* fix app command parser deps and alert messages

* Improve suggestion handling

* Fix test

* Fix lint

* Return errors as error

* Address PR feedback

* return error property

* fix error string wordings

Co-authored-by: Michael Kochell <6913320+mickmister@users.noreply.github.com>

* show bindings in threads and all post menus

* unused import

* fallback on post id in case this is the root post

* fix ephemeral reply for modal submissions

* fix merge typo

Co-authored-by: Daniel Espino García <larkox@gmail.com>
Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
2021-03-24 17:06:51 -04:00

241 lines
8.1 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {PureComponent} from 'react';
import {intlShape} from 'react-intl';
import {Theme} from '@mm-redux/types/preferences';
import {AppCallResponse, AppCallRequest, AppField, AppForm, AppFormValues, FormResponseData, AppCallType, AppLookupResponse} from '@mm-redux/types/apps';
import {AppCallResponseTypes, AppCallTypes} from '@mm-redux/constants/apps';
import AppsFormComponent from './apps_form_component';
import {makeCallErrorResponse} from '@utils/apps';
import {ActionResult} from '@mm-redux/types/actions';
export type Props = {
form?: AppForm;
call?: AppCallRequest;
actions: {
doAppCall: (call: AppCallRequest, type: AppCallType, intl: any) => Promise<{data?: AppCallResponse<any>, error?: AppCallResponse<any>}>;
sendEphemeralPost: (message: any, channelId?: string, parentId?: string) => Promise<ActionResult>;
};
theme: Theme;
componentId: string;
};
export type State = {
form?: AppForm;
}
export default class AppsFormContainer extends PureComponent<Props, State> {
static contextTypes = {
intl: intlShape.isRequired,
};
constructor(props: Props) {
super(props);
this.state = {form: props.form};
}
handleSubmit = async (submission: {values: AppFormValues}): Promise<{data?: AppCallResponse<FormResponseData>, error?: AppCallResponse<FormResponseData>}> => {
const intl = this.context.intl;
const makeErrorMsg = (msg: string) => {
return intl.formatMessage(
{
id: 'apps.error.form.submit.pretext',
defaultMessage: 'There has been an error submitting the modal. Contact the app developer. Details: {details}',
},
{details: msg},
);
};
const {form} = this.state;
if (!form) {
return {error: makeCallErrorResponse(makeErrorMsg(intl.formatMessage(
{
id: 'apps.error.form.no_form',
defaultMessage: '`form` is not defined',
},
)))};
}
const call = this.getCall();
if (!call) {
return {error: makeCallErrorResponse(makeErrorMsg(intl.formatMessage(
{
id: 'apps.error.form.no_call',
defaultMessage: '`call` is not defined',
},
)))};
}
const res = await this.props.actions.doAppCall({
...call,
values: submission.values,
}, AppCallTypes.SUBMIT, intl);
if (res.error) {
return res;
}
const callResp = res.data!;
switch (callResp.type) {
case AppCallResponseTypes.OK:
if (callResp.markdown) {
this.props.actions.sendEphemeralPost(callResp.markdown, call.context.channel_id, call.context.root_id || call.context.post_id);
}
break;
case AppCallResponseTypes.FORM:
this.setState({form: callResp.form});
break;
case AppCallResponseTypes.NAVIGATE:
break;
default:
return {error: makeCallErrorResponse(makeErrorMsg(intl.formatMessage(
{
id: 'apps.error.responses.unknown_type',
defaultMessage: 'App response type not supported. Response type: {type}.',
}, {
type: callResp.type,
},
)))};
}
return res;
}
refreshOnSelect = async (field: AppField, values: AppFormValues): Promise<{data?: AppCallResponse<FormResponseData>, error?: AppCallResponse<FormResponseData>}> => {
const intl = this.context.intl;
const makeErrorMsg = (message: string) => intl.formatMessage(
{
id: 'apps.error.form.refresh',
defaultMessage: 'There has been an error updating the modal. Contact the app developer. Details: {details}',
},
{details: message},
);
const {form} = this.state;
if (!form) {
return {error: makeCallErrorResponse(makeErrorMsg(intl.formatMessage({
id: 'apps.error.form.no_form',
defaultMessage: '`form` is not defined.',
})))};
}
const call = this.getCall();
if (!call) {
return {error: makeCallErrorResponse(makeErrorMsg(intl.formatMessage({
id: 'apps.error.form.no_call',
defaultMessage: '`call` is not defined.',
})))};
}
if (!field.refresh) {
// Should never happen
return {error: makeCallErrorResponse(makeErrorMsg(intl.formatMessage({
id: 'apps.error.form.refresh_no_refresh',
defaultMessage: 'Called refresh on no refresh field.',
})))};
}
const res = await this.props.actions.doAppCall({
...call,
selected_field: field.name,
values,
}, AppCallTypes.FORM, intl);
if (res.error) {
return res;
}
const callResp = res.data!;
switch (callResp.type) {
case AppCallResponseTypes.FORM:
this.setState({form: callResp.form});
break;
case AppCallResponseTypes.OK:
case AppCallResponseTypes.NAVIGATE:
return {error: makeCallErrorResponse(makeErrorMsg(intl.formatMessage({
id: 'apps.error.responses.unexpected_type',
defaultMessage: 'App response type was not expected. Response type: {type}.',
}, {
type: callResp.type,
},
)))};
default:
return {error: makeCallErrorResponse(makeErrorMsg(intl.formatMessage({
id: 'apps.error.responses.unknown_type',
defaultMessage: 'App response type not supported. Response type: {type}.',
}, {
type: callResp.type,
},
)))};
}
return res;
};
performLookupCall = async (field: AppField, values: AppFormValues, userInput: string): Promise<{data?: AppCallResponse<AppLookupResponse>, error?: AppCallResponse<AppLookupResponse>}> => {
const intl = this.context.intl;
const makeErrorMsg = (message: string) => intl.formatMessage(
{
id: 'apps.error.form.refresh',
defaultMessage: 'There has been an error fetching the select fields. Contact the app developer. Details: {details}',
},
{details: message},
);
const call = this.getCall();
if (!call) {
return makeErrorMsg(intl.formatMessage({id: 'apps.error.form.no_lookup_call', defaultMessage: 'performLookupCall props.call is not defined'}));
}
return this.props.actions.doAppCall({
...call,
values,
selected_field: field.name,
query: userInput,
}, AppCallTypes.LOOKUP, intl);
}
getCall = (): AppCallRequest | null => {
const {form} = this.state;
const {call} = this.props;
if (!call) {
return null;
}
return {
...call,
...form?.call,
context: {
...call.context,
},
values: {
...call.values,
},
};
}
render() {
const {form} = this.state;
if (!form) {
return null;
}
const call = this.getCall();
if (!call) {
return null;
}
return (
<AppsFormComponent
form={form}
call={call}
actions={{
submit: this.handleSubmit,
performLookupCall: this.performLookupCall,
refreshOnSelect: this.refreshOnSelect,
}}
theme={this.props.theme}
componentId={this.props.componentId}
/>
);
}
}