From 7189b9090c1143117b230e8fda60c30b050e4032 Mon Sep 17 00:00:00 2001 From: Jonathan Ruddell Date: Wed, 18 Sep 2019 20:02:51 -0700 Subject: [PATCH] update all componentWillReceiveProps to componentDidUpdate in main app also removes most WRONG error messages and uses the server-provided message --- .../password-reset/forgot-password-screen.js | 18 +++------- .../password-reset/forgot-password.sagas.js | 2 +- .../password/change-password-screen.js | 16 +++------ .../account/password/change-password.sagas.js | 2 +- .../account/register/register-screen.js.ejs | 16 +++------ .../account/register/register.sagas.js | 2 +- .../account/settings/settings-screen.js | 20 +++-------- .../app/modules/login/login-screen.js.ejs | 14 ++++---- .../app/modules/login/login.sagas.js.ejs | 7 ++-- .../app/shared/services/fixture-api.js.ejs | 4 ++- boilerplate/package.json.ejs | 2 +- .../patches/tcomb-form-native+0.6.20.patch | 34 +++++++++++++++++++ .../forgot-passsword.sagas.spec.js | 2 +- .../password/change-passsword.sagas.spec.js | 2 +- .../account/register/register.sagas.spec.js | 2 +- .../modules/login/login.sagas.spec.js.ejs | 4 +-- .../spec/shared/sagas/account.sagas.spec.js | 4 +-- src/entity/files.js | 2 +- 18 files changed, 76 insertions(+), 77 deletions(-) diff --git a/boilerplate/app/modules/account/password-reset/forgot-password-screen.js b/boilerplate/app/modules/account/password-reset/forgot-password-screen.js index f90648846..906e3a8be 100644 --- a/boilerplate/app/modules/account/password-reset/forgot-password-screen.js +++ b/boilerplate/app/modules/account/password-reset/forgot-password-screen.js @@ -25,16 +25,12 @@ class ForgotPasswordScreen extends React.Component { onSubmitEditing: () => this.submitForm() } }, - success: false } this.submitForm = this.submitForm.bind(this) this.formChange = this.formChange.bind(this) } submitForm () { - this.setState({ - success: false - }) // call getValue() to get the values of the form const value = this.form.getValue() if (value) { // if validation fails, value will be null @@ -42,17 +38,11 @@ class ForgotPasswordScreen extends React.Component { } } - componentWillReceiveProps (newProps) { - // Did the update attempt complete? - if (!newProps.fetching) { - if (newProps.error) { - if (newProps.error === 'WRONG') { - Alert.alert('Error', 'Something when wrong resetting your password', [{ text: 'OK' }]) - } + componentDidUpdate(prevProps) { + if (prevProps.fetching && !this.props.fetching) { + if (this.props.error) { + Alert.alert('Error', this.props.error, [{ text: 'OK' }]) } else { - this.setState({ - success: true - }) Alert.alert('Success', 'Password reset email sent', [{ text: 'OK' }]) Navigation.popToRoot(this.props.componentId) } diff --git a/boilerplate/app/modules/account/password-reset/forgot-password.sagas.js b/boilerplate/app/modules/account/password-reset/forgot-password.sagas.js index a78cb4af3..2457eb9a7 100644 --- a/boilerplate/app/modules/account/password-reset/forgot-password.sagas.js +++ b/boilerplate/app/modules/account/password-reset/forgot-password.sagas.js @@ -11,6 +11,6 @@ export function * forgotPassword (api, { email }) { yield put(ForgotPasswordActions.forgotPasswordSuccess(response.data)) } else { console.tron.log('ForgotPassword - FAIL') - yield put(ForgotPasswordActions.forgotPasswordFailure('WRONG')) + yield put(ForgotPasswordActions.forgotPasswordFailure((response.data && response.data.title) || 'Something when wrong resetting your password')) } } diff --git a/boilerplate/app/modules/account/password/change-password-screen.js b/boilerplate/app/modules/account/password/change-password-screen.js index 1dd1dca13..fd3242147 100644 --- a/boilerplate/app/modules/account/password/change-password-screen.js +++ b/boilerplate/app/modules/account/password/change-password-screen.js @@ -41,16 +41,12 @@ class ChangePasswordScreen extends React.Component { } } }, - success: false } this.submitForm = this.submitForm.bind(this) this.formChange = this.formChange.bind(this) } submitForm () { - this.setState({ - success: false - }) // call getValue() to get the values of the form const value = this.form.getValue() if (value) { // if validation fails, value will be null @@ -62,15 +58,11 @@ class ChangePasswordScreen extends React.Component { } } - componentWillReceiveProps (newProps) { - // Did the changePassword attempt complete? - if (!newProps.fetching) { - if (newProps.error) { - Alert.alert('Error', newProps.error, [{ text: 'OK' }]) + componentDidUpdate(prevProps) { + if (prevProps.fetching && !this.props.fetching) { + if (this.props.error) { + Alert.alert('Error', this.props.error, [{ text: 'OK' }]) } else { - this.setState({ - success: true - }) Alert.alert('Success', 'Password changed', [{ text: 'OK' }]) } } diff --git a/boilerplate/app/modules/account/password/change-password.sagas.js b/boilerplate/app/modules/account/password/change-password.sagas.js index 145cd94c4..aad94e78a 100644 --- a/boilerplate/app/modules/account/password/change-password.sagas.js +++ b/boilerplate/app/modules/account/password/change-password.sagas.js @@ -13,6 +13,6 @@ export function * changePassword (api, { currentPassword, newPassword }) { yield put(ChangePasswordActions.changePasswordSuccess()) } else { console.tron.log('ChangePassword - FAIL') - yield put(ChangePasswordActions.changePasswordFailure('WRONG')) + yield put(ChangePasswordActions.changePasswordFailure((response.data && response.data.title) || 'Failed to change password')) } } diff --git a/boilerplate/app/modules/account/register/register-screen.js.ejs b/boilerplate/app/modules/account/register/register-screen.js.ejs index c9855e1d8..f3223b65a 100644 --- a/boilerplate/app/modules/account/register/register-screen.js.ejs +++ b/boilerplate/app/modules/account/register/register-screen.js.ejs @@ -50,16 +50,12 @@ class RegisterScreen extends React.Component { } } }, - success: false } this.submitUpdate = this.submitUpdate.bind(this) this.accountChange = this.accountChange.bind(this) } submitUpdate () { - this.setState({ - success: false - }) // call getValue() to get the values of the form const value = this.form.getValue() if (value) { // if validation fails, value will be null @@ -71,15 +67,11 @@ class RegisterScreen extends React.Component { } } - componentWillReceiveProps (newProps) { - // Did the register attempt complete? - if (!newProps.fetching) { - if (newProps.error) { - Alert.alert('Error', (newProps.error && newProps.error.title) ? newProps.error.title : '', [{ text: 'OK' }]) + componentDidUpdate(prevProps) { + if (prevProps.fetching && !this.props.fetching) { + if (this.props.error) { + Alert.alert('Error', this.props.error, [{ text: 'OK' }]) } else { - this.setState({ - success: true - }) Navigation.popToRoot(this.props.componentId) Alert.alert('Registration Successful', 'Please check your email', [{ text: 'OK' }]) } diff --git a/boilerplate/app/modules/account/register/register.sagas.js b/boilerplate/app/modules/account/register/register.sagas.js index 035ffa6f5..d272fd663 100644 --- a/boilerplate/app/modules/account/register/register.sagas.js +++ b/boilerplate/app/modules/account/register/register.sagas.js @@ -11,6 +11,6 @@ export function * register (api, { user }) { yield put(RegisterActions.registerSuccess()) } else { console.tron.log('Register - FAIL') - yield put(RegisterActions.registerFailure(response.data)) + yield put(RegisterActions.registerFailure((response.data && response.data.title) || 'Registration failed')) } } diff --git a/boilerplate/app/modules/account/settings/settings-screen.js b/boilerplate/app/modules/account/settings/settings-screen.js index df7a72508..4a70905d7 100644 --- a/boilerplate/app/modules/account/settings/settings-screen.js +++ b/boilerplate/app/modules/account/settings/settings-screen.js @@ -51,16 +51,12 @@ class SettingsScreen extends React.Component { } } }, - success: false } this.submitUpdate = this.submitUpdate.bind(this) this.accountChange = this.accountChange.bind(this) } submitUpdate () { - this.setState({ - success: false - }) // call getValue() to get the values of the form const value = this.form.getValue() if (value) { // if validation fails, value will be null @@ -68,17 +64,11 @@ class SettingsScreen extends React.Component { } } - componentWillReceiveProps (newProps) { - // Did the update attempt complete? - if (!newProps.updating) { - if (newProps.error) { - if (newProps.error === 'WRONG') { - Alert.alert('Error', 'Something went wrong while saving the settings', [{ text: 'OK' }]) - } - } else if (!this.state.success) { - this.setState({ - success: true - }) + componentDidUpdate(prevProps) { + if (prevProps.updating && !this.props.updating) { + if (this.props.error) { + Alert.alert('Error', this.props.error, [{ text: 'OK' }]) + } else { Alert.alert('Success', 'Settings updated', [{ text: 'OK' }]) this.props.getAccount() } diff --git a/boilerplate/app/modules/login/login-screen.js.ejs b/boilerplate/app/modules/login/login-screen.js.ejs index 6c1d2889e..6c829f5f7 100644 --- a/boilerplate/app/modules/login/login-screen.js.ejs +++ b/boilerplate/app/modules/login/login-screen.js.ejs @@ -26,14 +26,12 @@ class LoginScreen extends React.Component { } } - componentWillReceiveProps (newProps) { - // Did the login attempt complete? - if (!newProps.fetching) { - if (newProps.error) { - if (newProps.error === 'WRONG') { - Alert.alert('Error', 'Invalid login', [{ text: 'OK' }]) - } - } else if (newProps.account) { + componentDidUpdate(prevProps) { + if (!this.props.fetching) { + if (prevProps.fetching && this.props.error) { + Alert.alert('Error', this.props.error, [{ text: 'OK' }]) + } + if (!prevProps.account && this.props.account) { Navigation.dismissModal(this.props.componentId) } } diff --git a/boilerplate/app/modules/login/login.sagas.js.ejs b/boilerplate/app/modules/login/login.sagas.js.ejs index fe5c99c52..70832a90d 100644 --- a/boilerplate/app/modules/login/login.sagas.js.ejs +++ b/boilerplate/app/modules/login/login.sagas.js.ejs @@ -46,10 +46,11 @@ export function * login (api) { yield put({ type: 'RELOGIN_OK' }) } catch (error) { console.tron.log(error) - yield put(LoginActions.loginFailure('WRONG')) + <%_ // todo get expected error format _%> + yield put(LoginActions.loginFailure('Login failed')) } } else { - yield put(LoginActions.loginFailure('WRONG')) + yield put(LoginActions.loginFailure((response.data && response.data.detail) || 'Could not connect to OAuth2 Provider')) } } @@ -87,7 +88,7 @@ export function * login (api, { username, password }) { <%_ } _%> yield put({ type: 'RELOGIN_OK' }) } else { - yield put(LoginActions.loginFailure('WRONG')) + yield put(LoginActions.loginFailure((response.data && response.data.detail) || 'Bad Credentials')) } } <%_ } _%> diff --git a/boilerplate/app/shared/services/fixture-api.js.ejs b/boilerplate/app/shared/services/fixture-api.js.ejs index db2ab62fd..8f537764e 100644 --- a/boilerplate/app/shared/services/fixture-api.js.ejs +++ b/boilerplate/app/shared/services/fixture-api.js.ejs @@ -83,7 +83,9 @@ export default { } else { return { ok: false, - data: 'Invalid email' + data: { + title: 'Invalid email', + } } } }, diff --git a/boilerplate/package.json.ejs b/boilerplate/package.json.ejs index 679b07d52..bc8fe665d 100644 --- a/boilerplate/package.json.ejs +++ b/boilerplate/package.json.ejs @@ -29,7 +29,7 @@ "querystringify": "2.1.1", "ramda": "0.26.1", "react-native-vector-icons": "6.6.0", - "react-native-keyboard-aware-scroll-view": "0.8.0", + "react-native-keyboard-aware-scroll-view": "0.9.1", "react-redux": "7.1.0", "redux": "4.0.4", "redux-persist": "5.10.0", diff --git a/boilerplate/patches/tcomb-form-native+0.6.20.patch b/boilerplate/patches/tcomb-form-native+0.6.20.patch index 369310af2..257138767 100644 --- a/boilerplate/patches/tcomb-form-native+0.6.20.patch +++ b/boilerplate/patches/tcomb-form-native+0.6.20.patch @@ -1,3 +1,37 @@ +diff --git a/node_modules/tcomb-form-native/lib/components.js b/node_modules/tcomb-form-native/lib/components.js +index 1cb5b5e..a508af7 100644 +--- a/node_modules/tcomb-form-native/lib/components.js ++++ b/node_modules/tcomb-form-native/lib/components.js +@@ -81,11 +81,11 @@ class Component extends React.Component { + return should; + } + +- componentWillReceiveProps(props) { +- if (props.type !== this.props.type) { ++ componentDidUpdate(prevProps) { ++ if (this.props.type !== prevProps.type) { + this.typeInfo = getTypeInfo(props.type); + } +- this.setState({ value: this.getTransformer().format(props.value) }); ++ this.setState({ value: this.getTransformer().format(this.props.value) }); + } + + onChange(value) { +@@ -592,11 +592,11 @@ export class List extends Component { + this.state.keys = this.state.value.map(() => props.ctx.uidGenerator.next()); + } + +- componentWillReceiveProps(props) { +- if (props.type !== this.props.type) { ++ componentDidUpdate(prevProps) { ++ if (this.props.type !== prevProps.type) { + this.typeInfo = getTypeInfo(props.type); + } +- const value = this.getTransformer().format(props.value); ++ const value = this.getTransformer().format(this.props.value) + this.setState({ + value, + keys: toSameLength(value, this.state.keys, props.ctx.uidGenerator) diff --git a/node_modules/tcomb-form-native/lib/templates/bootstrap/select.ios.js b/node_modules/tcomb-form-native/lib/templates/bootstrap/select.ios.js index 56385d1..488b6e9 100644 --- a/node_modules/tcomb-form-native/lib/templates/bootstrap/select.ios.js diff --git a/boilerplate/test/spec/modules/account/password-reset/forgot-passsword.sagas.spec.js b/boilerplate/test/spec/modules/account/password-reset/forgot-passsword.sagas.spec.js index 87b76d020..b3f918021 100644 --- a/boilerplate/test/spec/modules/account/password-reset/forgot-passsword.sagas.spec.js +++ b/boilerplate/test/spec/modules/account/password-reset/forgot-passsword.sagas.spec.js @@ -21,5 +21,5 @@ test('forgot password failure path', () => { // Step 1: Hit the api expect(step(response)).toEqual(call(FixtureAPI.forgotPassword, 'not-valid@gmail.com')) // Step 2: Successful return and data! - expect(step(response)).toEqual(put(ForgotPasswordActions.forgotPasswordFailure('WRONG'))) + expect(step(response)).toEqual(put(ForgotPasswordActions.forgotPasswordFailure('Something when wrong resetting your password'))) }) diff --git a/boilerplate/test/spec/modules/account/password/change-passsword.sagas.spec.js b/boilerplate/test/spec/modules/account/password/change-passsword.sagas.spec.js index f574c78e5..2454be5bf 100644 --- a/boilerplate/test/spec/modules/account/password/change-passsword.sagas.spec.js +++ b/boilerplate/test/spec/modules/account/password/change-passsword.sagas.spec.js @@ -21,5 +21,5 @@ test('change password failure path', () => { // Step 1: Hit the api step() // Step 2: Successful return and data! - expect(step(response)).toEqual(put(ChangePasswordActions.changePasswordFailure('WRONG'))) + expect(step(response)).toEqual(put(ChangePasswordActions.changePasswordFailure('Failed to change password'))) }) diff --git a/boilerplate/test/spec/modules/account/register/register.sagas.spec.js b/boilerplate/test/spec/modules/account/register/register.sagas.spec.js index 78fdb2819..36e82acc6 100644 --- a/boilerplate/test/spec/modules/account/register/register.sagas.spec.js +++ b/boilerplate/test/spec/modules/account/register/register.sagas.spec.js @@ -21,5 +21,5 @@ test('register failure path', () => { // Call the API with a failing registration expect(step({ user: 'user-bad-bad' })).toEqual(call(FixtureAPI.register, 'user-bad')) // Finish the saga with failure - expect(step(response)).toEqual(put(RegisterActions.registerFailure(response.data))) + expect(step(response)).toEqual(put(RegisterActions.registerFailure(response.data.title))) }) diff --git a/boilerplate/test/spec/modules/login/login.sagas.spec.js.ejs b/boilerplate/test/spec/modules/login/login.sagas.spec.js.ejs index ae89cdd9f..73c5a170a 100644 --- a/boilerplate/test/spec/modules/login/login.sagas.spec.js.ejs +++ b/boilerplate/test/spec/modules/login/login.sagas.spec.js.ejs @@ -74,7 +74,7 @@ test('login failure path', () => { // set response.ok to false as if the oauth issuer is down response.ok = false - expect(step(response)).toEqual(put(LoginActions.loginFailure('WRONG'))) + expect(step(response)).toEqual(put(LoginActions.loginFailure('Could not connect to OAuth2 Provider'))) <%_} else { _%> <%_ if (props.authType === 'session') { _%> <%_ if (props.oldSessionAuthParameters) { _%> @@ -95,7 +95,7 @@ test('login failure path', () => { // Attempt to login and fail expect(step(response)).toEqual(call(FixtureAPI.login, authObj)) // Send the error - expect(step(response)).toEqual(put(LoginActions.loginFailure('WRONG'))) + expect(step(response)).toEqual(put(LoginActions.loginFailure('Bad Credentials'))) <%_ } _%> }) <%_ if (props.authType === 'jwt' || props.authType === 'oauth2') { _%> diff --git a/boilerplate/test/spec/shared/sagas/account.sagas.spec.js b/boilerplate/test/spec/shared/sagas/account.sagas.spec.js index 2466a90fc..9b08eb76a 100644 --- a/boilerplate/test/spec/shared/sagas/account.sagas.spec.js +++ b/boilerplate/test/spec/shared/sagas/account.sagas.spec.js @@ -22,7 +22,7 @@ test('get failure path', () => { // Step 1: Hit the api step() // Step 2: Failed response. - expect(step(response)).toEqual(put(AccountActions.accountFailure('WRONG'))) + expect(step(response)).toEqual(put(AccountActions.accountFailure('Failed to get account'))) }) test('update success path', () => { @@ -40,5 +40,5 @@ test('update failure path', () => { // Step 1: Hit the api step() // Step 2: Failed response. - expect(step(response)).toEqual(put(AccountActions.accountUpdateFailure('WRONG'))) + expect(step(response)).toEqual(put(AccountActions.accountUpdateFailure('Failed to update account'))) }) diff --git a/src/entity/files.js b/src/entity/files.js index a3d49ce9b..f0c563c65 100644 --- a/src/entity/files.js +++ b/src/entity/files.js @@ -419,7 +419,7 @@ export const ${camelCase(props.name)}EntityEditScreen = data => { target: 'app/shared/sagas/index.js' }, { target: 'app/shared/reducers/index.js' }, { target: 'app/navigation/layouts.js' }, - { target: 'app/modules/entities/entities-screen.js' }, + { target: 'app/modules/entities/entities-screen.js' } ] await prettierTransformBatch(filesToRunPrettierOn) }