diff --git a/@types/formatjs.d.ts b/@types/formatjs.d.ts new file mode 100644 index 0000000..273fbd7 --- /dev/null +++ b/@types/formatjs.d.ts @@ -0,0 +1,2 @@ +declare module '@formatjs/intl-pluralrules/polyfill' {} +declare module '@formatjs/intl-relativetimeformat/polyfill' {} diff --git a/@types/redux-localstorage.d.ts b/@types/redux-localstorage.d.ts new file mode 100644 index 0000000..07130b3 --- /dev/null +++ b/@types/redux-localstorage.d.ts @@ -0,0 +1,25 @@ +// https://github.com/elgerlambert/redux-localstorage/issues/78#issuecomment-323609784 + +// import * as Redux from 'redux'; + +declare module 'redux-localstorage' { + export interface ConfigRS { + key: string; + merge?: any; + slicer?: any; + serialize?: ( + value: any, + replacer?: (key: string, value: any) => any, + space?: string | number, + ) => string; + deserialize?: ( + text: string, + reviver?: (key: any, value: any) => any, + ) => any; + } + + export default function persistState( + paths: string | string[], + config: ConfigRS, + ): () => any; +} diff --git a/@types/unexpected.d.ts b/@types/unexpected.d.ts new file mode 100644 index 0000000..491d67c --- /dev/null +++ b/@types/unexpected.d.ts @@ -0,0 +1,86 @@ +declare module 'unexpected' { + namespace unexpected { + interface EnchantedPromise extends Promise { + and = []>( + assertionName: string, + subject: unknown, + ...args: A + ): EnchantedPromise; + } + + interface Expect { + /** + * @see http://unexpected.js.org/api/expect/ + */ + = []>( + subject: unknown, + assertionName: string, + ...args: A + ): EnchantedPromise; + + it = []>( + assertionName: string, + subject?: unknown, + ...args: A + ): EnchantedPromise; + + /** + * @see http://unexpected.js.org/api/clone/ + */ + clone(): this; + + /** + * @see http://unexpected.js.org/api/addAssertion/ + */ + addAssertion = []>( + pattern: string, + handler: (expect: Expect, subject: T, ...args: A) => void, + ): this; + + /** + * @see http://unexpected.js.org/api/addType/ + */ + addType(typeDefinition: unexpected.TypeDefinition): this; + + /** + * @see http://unexpected.js.org/api/fail/ + */ + fail = []>(format: string, ...args: A): void; + fail(error: E): void; + + /** + * @see http://unexpected.js.org/api/freeze/ + */ + freeze(): this; + + /** + * @see http://unexpected.js.org/api/use/ + */ + use(plugin: unexpected.PluginDefinition): this; + } + + interface PluginDefinition { + name?: string; + version?: string; + dependencies?: Array; + installInto(expect: Expect): void; + } + + interface TypeDefinition { + name: string; + identify(value: unknown): value is T; + base?: string; + equal?(a: T, b: T, equal: (a: unknown, b: unknown) => boolean): boolean; + inspect?( + value: T, + depth: number, + output: any, + inspect: (value: unknown, depth: number) => any, + ): void; + } + } + + const unexpected: unexpected.Expect; + + export = unexpected; +} diff --git a/@types/webpack-loaders.d.ts b/@types/webpack-loaders.d.ts index 2948cb9..b198f9a 100644 --- a/@types/webpack-loaders.d.ts +++ b/@types/webpack-loaders.d.ts @@ -26,9 +26,7 @@ declare module '*.jpg' { declare module '*.intl.json' { import { MessageDescriptor } from 'react-intl'; - const descriptor: { - [key: string]: MessageDescriptor; - }; + const descriptor: Record; export = descriptor; } diff --git a/package.json b/package.json index e6960cc..62c2c66 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "license": "Apache-2.0", "repository": "https://github.com/elyby/accounts-frontend", "engines": { - "node": ">=10.0.0" + "node": ">=10.0.0", + "yarn": "1.19.1" }, "workspaces": [ "packages/*", @@ -124,6 +125,7 @@ "@storybook/addons": "^5.3.4", "@storybook/react": "^5.3.4", "@types/jest": "^24.9.0", + "@types/sinon": "^7.5.1", "@typescript-eslint/eslint-plugin": "^2.16.0", "@typescript-eslint/parser": "^2.16.0", "babel-loader": "^8.0.0", diff --git a/packages/app/components/accounts/AccountSwitcher.tsx b/packages/app/components/accounts/AccountSwitcher.tsx index a30e056..a5405ce 100644 --- a/packages/app/components/accounts/AccountSwitcher.tsx +++ b/packages/app/components/accounts/AccountSwitcher.tsx @@ -3,7 +3,7 @@ import { connect } from 'react-redux'; import clsx from 'clsx'; import { Link } from 'react-router-dom'; import { FormattedMessage as Message } from 'react-intl'; -import loader from 'app/services/loader'; +import * as loader from 'app/services/loader'; import { SKIN_DARK, COLOR_WHITE, Skin } from 'app/components/ui'; import { Button } from 'app/components/ui/form'; import { authenticate, revoke } from 'app/components/accounts/actions'; diff --git a/packages/app/components/accounts/actions.test.ts b/packages/app/components/accounts/actions.test.ts index 11a16e7..185f62a 100644 --- a/packages/app/components/accounts/actions.test.ts +++ b/packages/app/components/accounts/actions.test.ts @@ -12,13 +12,10 @@ import { } from 'app/components/accounts/actions'; import { add, - ADD, activate, - ACTIVATE, remove, reset, } from 'app/components/accounts/actions/pure-actions'; -import { SET_LOCALE } from 'app/components/i18n/actions'; import { updateUser, setUser } from 'app/components/user/actions'; import { setLogin, setAccountSwitcher } from 'app/components/auth/actions'; import { Dispatch, RootState } from 'app/reducers'; @@ -124,20 +121,20 @@ describe('components/accounts/actions', () => { ]), )); - it(`dispatches ${ADD} action`, () => + it(`dispatches accounts:add action`, () => authenticate(account)(dispatch, getState, undefined).then(() => expect(dispatch, 'to have a call satisfying', [add(account)]), )); - it(`dispatches ${ACTIVATE} action`, () => + it(`dispatches accounts:activate action`, () => authenticate(account)(dispatch, getState, undefined).then(() => expect(dispatch, 'to have a call satisfying', [activate(account)]), )); - it(`dispatches ${SET_LOCALE} action`, () => + it(`dispatches i18n:setLocale action`, () => authenticate(account)(dispatch, getState, undefined).then(() => expect(dispatch, 'to have a call satisfying', [ - { type: SET_LOCALE, payload: { locale: 'be' } }, + { type: 'i18n:setLocale', payload: { locale: 'be' } }, ]), )); @@ -479,7 +476,7 @@ describe('components/accounts/actions', () => { const key = `stranger${foreignAccount.id}`; beforeEach(() => { - sessionStorage.setItem(key, 1); + sessionStorage.setItem(key, '1'); logoutStrangers()(dispatch, getState, undefined); }); diff --git a/packages/app/components/accounts/actions.ts b/packages/app/components/accounts/actions.ts index 1c2c5d1..5962d58 100644 --- a/packages/app/components/accounts/actions.ts +++ b/packages/app/components/accounts/actions.ts @@ -90,7 +90,7 @@ export function authenticate( if (!newRefreshToken) { // mark user as stranger (user does not want us to remember his account) - sessionStorage.setItem(`stranger${newAccount.id}`, 1); + sessionStorage.setItem(`stranger${newAccount.id}`, '1'); } if (auth && auth.oauth && auth.oauth.clientId) { diff --git a/packages/app/components/accounts/actions/pure-actions.ts b/packages/app/components/accounts/actions/pure-actions.ts index 3859f5c..c65d464 100644 --- a/packages/app/components/accounts/actions/pure-actions.ts +++ b/packages/app/components/accounts/actions/pure-actions.ts @@ -1,78 +1,67 @@ -import { - Account, - AddAction, - RemoveAction, - ActivateAction, - UpdateTokenAction, - ResetAction, -} from '../reducer'; +import { Action as ReduxAction } from 'redux'; +import { Account } from 'app/components/accounts/reducer'; + +interface AddAction extends ReduxAction { + type: 'accounts:add'; + payload: Account; +} -export const ADD = 'accounts:add'; -/** - * @private - * - * @param {Account} account - * - * @returns {object} - action definition - */ export function add(account: Account): AddAction { return { - type: ADD, + type: 'accounts:add', payload: account, }; } -export const REMOVE = 'accounts:remove'; -/** - * @private - * - * @param {Account} account - * - * @returns {object} - action definition - */ +interface RemoveAction extends ReduxAction { + type: 'accounts:remove'; + payload: Account; +} + export function remove(account: Account): RemoveAction { return { - type: REMOVE, + type: 'accounts:remove', payload: account, }; } -export const ACTIVATE = 'accounts:activate'; -/** - * @private - * - * @param {Account} account - * - * @returns {object} - action definition - */ +interface ActivateAction extends ReduxAction { + type: 'accounts:activate'; + payload: Account; +} + export function activate(account: Account): ActivateAction { return { - type: ACTIVATE, + type: 'accounts:activate', payload: account, }; } -export const RESET = 'accounts:reset'; -/** - * @private - * - * @returns {object} - action definition - */ +interface ResetAction extends ReduxAction { + type: 'accounts:reset'; +} + export function reset(): ResetAction { return { - type: RESET, + type: 'accounts:reset', }; } -export const UPDATE_TOKEN = 'accounts:updateToken'; -/** - * @param {string} token - * - * @returns {object} - action definition - */ +interface UpdateTokenAction extends ReduxAction { + type: 'accounts:updateToken'; + payload: string; +} + export function updateToken(token: string): UpdateTokenAction { return { - type: UPDATE_TOKEN, + type: 'accounts:updateToken', payload: token, }; } + +export type Action = + | AddAction + | RemoveAction + | ActivateAction + | ResetAction + | UpdateTokenAction; diff --git a/packages/app/components/accounts/reducer.test.ts b/packages/app/components/accounts/reducer.test.ts index c4f5fa9..4ec1c47 100644 --- a/packages/app/components/accounts/reducer.test.ts +++ b/packages/app/components/accounts/reducer.test.ts @@ -1,17 +1,8 @@ import expect from 'app/test/unexpected'; import { updateToken } from './actions'; -import { - add, - remove, - activate, - reset, - ADD, - REMOVE, - ACTIVATE, - UPDATE_TOKEN, - RESET, -} from './actions/pure-actions'; +import { add, remove, activate, reset } from './actions/pure-actions'; +import { AccountsState } from './index'; import accounts, { Account } from './reducer'; const account: Account = { @@ -22,7 +13,7 @@ const account: Account = { } as Account; describe('Accounts reducer', () => { - let initial; + let initial: AccountsState; beforeEach(() => { initial = accounts(undefined, {} as any); @@ -39,7 +30,7 @@ describe('Accounts reducer', () => { state: 'foo', })); - describe(ACTIVATE, () => { + describe('accounts:activate', () => { it('sets active account', () => { expect(accounts(initial, activate(account)), 'to satisfy', { active: account.id, @@ -47,7 +38,7 @@ describe('Accounts reducer', () => { }); }); - describe(ADD, () => { + describe('accounts:add', () => { it('adds an account', () => expect(accounts(initial, add(account)), 'to satisfy', { available: [account], @@ -106,7 +97,7 @@ describe('Accounts reducer', () => { }); }); - describe(REMOVE, () => { + describe('accounts:remove', () => { it('should remove an account', () => expect( accounts({ ...initial, available: [account] }, remove(account)), @@ -128,7 +119,7 @@ describe('Accounts reducer', () => { }); }); - describe(RESET, () => { + describe('actions:reset', () => { it('should reset accounts state', () => expect( accounts({ ...initial, available: [account] }, reset()), @@ -137,7 +128,7 @@ describe('Accounts reducer', () => { )); }); - describe(UPDATE_TOKEN, () => { + describe('accounts:updateToken', () => { it('should update token', () => { const newToken = 'newToken'; diff --git a/packages/app/components/accounts/reducer.ts b/packages/app/components/accounts/reducer.ts index c18bd16..389f79e 100644 --- a/packages/app/components/accounts/reducer.ts +++ b/packages/app/components/accounts/reducer.ts @@ -1,3 +1,5 @@ +import { Action } from './actions/pure-actions'; + export type Account = { id: number; username: string; @@ -8,25 +10,9 @@ export type Account = { export type State = { active: number | null; - available: Account[]; + available: Array; }; -export type AddAction = { type: 'accounts:add'; payload: Account }; -export type RemoveAction = { type: 'accounts:remove'; payload: Account }; -export type ActivateAction = { type: 'accounts:activate'; payload: Account }; -export type UpdateTokenAction = { - type: 'accounts:updateToken'; - payload: string; -}; -export type ResetAction = { type: 'accounts:reset' }; - -type Action = - | AddAction - | RemoveAction - | ActivateAction - | UpdateTokenAction - | ResetAction; - export function getActiveAccount(state: { accounts: State }): Account | null { const accountId = state.accounts.active; diff --git a/packages/app/components/auth/BaseAuthBody.tsx b/packages/app/components/auth/BaseAuthBody.tsx index 07228ee..7692663 100644 --- a/packages/app/components/auth/BaseAuthBody.tsx +++ b/packages/app/components/auth/BaseAuthBody.tsx @@ -1,7 +1,8 @@ -import React from 'react'; +import React, { ReactNode } from 'react'; +import { RouteComponentProps } from 'react-router-dom'; + import AuthError from 'app/components/auth/authError/AuthError'; import { FormModel } from 'app/components/ui/form'; -import { RouteComponentProps } from 'react-router-dom'; import Context, { AuthContext } from './Context'; @@ -11,7 +12,7 @@ import Context, { AuthContext } from './Context'; class BaseAuthBody extends React.Component< // TODO: this may be converted to generic type RouteComponentProps - RouteComponentProps<{ [key: string]: any }> + RouteComponentProps> > { static contextType = Context; /* TODO: use declare */ context: React.ContextType; @@ -32,10 +33,14 @@ class BaseAuthBody extends React.Component< this.prevErrors = this.context.auth.error; } - renderErrors() { + renderErrors(): ReactNode { const error = this.form.getFirstError(); - return error && ; + if (error === null) { + return null; + } + + return ; } onFormSubmit() { diff --git a/packages/app/components/auth/PanelTransition.tsx b/packages/app/components/auth/PanelTransition.tsx index 5e1a88e..bd2fddb 100644 --- a/packages/app/components/auth/PanelTransition.tsx +++ b/packages/app/components/auth/PanelTransition.tsx @@ -1,8 +1,15 @@ -import React from 'react'; +import React, { CSSProperties, MouseEventHandler, ReactElement, ReactNode } from 'react'; import { AccountsState } from 'app/components/accounts'; import { User } from 'app/components/user'; import { connect } from 'react-redux'; -import { TransitionMotion, spring } from 'react-motion'; +import { + TransitionMotion, + spring, + PlainStyle, + Style, + TransitionStyle, + TransitionPlainStyle, +} from 'react-motion'; import { Panel, PanelBody, @@ -44,7 +51,7 @@ type PanelId = string; * - Panel index defines the direction of X transition of both panels * (e.g. the panel with lower index will slide from left side, and with greater from right side) */ -const contexts: Array = [ +const contexts: Array> = [ ['login', 'password', 'forgotPassword', 'mfa', 'recoverPassword'], ['register', 'activation', 'resendActivation'], ['acceptRules'], @@ -70,40 +77,41 @@ if (process.env.NODE_ENV !== 'production') { }); return acc; - }, {}); + }, {} as Record>); } type ValidationError = | string | { type: string; - payload: { [key: string]: any }; + payload: Record; }; -type AnimationProps = { +interface AnimationStyle extends PlainStyle { opacitySpring: number; transformSpring: number; -}; +} -type AnimationContext = { +interface AnimationData { + Title: ReactElement; + Body: ReactElement; + Footer: ReactElement; + Links: ReactNode; + hasBackButton: boolean | ((props: Props) => boolean); +} + +interface AnimationContext extends TransitionPlainStyle { key: PanelId; - style: AnimationProps; - data: { - Title: React.ReactElement; - Body: React.ReactElement; - Footer: React.ReactElement; - Links: React.ReactElement; - hasBackButton: boolean | ((props: Props) => boolean); - }; -}; + style: AnimationStyle; + data: AnimationData; +} -type OwnProps = { - Title: React.ReactElement; - Body: React.ReactElement; - Footer: React.ReactElement; - Links: React.ReactElement; - children?: React.ReactElement; -}; +interface OwnProps { + Title: ReactElement; + Body: ReactElement; + Footer: ReactElement; + Links: ReactNode; +} interface Props extends OwnProps { // context props @@ -114,17 +122,18 @@ interface Props extends OwnProps { resolve: () => void; reject: () => void; - setErrors: (errors: { [key: string]: ValidationError }) => void; + setErrors: (errors: Record) => void; } -type State = { +interface State { contextHeight: number; panelId: PanelId | void; prevPanelId: PanelId | void; isHeightDirty: boolean; forceHeight: 1 | 0; direction: 'X' | 'Y'; -}; + formsHeights: Record; +} class PanelTransition extends React.PureComponent { state: State = { @@ -134,16 +143,17 @@ class PanelTransition extends React.PureComponent { forceHeight: 0 as const, direction: 'X' as const, prevPanelId: undefined, + formsHeights: {}, }; isHeightMeasured: boolean = false; wasAutoFocused: boolean = false; - body: null | { + body: { autoFocus: () => void; onFormSubmit: () => void; - } = null; + } | null = null; - timerIds: NodeJS.Timeout[] = []; // this is a list of a probably running timeouts to clean on unmount + timerIds: Array = []; // this is a list of a probably running timeouts to clean on unmount componentDidUpdate(prevProps: Props) { const nextPanel: PanelId = @@ -166,7 +176,8 @@ class PanelTransition extends React.PureComponent { if (forceHeight) { this.timerIds.push( - setTimeout(() => { + // https://stackoverflow.com/a/51040768/5184751 + window.setTimeout(() => { this.setState({ forceHeight: 0 }); }, 100), ); @@ -208,7 +219,7 @@ class PanelTransition extends React.PureComponent { hasGoBack: boolean; } = Body.type as any; - const formHeight = this.state[`formHeight${panelId}`] || 0; + const formHeight = this.state.formsHeights[panelId] || 0; // a hack to disable height animation on first render const { isHeightMeasured } = this; @@ -310,7 +321,7 @@ class PanelTransition extends React.PureComponent { ); } - onFormSubmit = () => { + onFormSubmit = (): void => { this.props.clearErrors(); if (this.body) { @@ -318,29 +329,28 @@ class PanelTransition extends React.PureComponent { } }; - onFormInvalid = (errors: { [key: string]: ValidationError }) => + onFormInvalid = (errors: Record): void => this.props.setErrors(errors); - willEnter = (config: AnimationContext) => this.getTransitionStyles(config); - willLeave = (config: AnimationContext) => - this.getTransitionStyles(config, { isLeave: true }); + willEnter = (config: TransitionStyle): PlainStyle => { + const transform = this.getTransformForPanel(config.key); - /** - * @param {object} config - * @param {string} config.key - * @param {object} [options] - * @param {object} [options.isLeave=false] - true, if this is a leave transition - * - * @returns {object} - */ - getTransitionStyles( - { key }: AnimationContext, - options: { isLeave?: boolean } = {}, - ): { - transformSpring: number; - opacitySpring: number; - } { - const { isLeave = false } = options; + return { + transformSpring: transform, + opacitySpring: 1, + }; + }; + + willLeave = (config: TransitionStyle): Style => { + const transform = this.getTransformForPanel(config.key); + + return { + transformSpring: spring(transform, transformSpringConfig), + opacitySpring: spring(0, opacitySpringConfig), + }; + }; + + getTransformForPanel(key: PanelId): number { const { panelId, prevPanelId } = this.state; const fromLeft = -1; @@ -363,14 +373,7 @@ class PanelTransition extends React.PureComponent { sign *= -1; } - const transform = sign * 100; - - return { - transformSpring: isLeave - ? spring(transform, transformSpringConfig) - : transform, - opacitySpring: isLeave ? spring(0, opacitySpringConfig) : 1, - }; + return sign * 100; } getDirection(next: PanelId, prev: PanelId): 'X' | 'Y' { @@ -383,24 +386,23 @@ class PanelTransition extends React.PureComponent { return context.includes(next) ? 'X' : 'Y'; } - onUpdateHeight = (height: number, key: PanelId) => { - const heightKey = `formHeight${key}`; - - // @ts-ignore + onUpdateHeight = (height: number, key: PanelId): void => { this.setState({ - [heightKey]: height, + formsHeights: { + ...this.state.formsHeights, + [key]: height, + }, }); }; - onUpdateContextHeight = (height: number) => { + onUpdateContextHeight = (height: number): void => { this.setState({ contextHeight: height, }); }; - onGoBack = (event: React.MouseEvent) => { + onGoBack: MouseEventHandler = (event): void => { event.preventDefault(); - authFlow.goBack(); }; @@ -409,7 +411,7 @@ class PanelTransition extends React.PureComponent { * * @param {number} length number of panels transitioned */ - tryToAutoFocus(length: number) { + tryToAutoFocus(length: number): void { if (!this.body) { return; } @@ -425,20 +427,17 @@ class PanelTransition extends React.PureComponent { } } - shouldMeasureHeight() { + shouldMeasureHeight(): string { const { user, accounts, auth } = this.props; const { isHeightDirty } = this.state; - const errorString = Object.values(auth.error || {}).reduce( - (acc: string, item: ValidationError): string => { - if (typeof item === 'string') { - return acc + item; - } + const errorString = Object.values(auth.error || {}).reduce((acc, item) => { + if (typeof item === 'string') { + return acc + item; + } - return acc + item.type; - }, - '', - ) as string; + return acc + item.type; + }, '') as string; return [ errorString, @@ -448,9 +447,9 @@ class PanelTransition extends React.PureComponent { ].join(''); } - getHeader({ key, style, data }: AnimationContext) { - const { Title } = data; - const { transformSpring } = style; + getHeader({ key, style, data }: TransitionPlainStyle): ReactElement { + const { Title } = data as AnimationData; + const { transformSpring } = (style as unknown) as AnimationStyle; let { hasBackButton } = data; @@ -459,7 +458,10 @@ class PanelTransition extends React.PureComponent { } const transitionStyle = { - ...this.getDefaultTransitionStyles(key, style), + ...this.getDefaultTransitionStyles( + key, + (style as unknown) as AnimationStyle, + ), opacity: 1, // reset default }; @@ -491,15 +493,12 @@ class PanelTransition extends React.PureComponent { ); } - getBody({ key, style, data }: AnimationContext) { - const { Body } = data; - const { transformSpring } = style; + getBody({ key, style, data }: TransitionPlainStyle): ReactElement { + const { Body } = data as AnimationData; + const { transformSpring } = (style as unknown) as AnimationStyle; const { direction } = this.state; - let transform: { [key: string]: string } = this.translate( - transformSpring, - direction, - ); + let transform = this.translate(transformSpring, direction); let verticalOrigin = 'top'; if (direction === 'Y') { @@ -507,8 +506,11 @@ class PanelTransition extends React.PureComponent { transform = {}; } - const transitionStyle = { - ...this.getDefaultTransitionStyles(key, style), + const transitionStyle: CSSProperties = { + ...this.getDefaultTransitionStyles( + key, + (style as unknown) as AnimationStyle, + ), top: 'auto', // reset default [verticalOrigin]: 0, ...transform, @@ -522,6 +524,7 @@ class PanelTransition extends React.PureComponent { onMeasure={height => this.onUpdateHeight(height, key)} > {React.cloneElement(Body, { + // @ts-ignore ref: body => { this.body = body; }, @@ -530,10 +533,13 @@ class PanelTransition extends React.PureComponent { ); } - getFooter({ key, style, data }: AnimationContext) { - const { Footer } = data; + getFooter({ key, style, data }: TransitionPlainStyle): ReactElement { + const { Footer } = data as AnimationData; - const transitionStyle = this.getDefaultTransitionStyles(key, style); + const transitionStyle = this.getDefaultTransitionStyles( + key, + (style as unknown) as AnimationStyle, + ); return (
@@ -542,10 +548,13 @@ class PanelTransition extends React.PureComponent { ); } - getLinks({ key, style, data }: AnimationContext) { - const { Links } = data; + getLinks({ key, style, data }: TransitionPlainStyle): ReactElement { + const { Links } = data as AnimationData; - const transitionStyle = this.getDefaultTransitionStyles(key, style); + const transitionStyle = this.getDefaultTransitionStyles( + key, + (style as unknown) as AnimationStyle, + ); return (
@@ -554,16 +563,9 @@ class PanelTransition extends React.PureComponent { ); } - /** - * @param {string} key - * @param {object} style - * @param {number} style.opacitySpring - * - * @returns {object} - */ getDefaultTransitionStyles( key: string, - { opacitySpring }: Readonly, + { opacitySpring }: Readonly, ): { position: 'absolute'; top: number; @@ -582,7 +584,11 @@ class PanelTransition extends React.PureComponent { }; } - translate(value: number, direction: 'X' | 'Y' = 'X', unit: '%' | 'px' = '%') { + translate( + value: number, + direction: 'X' | 'Y' = 'X', + unit: '%' | 'px' = '%', + ): CSSProperties { return { WebkitTransform: `translate${direction}(${value}${unit})`, transform: `translate${direction}(${value}${unit})`, diff --git a/packages/app/components/auth/RejectionLink.tsx b/packages/app/components/auth/RejectionLink.tsx index 78f1a76..f63382d 100644 --- a/packages/app/components/auth/RejectionLink.tsx +++ b/packages/app/components/auth/RejectionLink.tsx @@ -1,20 +1,22 @@ -import React, { useContext } from 'react'; +import React, { ComponentType, useContext } from 'react'; import { FormattedMessage as Message, MessageDescriptor } from 'react-intl'; import Context, { AuthContext } from './Context'; interface Props { isAvailable?: (context: AuthContext) => boolean; - payload?: { [key: string]: any }; + payload?: Record; label: MessageDescriptor; } -export type RejectionLinkProps = Props; - -function RejectionLink(props: Props) { +const RejectionLink: ComponentType = ({ + isAvailable, + payload, + label, +}) => { const context = useContext(Context); - if (props.isAvailable && !props.isAvailable(context)) { + if (isAvailable && !isAvailable(context)) { // TODO: if want to properly support multiple links, we should control // the dividers ' | ' rendered from factory too return null; @@ -26,12 +28,12 @@ function RejectionLink(props: Props) { onClick={event => { event.preventDefault(); - context.reject(props.payload); + context.reject(payload); }} > - + ); -} +}; export default RejectionLink; diff --git a/packages/app/components/auth/acceptRules/AcceptRulesBody.js b/packages/app/components/auth/acceptRules/AcceptRulesBody.tsx similarity index 100% rename from packages/app/components/auth/acceptRules/AcceptRulesBody.js rename to packages/app/components/auth/acceptRules/AcceptRulesBody.tsx diff --git a/packages/app/components/auth/actions.test.ts b/packages/app/components/auth/actions.test.ts index 8fa5bc0..2374da5 100644 --- a/packages/app/components/auth/actions.test.ts +++ b/packages/app/components/auth/actions.test.ts @@ -1,3 +1,4 @@ +import { Action as ReduxAction } from 'redux'; import sinon from 'sinon'; import expect from 'app/test/unexpected'; @@ -15,33 +16,36 @@ import { login, setLogin, } from 'app/components/auth/actions'; +import { OauthData, OAuthValidateResponse } from '../../services/api/oauth'; -const oauthData = { +const oauthData: OauthData = { clientId: '', redirectUrl: '', responseType: '', scope: '', state: '', + prompt: 'none', }; describe('components/auth/actions', () => { const dispatch = sinon.stub().named('store.dispatch'); const getState = sinon.stub().named('store.getState'); - function callThunk(fn, ...args) { + function callThunk, F extends (...args: A) => any>( + fn: F, + ...args: A + ): Promise { const thunk = fn(...args); return thunk(dispatch, getState); } - function expectDispatchCalls(calls) { - expect( - dispatch, - 'to have calls satisfying', - [[setLoadingState(true)]] - .concat(calls) - .concat([[setLoadingState(false)]]), - ); + function expectDispatchCalls(calls: Array>) { + expect(dispatch, 'to have calls satisfying', [ + [setLoadingState(true)], + ...calls, + [setLoadingState(false)], + ]); } beforeEach(() => { @@ -58,14 +62,20 @@ describe('components/auth/actions', () => { }); describe('#oAuthValidate()', () => { - let resp; + let resp: OAuthValidateResponse; beforeEach(() => { resp = { - client: { id: 123 }, - oAuth: { state: 123 }, + client: { + id: '123', + name: '', + description: '', + }, + oAuth: { + state: 123, + }, session: { - scopes: ['scopes'], + scopes: ['account_info'], }, }; diff --git a/packages/app/components/auth/actions.ts b/packages/app/components/auth/actions.ts index 38048ba..23fde96 100644 --- a/packages/app/components/auth/actions.ts +++ b/packages/app/components/auth/actions.ts @@ -1,7 +1,8 @@ +import { Action as ReduxAction } from 'redux'; import { browserHistory } from 'app/services/history'; import logger from 'app/services/logger'; import localStorage from 'app/services/localStorage'; -import loader from 'app/services/loader'; +import * as loader from 'app/services/loader'; import history from 'app/services/history'; import { updateUser, @@ -15,22 +16,27 @@ import { recoverPassword as recoverPasswordEndpoint, OAuthResponse, } from 'app/services/api/authentication'; -import oauth, { OauthData, Client, Scope } from 'app/services/api/oauth'; -import signup from 'app/services/api/signup'; +import oauth, { OauthData, Scope } from 'app/services/api/oauth'; +import { + register as registerEndpoint, + activate as activateEndpoint, + resendActivation as resendActivationEndpoint, +} from 'app/services/api/signup'; import dispatchBsod from 'app/components/ui/bsod/dispatchBsod'; import { create as createPopup } from 'app/components/ui/popup/actions'; import ContactForm from 'app/components/contact/ContactForm'; import { Account } from 'app/components/accounts/reducer'; import { ThunkAction, Dispatch } from 'app/reducers'; +import { Resp } from 'app/services/request'; -import { getCredentials } from './reducer'; +import { Credentials, Client, OAuthState, getCredentials } from './reducer'; -type ValidationError = - | string - | { - type: string; - payload: { [key: string]: any }; - }; +interface ValidationErrorLiteral { + type: string; + payload: Record; +} + +type ValidationError = string | ValidationErrorLiteral; /** * Routes user to the previous page if it is possible @@ -169,16 +175,15 @@ export function register({ rulesAgreement: boolean; }) { return wrapInLoader((dispatch, getState) => - signup - .register({ - email, - username, - password, - rePassword, - rulesAgreement, - lang: getState().user.lang, - captcha, - }) + registerEndpoint({ + email, + username, + password, + rePassword, + rulesAgreement, + lang: getState().user.lang, + captcha, + }) .then(() => { dispatch( updateUser({ @@ -201,8 +206,7 @@ export function activate({ key: string; }): ThunkAction> { return wrapInLoader(dispatch => - signup - .activate({ key }) + activateEndpoint(key) .then(authHandler(dispatch)) .catch(validationErrorsHandler(dispatch, '/resend-activation')), ); @@ -216,8 +220,7 @@ export function resendActivation({ captcha: string; }) { return wrapInLoader(dispatch => - signup - .resendActivation({ email, captcha }) + resendActivationEndpoint(email, captcha) .then(resp => { dispatch( updateUser({ @@ -235,25 +238,26 @@ export function contactUs() { return createPopup({ Popup: ContactForm }); } -export const SET_CREDENTIALS = 'auth:setCredentials'; +interface SetCredentialsAction extends ReduxAction { + type: 'auth:setCredentials'; + payload: Credentials | null; +} + +function setCredentials(payload: Credentials | null): SetCredentialsAction { + return { + type: 'auth:setCredentials', + payload, + }; +} + /** * Sets login in credentials state - * * Resets the state, when `null` is passed * - * @param {string|null} login - * - * @returns {object} + * @param login */ -export function setLogin(login: string | null) { - return { - type: SET_CREDENTIALS, - payload: login - ? { - login, - } - : null, - }; +export function setLogin(login: string | null): SetCredentialsAction { + return setCredentials(login ? { login } : null); } export function relogin(login: string | null): ThunkAction { @@ -262,19 +266,20 @@ export function relogin(login: string | null): ThunkAction { const returnUrl = credentials.returnUrl || location.pathname + location.search; - dispatch({ - type: SET_CREDENTIALS, - payload: { + dispatch( + setCredentials({ login, returnUrl, isRelogin: true, - }, - }); + }), + ); browserHistory.push('/login'); }; } +export type CredentialsAction = SetCredentialsAction; + function requestTotp({ login, password, @@ -288,41 +293,55 @@ function requestTotp({ // merging with current credentials to propogate returnUrl const credentials = getCredentials(getState()); - dispatch({ - type: SET_CREDENTIALS, - payload: { + dispatch( + setCredentials({ ...credentials, login, password, rememberMe, isTotpRequired: true, - }, - }); + }), + ); }; } -export const SET_SWITCHER = 'auth:setAccountSwitcher'; -export function setAccountSwitcher(isOn: boolean) { +interface SetSwitcherAction extends ReduxAction { + type: 'auth:setAccountSwitcher'; + payload: boolean; +} + +export function setAccountSwitcher(isOn: boolean): SetSwitcherAction { return { - type: SET_SWITCHER, + type: 'auth:setAccountSwitcher', payload: isOn, }; } -export const ERROR = 'auth:error'; -export function setErrors(errors: { [key: string]: ValidationError } | null) { +export type AccountSwitcherAction = SetSwitcherAction; + +interface SetErrorAction extends ReduxAction { + type: 'auth:error'; + payload: Record | null; + error: boolean; +} + +export function setErrors( + errors: Record | null, +): SetErrorAction { return { - type: ERROR, + type: 'auth:error', payload: errors, error: true, }; } -export function clearErrors() { +export function clearErrors(): SetErrorAction { return setErrors(null); } -const KNOWN_SCOPES = [ +export type ErrorAction = SetErrorAction; + +const KNOWN_SCOPES: ReadonlyArray = [ 'minecraft_server_session', 'offline_access', 'account_info', @@ -470,11 +489,76 @@ function handleOauthParamsValidation( return Promise.reject(resp); } -export const SET_CLIENT = 'set_client'; -export function setClient({ id, name, description }: Client) { +interface SetClientAction extends ReduxAction { + type: 'set_client'; + payload: Client; +} + +export function setClient(payload: Client): SetClientAction { return { - type: SET_CLIENT, - payload: { id, name, description }, + type: 'set_client', + payload, + }; +} + +export type ClientAction = SetClientAction; + +interface SetOauthAction extends ReduxAction { + type: 'set_oauth'; + payload: Pick< + OAuthState, + | 'clientId' + | 'redirectUrl' + | 'responseType' + | 'scope' + | 'prompt' + | 'loginHint' + | 'state' + >; +} + +// Input data is coming right from the query string, so the names +// are the same, as used for initializing OAuth2 request +export function setOAuthRequest(data: { + client_id?: string; + redirect_uri?: string; + response_type?: string; + scope?: string; + prompt?: string; + loginHint?: string; + state?: string; +}): SetOauthAction { + return { + type: 'set_oauth', + payload: { + // TODO: there is too much default empty string. Maybe we can somehow validate it + // on the level, where this action is called? + clientId: data.client_id || '', + redirectUrl: data.redirect_uri || '', + responseType: data.response_type || '', + scope: data.scope || '', + prompt: data.prompt || '', + loginHint: data.loginHint || '', + state: data.state || '', + }, + }; +} + +interface SetOAuthResultAction extends ReduxAction { + type: 'set_oauth_result'; + payload: Pick; +} + +export const SET_OAUTH_RESULT = 'set_oauth_result'; // TODO: remove + +export function setOAuthCode(payload: { + success: boolean; + code: string; + displayCode: boolean; +}): SetOAuthResultAction { + return { + type: 'set_oauth_result', + payload, }; } @@ -507,69 +591,43 @@ export function resetAuth(): ThunkAction { }; } -export const SET_OAUTH = 'set_oauth'; -export function setOAuthRequest(data: { - client_id?: string; - redirect_uri?: string; - response_type?: string; - scope?: string; - prompt?: string; - loginHint?: string; - state?: string; -}) { +interface RequestPermissionsAcceptAction extends ReduxAction { + type: 'require_permissions_accept'; +} + +export function requirePermissionsAccept(): RequestPermissionsAcceptAction { return { - type: SET_OAUTH, - payload: { - clientId: data.client_id, - redirectUrl: data.redirect_uri, - responseType: data.response_type, - scope: data.scope, - prompt: data.prompt, - loginHint: data.loginHint, - state: data.state, - }, + type: 'require_permissions_accept', }; } -export const SET_OAUTH_RESULT = 'set_oauth_result'; -export function setOAuthCode(data: { - success: boolean; - code: string; - displayCode: boolean; -}) { +export type OAuthAction = + | SetOauthAction + | SetOAuthResultAction + | RequestPermissionsAcceptAction; + +interface SetScopesAction extends ReduxAction { + type: 'set_scopes'; + payload: Array; +} + +export function setScopes(payload: Array): SetScopesAction { return { - type: SET_OAUTH_RESULT, - payload: { - success: data.success, - code: data.code, - displayCode: data.displayCode, - }, + type: 'set_scopes', + payload, }; } -export const REQUIRE_PERMISSIONS_ACCEPT = 'require_permissions_accept'; -export function requirePermissionsAccept() { - return { - type: REQUIRE_PERMISSIONS_ACCEPT, - }; +export type ScopesAction = SetScopesAction; + +interface SetLoadingAction extends ReduxAction { + type: 'set_loading_state'; + payload: boolean; } -export const SET_SCOPES = 'set_scopes'; -export function setScopes(scopes: Scope[]) { - if (!Array.isArray(scopes)) { - throw new Error('Scopes must be array'); - } - +export function setLoadingState(isLoading: boolean): SetLoadingAction { return { - type: SET_SCOPES, - payload: scopes, - }; -} - -export const SET_LOADING_STATE = 'set_loading_state'; -export function setLoadingState(isLoading: boolean) { - return { - type: SET_LOADING_STATE, + type: 'set_loading_state', payload: isLoading, }; } @@ -594,6 +652,8 @@ function wrapInLoader(fn: ThunkAction>): ThunkAction> { }; } +export type LoadingAction = SetLoadingAction; + function needActivation() { return updateUser({ isActive: false, @@ -615,12 +675,20 @@ function authHandler(dispatch: Dispatch) { }); } -function validationErrorsHandler(dispatch: Dispatch, repeatUrl?: string) { +function validationErrorsHandler( + dispatch: Dispatch, + repeatUrl?: string, +): ( + resp: Resp<{ + errors?: Record; + data?: Record; + }>, +) => Promise { return resp => { if (resp.errors) { const [firstError] = Object.keys(resp.errors); - const error = { - type: resp.errors[firstError], + const firstErrorObj: ValidationError = { + type: resp.errors[firstError] as string, payload: { isGuest: true, repeatUrl: '', @@ -629,20 +697,24 @@ function validationErrorsHandler(dispatch: Dispatch, repeatUrl?: string) { if (resp.data) { // TODO: this should be formatted on backend - Object.assign(error.payload, resp.data); + Object.assign(firstErrorObj.payload, resp.data); } if ( - ['error.key_not_exists', 'error.key_expire'].includes(error.type) && + ['error.key_not_exists', 'error.key_expire'].includes( + firstErrorObj.type, + ) && repeatUrl ) { // TODO: this should be formatted on backend - error.payload.repeatUrl = repeatUrl; + firstErrorObj.payload.repeatUrl = repeatUrl; } - resp.errors[firstError] = error; + // TODO: can I clone the object or its necessary to catch modified errors list on corresponding catches? + const errors: Record = resp.errors; + errors[firstError] = firstErrorObj; - dispatch(setErrors(resp.errors)); + dispatch(setErrors(errors)); } return Promise.reject(resp); diff --git a/packages/app/components/auth/activation/ActivationBody.js b/packages/app/components/auth/activation/ActivationBody.tsx similarity index 89% rename from packages/app/components/auth/activation/ActivationBody.js rename to packages/app/components/auth/activation/ActivationBody.tsx index a949715..45dc4cd 100644 --- a/packages/app/components/auth/activation/ActivationBody.js +++ b/packages/app/components/auth/activation/ActivationBody.tsx @@ -1,4 +1,3 @@ -import PropTypes from 'prop-types'; import React from 'react'; import { FormattedMessage as Message } from 'react-intl'; @@ -13,14 +12,6 @@ export default class ActivationBody extends BaseAuthBody { static displayName = 'ActivationBody'; static panelId = 'activation'; - static propTypes = { - match: PropTypes.shape({ - params: PropTypes.shape({ - key: PropTypes.string, - }), - }), - }; - autoFocusField = this.props.match.params && this.props.match.params.key ? null : 'key'; diff --git a/packages/app/components/auth/authError/AuthError.js b/packages/app/components/auth/authError/AuthError.js deleted file mode 100644 index 93833ad..0000000 --- a/packages/app/components/auth/authError/AuthError.js +++ /dev/null @@ -1,45 +0,0 @@ -import PropTypes from 'prop-types'; -import React from 'react'; - -import errorsDict from 'app/services/errorsDict'; -import { PanelBodyHeader } from 'app/components/ui/Panel'; - -let autoHideTimer; -function resetTimer() { - if (autoHideTimer) { - clearTimeout(autoHideTimer); - autoHideTimer = null; - } -} -export default function AuthError({ error, onClose = function() {} }) { - resetTimer(); - - if (error.payload && error.payload.canRepeatIn) { - error.payload.msLeft = error.payload.canRepeatIn * 1000; - setTimeout(onClose, error.payload.msLeft - Date.now() + 1500); // 1500 to let the user see, that time is elapsed - } - - return ( - { - resetTimer(); - onClose(); - }} - > - {errorsDict.resolve(error)} - - ); -} - -AuthError.displayName = 'AuthError'; -AuthError.propTypes = { - error: PropTypes.oneOfType([ - PropTypes.string, - PropTypes.shape({ - type: PropTypes.string, - payload: PropTypes.object, - }), - ]).isRequired, - onClose: PropTypes.func, -}; diff --git a/packages/app/components/auth/authError/AuthError.tsx b/packages/app/components/auth/authError/AuthError.tsx new file mode 100644 index 0000000..08b29b0 --- /dev/null +++ b/packages/app/components/auth/authError/AuthError.tsx @@ -0,0 +1,45 @@ +import React, { ComponentType, useEffect } from 'react'; + +import { resolve as resolveError } from 'app/services/errorsDict'; +import { PanelBodyHeader } from 'app/components/ui/Panel'; +import { ValidationError } from 'app/components/ui/form/FormModel'; + +interface Props { + error: ValidationError; + onClose?: () => void; +} + +let autoHideTimer: number | null = null; +function resetTimeout(): void { + if (autoHideTimer) { + clearTimeout(autoHideTimer); + autoHideTimer = null; + } +} + +const AuthError: ComponentType = ({ error, onClose }) => { + useEffect(() => { + resetTimeout(); + + if ( + onClose && + typeof error !== 'string' && + error.payload && + error.payload.canRepeatIn + ) { + const msLeft = error.payload.canRepeatIn * 1000; + // 1500 to let the user see, that time is elapsed + setTimeout(onClose, msLeft - Date.now() + 1500); + } + + return resetTimeout; + }, [error, onClose]); + + return ( + + {resolveError(error)} + + ); +}; + +export default AuthError; diff --git a/packages/app/components/auth/chooseAccount/ChooseAccountBody.js b/packages/app/components/auth/chooseAccount/ChooseAccountBody.tsx similarity index 92% rename from packages/app/components/auth/chooseAccount/ChooseAccountBody.js rename to packages/app/components/auth/chooseAccount/ChooseAccountBody.tsx index 3b3d75e..7ea8286 100644 --- a/packages/app/components/auth/chooseAccount/ChooseAccountBody.js +++ b/packages/app/components/auth/chooseAccount/ChooseAccountBody.tsx @@ -4,6 +4,7 @@ import { FormattedMessage as Message } from 'react-intl'; import BaseAuthBody from 'app/components/auth/BaseAuthBody'; import { AccountSwitcher } from 'app/components/accounts'; +import { Account } from 'app/components/accounts/reducer'; import styles from './chooseAccount.scss'; import messages from './ChooseAccount.intl.json'; @@ -46,7 +47,7 @@ export default class ChooseAccountBody extends BaseAuthBody { ); } - onSwitch = account => { + onSwitch = (account: Account): void => { this.context.resolve(account); }; } diff --git a/packages/app/components/auth/factory.tsx b/packages/app/components/auth/factory.tsx index 7d10072..0cf6e13 100644 --- a/packages/app/components/auth/factory.tsx +++ b/packages/app/components/auth/factory.tsx @@ -1,36 +1,36 @@ -import React from 'react'; +import React, { ComponentProps, ComponentType } from 'react'; import { Button } from 'app/components/ui/form'; -import RejectionLink, { - RejectionLinkProps, -} from 'app/components/auth/RejectionLink'; +import RejectionLink from 'app/components/auth/RejectionLink'; import AuthTitle from 'app/components/auth/AuthTitle'; import { MessageDescriptor } from 'react-intl'; import { Color } from 'app/components/ui'; +import BaseAuthBody from './BaseAuthBody'; -/** - * @param {object} options - * @param {string|object} options.title - panel title - * @param {React.ReactElement} options.body - * @param {object} options.footer - config for footer Button - * @param {Array|object|null} options.links - link config or an array of link configs - * - * @returns {object} - structure, required for auth panel to work - */ -export default function({ - title, - body, - footer, - links, -}: { +export type Factory = () => { + Title: ComponentType; + Body: typeof BaseAuthBody; + Footer: ComponentType; + Links: ComponentType; +}; + +type RejectionLinkProps = ComponentProps; +interface FactoryParams { title: MessageDescriptor; - body: React.ElementType; + body: typeof BaseAuthBody; footer: { color?: Color; label: string | MessageDescriptor; autoFocus?: boolean; }; - links?: RejectionLinkProps | RejectionLinkProps[]; -}) { + links?: RejectionLinkProps | Array; +} + +export default function({ + title, + body, + footer, + links, +}: FactoryParams): Factory { return () => ({ Title: () => , Body: body, @@ -38,7 +38,7 @@ export default function({ Links: () => links ? ( - {([] as RejectionLinkProps[]) + {([] as Array) .concat(links) .map((link, index) => [ index ? ' | ' : '', diff --git a/packages/app/components/auth/finish/Finish.tsx b/packages/app/components/auth/finish/Finish.tsx index b52fba7..c5ddeff 100644 --- a/packages/app/components/auth/finish/Finish.tsx +++ b/packages/app/components/auth/finish/Finish.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { MouseEventHandler } from 'react'; import { connect } from 'react-redux'; import { FormattedMessage as Message } from 'react-intl'; import { Helmet } from 'react-helmet-async'; @@ -13,7 +13,7 @@ interface Props { appName: string; code?: string; state: string; - displayCode?: string; + displayCode?: boolean; success?: boolean; } @@ -84,7 +84,7 @@ class Finish extends React.Component { ); } - onCopyClick = event => { + onCopyClick: MouseEventHandler = event => { event.preventDefault(); const { code } = this.props; diff --git a/packages/app/components/auth/forgotPassword/ForgotPasswordBody.js b/packages/app/components/auth/forgotPassword/ForgotPasswordBody.tsx similarity index 100% rename from packages/app/components/auth/forgotPassword/ForgotPasswordBody.js rename to packages/app/components/auth/forgotPassword/ForgotPasswordBody.tsx diff --git a/packages/app/components/auth/login/LoginBody.js b/packages/app/components/auth/login/LoginBody.tsx similarity index 85% rename from packages/app/components/auth/login/LoginBody.js rename to packages/app/components/auth/login/LoginBody.tsx index f903977..a139801 100644 --- a/packages/app/components/auth/login/LoginBody.js +++ b/packages/app/components/auth/login/LoginBody.tsx @@ -1,13 +1,15 @@ import React from 'react'; + import { Input } from 'app/components/ui/form'; import BaseAuthBody from 'app/components/auth/BaseAuthBody'; +import { User } from 'app/components/user/reducer'; import messages from './Login.intl.json'; export default class LoginBody extends BaseAuthBody { static displayName = 'LoginBody'; static panelId = 'login'; - static hasGoBack = state => { + static hasGoBack = (state: { user: User }) => { return !state.user.isGuest; }; diff --git a/packages/app/components/auth/password/PasswordBody.js b/packages/app/components/auth/password/PasswordBody.tsx similarity index 100% rename from packages/app/components/auth/password/PasswordBody.js rename to packages/app/components/auth/password/PasswordBody.tsx diff --git a/packages/app/components/auth/permissions/PermissionsBody.js b/packages/app/components/auth/permissions/PermissionsBody.tsx similarity index 100% rename from packages/app/components/auth/permissions/PermissionsBody.js rename to packages/app/components/auth/permissions/PermissionsBody.tsx diff --git a/packages/app/components/auth/recoverPassword/RecoverPasswordBody.js b/packages/app/components/auth/recoverPassword/RecoverPasswordBody.tsx similarity index 92% rename from packages/app/components/auth/recoverPassword/RecoverPasswordBody.js rename to packages/app/components/auth/recoverPassword/RecoverPasswordBody.tsx index 98a970f..2a252a8 100644 --- a/packages/app/components/auth/recoverPassword/RecoverPasswordBody.js +++ b/packages/app/components/auth/recoverPassword/RecoverPasswordBody.tsx @@ -1,4 +1,3 @@ -import PropTypes from 'prop-types'; import React from 'react'; import { FormattedMessage as Message } from 'react-intl'; @@ -16,14 +15,6 @@ export default class RecoverPasswordBody extends BaseAuthBody { static panelId = 'recoverPassword'; static hasGoBack = true; - static propTypes = { - match: PropTypes.shape({ - params: PropTypes.shape({ - key: PropTypes.string, - }), - }), - }; - autoFocusField = this.props.match.params && this.props.match.params.key ? 'newPassword' diff --git a/packages/app/components/auth/reducer.test.ts b/packages/app/components/auth/reducer.test.ts index 666d668..1b5ba5d 100644 --- a/packages/app/components/auth/reducer.test.ts +++ b/packages/app/components/auth/reducer.test.ts @@ -1,14 +1,9 @@ import expect from 'app/test/unexpected'; import auth from './reducer'; -import { - setLogin, - SET_CREDENTIALS, - setAccountSwitcher, - SET_SWITCHER, -} from './actions'; +import { setLogin, setAccountSwitcher } from './actions'; describe('components/auth/reducer', () => { - describe(SET_CREDENTIALS, () => { + describe('auth:setCredentials', () => { it('should set login', () => { const expectedLogin = 'foo'; @@ -22,7 +17,7 @@ describe('components/auth/reducer', () => { }); }); - describe(SET_SWITCHER, () => { + describe('auth:setAccountSwitcher', () => { it('should be enabled by default', () => expect(auth(undefined, {} as any), 'to satisfy', { isSwitcherEnabled: true, diff --git a/packages/app/components/auth/reducer.ts b/packages/app/components/auth/reducer.ts index 4b0ae6e..8be2f20 100644 --- a/packages/app/components/auth/reducer.ts +++ b/packages/app/components/auth/reducer.ts @@ -1,26 +1,34 @@ -import { combineReducers } from 'redux'; +import { combineReducers, Reducer } from 'redux'; import { RootState } from 'app/reducers'; +import { Scope } from '../../services/api/oauth'; import { - ERROR, - SET_CLIENT, - SET_OAUTH, - SET_OAUTH_RESULT, - SET_SCOPES, - SET_LOADING_STATE, - REQUIRE_PERMISSIONS_ACCEPT, - SET_CREDENTIALS, - SET_SWITCHER, + ErrorAction, + CredentialsAction, + AccountSwitcherAction, + LoadingAction, + ClientAction, + OAuthAction, + ScopesAction, } from './actions'; -type Credentials = { - login?: string; +export interface Credentials { + login?: string | null; // By some reasons there is can be null value. Need to investigate. password?: string; rememberMe?: boolean; returnUrl?: string; isRelogin?: boolean; isTotpRequired?: boolean; -}; +} + +type Error = Record< + string, + | string + | { + type: string; + payload: Record; + } +> | null; export interface Client { id: string; @@ -28,7 +36,7 @@ export interface Client { description: string; } -interface OAuthState { +export interface OAuthState { clientId: string; redirectUrl: string; responseType: string; @@ -39,27 +47,113 @@ interface OAuthState { state: string; success?: boolean; code?: string; - displayCode?: string; + displayCode?: boolean; acceptRequired?: boolean; } +type Scopes = Array; + export interface State { credentials: Credentials; - error: null | { - [key: string]: - | string - | { - type: string; - payload: { [key: string]: any }; - }; - }; + error: Error; isLoading: boolean; isSwitcherEnabled: boolean; client: Client | null; oauth: OAuthState | null; - scopes: string[]; + scopes: Scopes; } +const error: Reducer = ( + state = null, + { type, payload }, +) => { + if (type === 'auth:error') { + return payload; + } + + return state; +}; + +const credentials: Reducer = ( + state = {}, + { type, payload }, +) => { + if (type === 'auth:setCredentials') { + if (payload) { + return { + ...payload, + }; + } + + return {}; + } + + return state; +}; + +const isSwitcherEnabled: Reducer< + State['isSwitcherEnabled'], + AccountSwitcherAction +> = (state = true, { type, payload }) => { + if (type === 'auth:setAccountSwitcher') { + return payload; + } + + return state; +}; + +const isLoading: Reducer = ( + state = false, + { type, payload }, +) => { + if (type === 'set_loading_state') { + return payload; + } + + return state; +}; + +const client: Reducer = ( + state = null, + { type, payload }, +) => { + if (type === 'set_client') { + return payload; + } + + return state; +}; + +const oauth: Reducer = (state = null, action) => { + switch (action.type) { + case 'set_oauth': + return action.payload; + case 'set_oauth_result': + return { + ...(state as OAuthState), + ...action.payload, + }; + case 'require_permissions_accept': + return { + ...(state as OAuthState), + acceptRequired: true, + }; + default: + return state; + } +}; + +const scopes: Reducer = ( + state = [], + { type, payload }, +) => { + if (type === 'set_scopes') { + return payload; + } + + return state; +}; + export default combineReducers({ credentials, error, @@ -70,135 +164,6 @@ export default combineReducers({ scopes, }); -function error( - state = null, - { type, payload = null, error = false }, -): State['error'] { - switch (type) { - case ERROR: - if (!error) { - throw new Error('Expected payload with error'); - } - - return payload; - - default: - return state; - } -} - -function credentials( - state = {}, - { - type, - payload, - }: { - type: string; - payload: Credentials | null; - }, -): State['credentials'] { - if (type === SET_CREDENTIALS) { - if (payload && typeof payload === 'object') { - return { - ...payload, - }; - } - - return {}; - } - - return state; -} - -function isSwitcherEnabled( - state = true, - { type, payload = false }, -): State['isSwitcherEnabled'] { - switch (type) { - case SET_SWITCHER: - if (typeof payload !== 'boolean') { - throw new Error('Expected payload of boolean type'); - } - - return payload; - - default: - return state; - } -} - -function isLoading( - state = false, - { type, payload = null }, -): State['isLoading'] { - switch (type) { - case SET_LOADING_STATE: - return !!payload; - - default: - return state; - } -} - -function client(state = null, { type, payload }): State['client'] { - switch (type) { - case SET_CLIENT: - return { - id: payload.id, - name: payload.name, - description: payload.description, - }; - - default: - return state; - } -} - -function oauth( - state: State['oauth'] = null, - { type, payload }, -): State['oauth'] { - switch (type) { - case SET_OAUTH: - return { - clientId: payload.clientId, - redirectUrl: payload.redirectUrl, - responseType: payload.responseType, - scope: payload.scope, - prompt: payload.prompt, - loginHint: payload.loginHint, - state: payload.state, - }; - - case SET_OAUTH_RESULT: - return { - ...(state as OAuthState), - success: payload.success, - code: payload.code, - displayCode: payload.displayCode, - }; - - case REQUIRE_PERMISSIONS_ACCEPT: - return { - ...(state as OAuthState), - acceptRequired: true, - }; - - default: - return state; - } -} - -function scopes(state = [], { type, payload = [] }): State['scopes'] { - switch (type) { - case SET_SCOPES: - return payload; - - default: - return state; - } -} - export function getLogin( state: RootState | Pick, ): string | null { diff --git a/packages/app/components/auth/register/RegisterBody.js b/packages/app/components/auth/register/RegisterBody.tsx similarity index 100% rename from packages/app/components/auth/register/RegisterBody.js rename to packages/app/components/auth/register/RegisterBody.tsx diff --git a/packages/app/components/auth/resendActivation/ResendActivationBody.js b/packages/app/components/auth/resendActivation/ResendActivationBody.tsx similarity index 100% rename from packages/app/components/auth/resendActivation/ResendActivationBody.js rename to packages/app/components/auth/resendActivation/ResendActivationBody.tsx diff --git a/packages/app/components/contact/ContactForm.test.tsx b/packages/app/components/contact/ContactForm.test.tsx index 4fc7a62..350b525 100644 --- a/packages/app/components/contact/ContactForm.test.tsx +++ b/packages/app/components/contact/ContactForm.test.tsx @@ -1,17 +1,23 @@ -import React from 'react'; +import React, { ComponentProps } from 'react'; import expect from 'app/test/unexpected'; import sinon from 'sinon'; -import { shallow, mount } from 'enzyme'; +import { shallow, mount, ShallowWrapper, ReactWrapper } from 'enzyme'; import { IntlProvider } from 'react-intl'; import feedback from 'app/services/api/feedback'; import { User } from 'app/components/user'; import { ContactForm } from './ContactForm'; +type ContactFormShallowType = ShallowWrapper< + ComponentProps, + any, + ContactForm +>; + describe('ContactForm', () => { describe('when rendered', () => { const user = {} as User; - let component; + let component: ContactFormShallowType; beforeEach(() => { component = shallow(); @@ -57,7 +63,7 @@ describe('ContactForm', () => { const user = { email: 'foo@bar.com', } as User; - let component; + let component: ContactFormShallowType; beforeEach(() => { component = shallow(); @@ -76,7 +82,7 @@ describe('ContactForm', () => { const user = { email: 'foo@bar.com', } as User; - let component; + let component: ContactFormShallowType; beforeEach(() => { component = shallow(); @@ -93,15 +99,15 @@ describe('ContactForm', () => { const user = { email: 'foo@bar.com', } as User; - let component; - let wrapper; + let component: ContactForm; + let wrapper: ReactWrapper; beforeEach(() => { // TODO: add polyfill for from validation for jsdom wrapper = mount( - (component = el)} /> + (component = el!)} /> , ); }); @@ -118,8 +124,8 @@ describe('ContactForm', () => { const user = { email: 'foo@bar.com', } as User; - let component; - let wrapper; + let component: ContactForm; + let wrapper: ReactWrapper; const requestData = { email: user.email, subject: 'Test subject', @@ -137,16 +143,18 @@ describe('ContactForm', () => { // TODO: try to rewrite with unexpected-react wrapper = mount( - (component = el)} /> + (component = el!)} /> , ); - wrapper.find('input[name="email"]').getDOMNode().value = + wrapper.find('input[name="email"]').getDOMNode().value = requestData.email; - wrapper.find('input[name="subject"]').getDOMNode().value = - requestData.subject; - wrapper.find('textarea[name="message"]').getDOMNode().value = - requestData.message; + wrapper + .find('input[name="subject"]') + .getDOMNode().value = requestData.subject; + wrapper + .find('textarea[name="message"]') + .getDOMNode().value = requestData.message; }); afterEach(() => { diff --git a/packages/app/components/contact/ContactForm.tsx b/packages/app/components/contact/ContactForm.tsx index f6d8e15..ca6f76d 100644 --- a/packages/app/components/contact/ContactForm.tsx +++ b/packages/app/components/contact/ContactForm.tsx @@ -172,9 +172,9 @@ export class ContactForm extends React.Component< ); } - onSubmit = () => { + onSubmit = (): Promise => { if (this.state.isLoading) { - return; + return Promise.resolve(); } this.setState({ isLoading: true }); diff --git a/packages/app/components/dev/apps/actions.ts b/packages/app/components/dev/apps/actions.ts index fcf7ea4..bd3e991 100644 --- a/packages/app/components/dev/apps/actions.ts +++ b/packages/app/components/dev/apps/actions.ts @@ -1,17 +1,15 @@ -import { Dispatch } from 'redux'; +import { Dispatch, Action as ReduxAction } from 'redux'; import { OauthAppResponse } from 'app/services/api/oauth'; import oauth from 'app/services/api/oauth'; import { User } from 'app/components/user'; +import { ThunkAction } from 'app/reducers'; import { Apps } from './reducer'; -type SetAvailableAction = { +interface SetAvailableAction extends ReduxAction { type: 'apps:setAvailable'; payload: Array; -}; -type DeleteAppAction = { type: 'apps:deleteApp'; payload: string }; -type AddAppAction = { type: 'apps:addApp'; payload: OauthAppResponse }; -export type Action = SetAvailableAction | DeleteAppAction | AddAppAction; +} export function setAppsList(apps: Array): SetAvailableAction { return { @@ -27,14 +25,19 @@ export function getApp( return state.apps.available.find(app => app.clientId === clientId) || null; } -export function fetchApp(clientId: string) { - return async (dispatch: Dispatch): Promise => { +export function fetchApp(clientId: string): ThunkAction> { + return async dispatch => { const app = await oauth.getApp(clientId); dispatch(addApp(app)); }; } +interface AddAppAction extends ReduxAction { + type: 'apps:addApp'; + payload: OauthAppResponse; +} + function addApp(app: OauthAppResponse): AddAppAction { return { type: 'apps:addApp', @@ -69,6 +72,11 @@ export function deleteApp(clientId: string) { }; } +interface DeleteAppAction extends ReduxAction { + type: 'apps:deleteApp'; + payload: string; +} + function createDeleteAppAction(clientId: string): DeleteAppAction { return { type: 'apps:deleteApp', @@ -76,8 +84,11 @@ function createDeleteAppAction(clientId: string): DeleteAppAction { }; } -export function resetApp(clientId: string, resetSecret: boolean) { - return async (dispatch: Dispatch): Promise => { +export function resetApp( + clientId: string, + resetSecret: boolean, +): ThunkAction> { + return async dispatch => { const { data: app } = await oauth.reset(clientId, resetSecret); if (resetSecret) { @@ -85,3 +96,5 @@ export function resetApp(clientId: string, resetSecret: boolean) { } }; } + +export type Action = SetAvailableAction | DeleteAppAction | AddAppAction; diff --git a/packages/app/components/dev/apps/applicationForm/ApplicationForm.tsx b/packages/app/components/dev/apps/applicationForm/ApplicationForm.tsx index 863e13d..4b0938c 100644 --- a/packages/app/components/dev/apps/applicationForm/ApplicationForm.tsx +++ b/packages/app/components/dev/apps/applicationForm/ApplicationForm.tsx @@ -19,12 +19,15 @@ import ApplicationTypeSwitcher from './ApplicationTypeSwitcher'; import WebsiteType from './WebsiteType'; import MinecraftServerType from './MinecraftServerType'; -const typeToForm: { - [K in ApplicationType]: { +type TypeToForm = Record< + ApplicationType, + { label: MessageDescriptor; component: React.ComponentType; - }; -} = { + } +>; + +const typeToForm: TypeToForm = { [TYPE_APPLICATION]: { label: messages.website, component: WebsiteType, @@ -35,16 +38,15 @@ const typeToForm: { }, }; -const typeToLabel = Object.keys(typeToForm).reduce( - (result, key: ApplicationType) => { - result[key] = typeToForm[key].label; +type TypeToLabel = Record; - return result; - }, - {} as { - [K in ApplicationType]: MessageDescriptor; - }, -); +const typeToLabel: TypeToLabel = ((Object.keys(typeToForm) as unknown) as Array< + ApplicationType +>).reduce((result, key) => { + result[key] = typeToForm[key].label; + + return result; +}, {} as TypeToLabel); export default class ApplicationForm extends React.Component<{ app: OauthAppResponse; diff --git a/packages/app/components/dev/apps/applicationForm/ApplicationTypeSwitcher.tsx b/packages/app/components/dev/apps/applicationForm/ApplicationTypeSwitcher.tsx index 9fcbb58..5cc4776 100644 --- a/packages/app/components/dev/apps/applicationForm/ApplicationTypeSwitcher.tsx +++ b/packages/app/components/dev/apps/applicationForm/ApplicationTypeSwitcher.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { ComponentType } from 'react'; import { ApplicationType } from 'app/components/dev/apps'; import { MessageDescriptor } from 'react-intl'; import { SKIN_LIGHT } from 'app/components/ui'; @@ -6,20 +6,20 @@ import { Radio } from 'app/components/ui/form'; import styles from './applicationTypeSwitcher.scss'; -export default function ApplicationTypeSwitcher({ - setType, - appTypes, - selectedType, -}: { - appTypes: { - [K in ApplicationType]: MessageDescriptor; - }; +interface Props { + appTypes: Record; selectedType: ApplicationType | null; setType: (type: ApplicationType) => void; -}) { - return ( -
- {Object.keys(appTypes).map((type: ApplicationType) => ( +} + +const ApplicationTypeSwitcher: ComponentType = ({ + appTypes, + selectedType, + setType, +}) => ( +
+ {((Object.keys(appTypes) as unknown) as Array).map( + type => (
setType(type)} @@ -29,7 +29,9 @@ export default function ApplicationTypeSwitcher({ checked={selectedType === type} />
- ))} -
- ); -} + ), + )} +
+); + +export default ApplicationTypeSwitcher; diff --git a/packages/app/components/dev/apps/applicationForm/MinecraftServerType.tsx b/packages/app/components/dev/apps/applicationForm/MinecraftServerType.tsx index 53a1c31..5993b88 100644 --- a/packages/app/components/dev/apps/applicationForm/MinecraftServerType.tsx +++ b/packages/app/components/dev/apps/applicationForm/MinecraftServerType.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { ComponentType } from 'react'; import { FormattedMessage as Message } from 'react-intl'; import { OauthAppResponse } from 'app/services/api/oauth'; import { Input, FormModel } from 'app/components/ui/form'; @@ -7,52 +7,51 @@ import styles from 'app/components/profile/profileForm.scss'; import messages from './ApplicationForm.intl.json'; -export default function MinecraftServerType({ - form, - app, -}: { +interface Props { form: FormModel; app: OauthAppResponse; -}) { - return ( -
-
- -
- -
-

- -

-
-
- -
- -
-

- -

-
-
- -
-
- ); } + +const MinecraftServerType: ComponentType = ({ form, app }) => ( +
+
+ +
+ +
+

+ +

+
+
+ +
+ +
+

+ +

+
+
+ +
+
+); + +export default MinecraftServerType; diff --git a/packages/app/components/dev/apps/applicationForm/WebsiteType.tsx b/packages/app/components/dev/apps/applicationForm/WebsiteType.tsx index 1aa54c4..b7ad785 100644 --- a/packages/app/components/dev/apps/applicationForm/WebsiteType.tsx +++ b/packages/app/components/dev/apps/applicationForm/WebsiteType.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { ComponentType } from 'react'; import { FormattedMessage as Message } from 'react-intl'; import { Input, TextArea, FormModel } from 'app/components/ui/form'; import { OauthAppResponse } from 'app/services/api/oauth'; @@ -7,68 +7,67 @@ import styles from 'app/components/profile/profileForm.scss'; import messages from './ApplicationForm.intl.json'; -export default function WebsiteType({ - form, - app, -}: { +interface Props { form: FormModel; app: OauthAppResponse; -}) { - return ( -
-
- -
- -
-

- -

-
-
-