This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Replace redux-form and mateus with final-form
- Loading branch information
Jiří Zdvomka
committed
Oct 15, 2021
1 parent
ba18684
commit 2cbdd65
Showing
19 changed files
with
125 additions
and
148 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
2 changes: 1 addition & 1 deletion
2
packages/cra-template-typescript/template/src/modules/auth/components/Login.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import firewall from '../HOC/firewall'; | ||
import LoginForm from '../containers/LoginForm'; | ||
import LoginForm from './LoginForm'; | ||
import AuthContent from './AuthContent'; | ||
|
||
export default firewall(AuthContent, LoginForm); |
96 changes: 57 additions & 39 deletions
96
packages/cra-template-typescript/template/src/modules/auth/components/LoginForm.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 |
---|---|---|
@@ -1,43 +1,61 @@ | ||
import React from "react"; | ||
import { Form } from "redux-form"; | ||
import { Button } from "antd"; | ||
import { TextField } from "@ackee/mateus"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
interface LoginFormProps { | ||
handleSubmit: () => void; | ||
submitting: boolean; | ||
error?: string; | ||
} | ||
|
||
function LoginForm({ handleSubmit, submitting, error = '' }: LoginFormProps) { | ||
import React from 'react'; | ||
import { FormattedMessage, useIntl } from 'react-intl'; | ||
import { Form, Field } from 'react-final-form'; | ||
|
||
import { useSubmitForm, validatorsWithMessage, composeValidators, translate } from 'modules/form'; | ||
|
||
import { types } from '../services/actions'; | ||
import { LoginFormField } from '../types'; | ||
|
||
const { required, email } = validatorsWithMessage; | ||
|
||
const validators = { | ||
[LoginFormField.EMAIL]: composeValidators(required, email), | ||
[LoginFormField.PASSWORD]: required, | ||
}; | ||
|
||
const LoginForm = () => { | ||
const { formatMessage } = useIntl(); | ||
const onSubmit = useSubmitForm(types.login); | ||
|
||
return ( | ||
<Form onSubmit={handleSubmit}> | ||
<TextField | ||
id="email" | ||
name="email" | ||
disabled={submitting} | ||
label={<FormattedMessage id="login.email" />} | ||
autoComplete="email" | ||
autoFocus | ||
/> | ||
|
||
<TextField | ||
id="password" | ||
name="password" | ||
type="password" | ||
disabled={submitting} | ||
label={<FormattedMessage id="login.password" />} | ||
autoComplete="current-password" | ||
/> | ||
|
||
<Button id="submitButton" htmlType="submit" disabled={submitting}> | ||
<FormattedMessage id="login.submit" /> | ||
</Button> | ||
|
||
{error && <p>{error}</p>} | ||
</Form> | ||
<Form | ||
onSubmit={onSubmit} | ||
render={({ handleSubmit, submitError }) => ( | ||
<form onSubmit={handleSubmit}> | ||
<div> | ||
<label> | ||
<FormattedMessage id="login.email" /> | ||
</label> | ||
<Field | ||
name={LoginFormField.EMAIL} | ||
type="email" | ||
component="input" | ||
validate={translate(formatMessage, validators[LoginFormField.EMAIL])} | ||
/> | ||
</div> | ||
|
||
<div> | ||
<label> | ||
<FormattedMessage id="login.password" /> | ||
</label> | ||
<Field | ||
name={LoginFormField.PASSWORD} | ||
type="password" | ||
component="input" | ||
validate={translate(formatMessage, validators[LoginFormField.PASSWORD])} | ||
/> | ||
</div> | ||
|
||
<button type="submit"> | ||
<FormattedMessage id="login.submit" /> | ||
</button> | ||
|
||
{submitError && <p>{submitError}</p>} | ||
</form> | ||
)} | ||
/> | ||
); | ||
} | ||
}; | ||
|
||
export default LoginForm; |
25 changes: 0 additions & 25 deletions
25
packages/cra-template-typescript/template/src/modules/auth/containers/LoginForm/LoginForm.ts
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
...-template-typescript/template/src/modules/auth/containers/LoginForm/LoginForm.validate.ts
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
packages/cra-template-typescript/template/src/modules/auth/containers/LoginForm/index.ts
This file was deleted.
Oops, something went wrong.
7 changes: 5 additions & 2 deletions
7
packages/cra-template-typescript/template/src/modules/auth/services/actions/index.ts
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export { default as types } from './types'; | ||
export * from './login'; | ||
import * as types from './types'; | ||
|
||
export { default as login } from './login'; | ||
|
||
export { types }; |
8 changes: 4 additions & 4 deletions
8
packages/cra-template-typescript/template/src/modules/auth/services/actions/login.ts
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import types from './types'; | ||
import { createFormActions } from 'react-final-form-redux-submit'; | ||
|
||
export const loginForm = () => ({ | ||
type: types.LOGIN_FORM, | ||
}); | ||
import { login as loginTypes } from './types'; | ||
|
||
export default createFormActions(loginTypes); |
12 changes: 2 additions & 10 deletions
12
packages/cra-template-typescript/template/src/modules/auth/services/actions/types.ts
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 |
---|---|---|
@@ -1,11 +1,3 @@ | ||
import { createAsyncType } from "@ackee/redux-utils"; | ||
import { createFormActionTypes } from 'react-final-form-redux-submit'; | ||
|
||
const asyncType = createAsyncType({ | ||
modulePrefix: 'auth', | ||
}); | ||
|
||
export default { | ||
...asyncType({ | ||
types: ['LOGIN_FORM'], | ||
}), | ||
}; | ||
export const login = createFormActionTypes('LOGIN_FORM'); |
33 changes: 13 additions & 20 deletions
33
packages/cra-template-typescript/template/src/modules/auth/services/sagas/loginForm.ts
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 |
---|---|---|
@@ -1,28 +1,21 @@ | ||
import * as Petrus from "@ackee/petrus"; | ||
import * as sagaEffects from "redux-saga/effects"; | ||
import { LOGIN_SUCCESS, LOGIN_FAILURE, loginRequest } from '@ackee/petrus'; | ||
import { put, take, takeLeading } from 'redux-saga/effects'; | ||
|
||
import { types } from '../actions'; | ||
import { types, login } from '../actions'; | ||
|
||
const { put, take, takeEvery } = sagaEffects; | ||
function* handleLoginForm(action: ReturnType<typeof login.submit>) { | ||
yield put(loginRequest(action.payload)); | ||
|
||
function* handleLoginForm(action) { | ||
yield action.startSubmit(); | ||
const result = yield take([LOGIN_SUCCESS, LOGIN_FAILURE]); | ||
|
||
yield put(Petrus.loginRequest(action.data)); | ||
|
||
const result = yield take([Petrus.LOGIN_SUCCESS, Petrus.LOGIN_FAILURE]); | ||
|
||
let payload; | ||
|
||
if (result.type === Petrus.LOGIN_FAILURE) { | ||
payload = { | ||
_error: 'Login failed', | ||
}; | ||
if (result.type === LOGIN_SUCCESS) { | ||
yield put(login.submitSuccess()); | ||
} else { | ||
console.error(result.error); | ||
yield put(login.submitFailure('login.failed')); | ||
} | ||
|
||
yield action.stopSubmit(payload); | ||
} | ||
|
||
export default function*() { | ||
yield takeEvery(types.LOGIN_FORM, handleLoginForm); | ||
export default function* () { | ||
yield takeLeading(types.login.SUBMIT, handleLoginForm); | ||
} |
9 changes: 7 additions & 2 deletions
9
packages/cra-template-typescript/template/src/modules/auth/types.ts
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
export type User = {email: string}; | ||
export type User = { email: string }; | ||
|
||
export interface PetrusEntitiesState { | ||
user: null | User; | ||
} | ||
} | ||
|
||
export enum LoginFormField { | ||
EMAIL = 'email', | ||
PASSWORD = 'password', | ||
} |
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
5 changes: 5 additions & 0 deletions
5
...cra-template-typescript/template/src/modules/core/modules/redux/config/promiseListener.ts
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,5 @@ | ||
// @use-form-module-begin | ||
import createReduxPromiseListener from 'redux-promise-listener'; | ||
|
||
export default createReduxPromiseListener(); | ||
// @use-form-module-end |
2 changes: 1 addition & 1 deletion
2
packages/cra-template-typescript/template/src/modules/form/hooks/useSubmitForm.ts
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
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