From c454c58d5c92e2eb35b5a94a6c1e6caeb42836d1 Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Sat, 5 May 2018 12:01:25 +0300 Subject: [PATCH] #22: refactor application forms --- .../apps/applicationForm/ApplicationForm.js | 34 +++-- .../ApplicationTypeSwitcher.js | 54 ++++---- .../applicationForm/MinecraftServerType.js | 90 ++++++------- .../dev/apps/applicationForm/WebsiteType.js | 122 +++++++++--------- src/components/dev/apps/index.js | 6 +- src/components/ui/collapse/Collapse.js | 4 +- src/components/ui/form/Checkbox.js | 8 +- src/components/ui/form/FormComponent.js | 12 +- src/components/ui/form/Input.js | 14 +- src/components/ui/form/Radio.js | 8 +- src/components/ui/form/TextArea.js | 12 +- src/pages/dev/CreateNewApplicationPage.js | 18 +-- src/pages/dev/UpdateApplicationPage.js | 32 +++-- src/services/api/oauth.js | 3 +- src/services/logger/logger.js | 7 + 15 files changed, 212 insertions(+), 212 deletions(-) diff --git a/src/components/dev/apps/applicationForm/ApplicationForm.js b/src/components/dev/apps/applicationForm/ApplicationForm.js index d0ee78e..12a2618 100644 --- a/src/components/dev/apps/applicationForm/ApplicationForm.js +++ b/src/components/dev/apps/applicationForm/ApplicationForm.js @@ -1,26 +1,25 @@ // @flow +import type { ComponentType } from 'react'; +import type { MessageDescriptor } from 'react-intl'; +import type { OauthAppResponse } from 'services/api/oauth'; +import type { ApplicationType } from 'components/dev/apps'; import React, { Component } from 'react'; import { FormattedMessage as Message } from 'react-intl'; import { Helmet } from 'react-helmet'; - import { Form, FormModel, Button } from 'components/ui/form'; import { BackButton } from 'components/profile/ProfileForm'; import { COLOR_GREEN } from 'components/ui'; - import { TYPE_APPLICATION, TYPE_MINECRAFT_SERVER } from 'components/dev/apps'; import styles from 'components/profile/profileForm.scss'; +import logger from 'services/logger'; import messages from './ApplicationForm.intl.json'; import ApplicationTypeSwitcher from './ApplicationTypeSwitcher'; import WebsiteType from './WebsiteType'; import MinecraftServerType from './MinecraftServerType'; -import type { ComponentType } from 'react'; -import type { MessageDescriptor } from 'react-intl'; -import type { OauthAppResponse } from 'services/api/oauth'; - const typeToForm: { - [key: string]: { + [key: ApplicationType]: { label: MessageDescriptor, component: ComponentType, }, @@ -36,9 +35,10 @@ const typeToForm: { }; const typeToLabel: { - [key: string]: MessageDescriptor, -} = Object.keys(typeToForm).reduce((result, key: string) => { + [key: ApplicationType]: MessageDescriptor, +} = Object.keys(typeToForm).reduce((result, key: ApplicationType) => { result[key] = typeToForm[key].label; + return result; }, {}); @@ -46,12 +46,10 @@ export default class ApplicationForm extends Component<{ app: OauthAppResponse, form: FormModel, displayTypeSwitcher?: bool, - type: ?string, - setType: (string) => void, - onSubmit: (FormModel) => Promise<*>, + type: ?ApplicationType, + setType: (ApplicationType) => void, + onSubmit: (FormModel) => Promise, }> { - static displayName = 'ApplicationForm'; - static defaultProps = { setType: () => {}, }; @@ -87,15 +85,15 @@ export default class ApplicationForm extends Component<{ )} - {!FormComponent && ( + {FormComponent ? ( + + ) : (

)} - - {FormComponent && } @@ -123,7 +121,7 @@ export default class ApplicationForm extends Component<{ return; } - throw resp; + logger.unexpected(new Error('Error submitting application form'), resp); } }; } diff --git a/src/components/dev/apps/applicationForm/ApplicationTypeSwitcher.js b/src/components/dev/apps/applicationForm/ApplicationTypeSwitcher.js index 5ffab91..9cddf6a 100644 --- a/src/components/dev/apps/applicationForm/ApplicationTypeSwitcher.js +++ b/src/components/dev/apps/applicationForm/ApplicationTypeSwitcher.js @@ -1,40 +1,32 @@ // @flow -import React, { Component } from 'react'; - +import type { ApplicationType } from 'components/dev/apps'; +import type { MessageDescriptor } from 'react-intl'; +import React from 'react'; import { SKIN_LIGHT } from 'components/ui'; import { Radio } from 'components/ui/form'; import styles from './applicationTypeSwitcher.scss'; -import type { MessageDescriptor } from 'react-intl'; - -export default class ApplicationTypeSwitcher extends Component<{ +export default function ApplicationTypeSwitcher({ setType, appTypes, selectedType }: { appTypes: { - [key: string]: MessageDescriptor, + [key: ApplicationType]: MessageDescriptor, }, - selectedType: ?string, - setType: (type: string) => void, -}> { - render() { - const { appTypes, selectedType } = this.props; - - return ( -
- {Object.keys(appTypes).map((type: string) => ( -
- -
- ))} -
- ); - } - - onChange = (event: {target: {value: string}}) => { - this.props.setType(event.target.value); - } + selectedType: ?ApplicationType, + setType: (type: ApplicationType) => void, +}) { + return ( +
+ {Object.keys(appTypes).map((type: ApplicationType) => ( +
+ setType(type)} + skin={SKIN_LIGHT} + label={appTypes[type]} + value={type} + checked={selectedType === type} + /> +
+ ))} +
+ ); } diff --git a/src/components/dev/apps/applicationForm/MinecraftServerType.js b/src/components/dev/apps/applicationForm/MinecraftServerType.js index 4168683..bf8eae3 100644 --- a/src/components/dev/apps/applicationForm/MinecraftServerType.js +++ b/src/components/dev/apps/applicationForm/MinecraftServerType.js @@ -1,59 +1,53 @@ // @flow -import React, { Component } from 'react'; +import type {OauthAppResponse} from 'services/api/oauth'; +import React from 'react'; import { FormattedMessage as Message } from 'react-intl'; - import styles from 'components/profile/profileForm.scss'; -import messages from './ApplicationForm.intl.json'; - import { Input, FormModel } from 'components/ui/form'; import { SKIN_LIGHT } from 'components/ui'; -import type {OauthAppResponse} from 'services/api/oauth'; +import messages from './ApplicationForm.intl.json'; -export default class MinecraftServerType extends Component<{ +export default function MinecraftServerType({ form, app }: { form: FormModel, app: OauthAppResponse, -}> { - render() { - const { form, app } = this.props; - - return ( -
-
- -
- -
-

- -

-
-
- -
- -
-

- -

-
-
- -
+}) { + return ( +
+
+
- ); - } + +
+

+ +

+
+
+ +
+ +
+

+ +

+
+
+ +
+
+ ); } diff --git a/src/components/dev/apps/applicationForm/WebsiteType.js b/src/components/dev/apps/applicationForm/WebsiteType.js index 29667bb..d8dcf20 100644 --- a/src/components/dev/apps/applicationForm/WebsiteType.js +++ b/src/components/dev/apps/applicationForm/WebsiteType.js @@ -1,74 +1,68 @@ // @flow -import React, { Component } from 'react'; +import type { OauthAppResponse } from 'services/api/oauth'; +import React from 'react'; import { FormattedMessage as Message } from 'react-intl'; - -import styles from 'components/profile/profileForm.scss'; -import messages from './ApplicationForm.intl.json'; - import { Input, TextArea, FormModel } from 'components/ui/form'; import { SKIN_LIGHT } from 'components/ui'; +import styles from 'components/profile/profileForm.scss'; -import type { OauthAppResponse } from 'services/api/oauth'; +import messages from './ApplicationForm.intl.json'; -export default class WebsiteType extends Component<{ +export default function WebsiteType({ form, app }: { form: FormModel, app: OauthAppResponse, -}> { - render() { - const { form, app } = this.props; - - return ( -
-
- -
- -
-

- -

-
-
-