Skip to content

Commit

Permalink
update all componentWillReceiveProps to componentDidUpdate in main app
Browse files Browse the repository at this point in the history
also removes most WRONG error messages and uses the server-provided message
  • Loading branch information
ruddell committed Sep 19, 2019
1 parent 7051388 commit 7189b90
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,24 @@ 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
this.props.resetPassword(value.email)
}
}

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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
}
}
16 changes: 4 additions & 12 deletions boilerplate/app/modules/account/password/change-password-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' }])
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
}
}
16 changes: 4 additions & 12 deletions boilerplate/app/modules/account/register/register-screen.js.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' }])
}
Expand Down
2 changes: 1 addition & 1 deletion boilerplate/app/modules/account/register/register.sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
}
}
20 changes: 5 additions & 15 deletions boilerplate/app/modules/account/settings/settings-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,24 @@ 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
this.props.updateAccount(value)
}
}

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()
}
Expand Down
14 changes: 6 additions & 8 deletions boilerplate/app/modules/login/login-screen.js.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
7 changes: 4 additions & 3 deletions boilerplate/app/modules/login/login.sagas.js.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
}
}
Expand Down Expand Up @@ -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'))
}
}
<%_ } _%>
Expand Down
4 changes: 3 additions & 1 deletion boilerplate/app/shared/services/fixture-api.js.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export default {
} else {
return {
ok: false,
data: 'Invalid email'
data: {
title: 'Invalid email',
}
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion boilerplate/package.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions boilerplate/patches/tcomb-form-native+0.6.20.patch
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')))
})
Original file line number Diff line number Diff line change
Expand Up @@ -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')))
})
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})
4 changes: 2 additions & 2 deletions boilerplate/test/spec/modules/login/login.sagas.spec.js.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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) { _%>
Expand All @@ -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') { _%>
Expand Down
4 changes: 2 additions & 2 deletions boilerplate/test/spec/shared/sagas/account.sagas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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')))
})
2 changes: 1 addition & 1 deletion src/entity/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 7189b90

Please sign in to comment.