Skip to content

Commit

Permalink
Show custom alert message for duplicate email address
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-white committed Dec 11, 2023
1 parent ecc02c9 commit 8e5d279
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/components/user/edit/basic-details.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ except according to the terms contained in the LICENSE file.

<script setup>
import { computed, inject, ref } from 'vue';
import { equals } from 'ramda';
import { useI18n } from 'vue-i18n';

import FormGroup from '../../form-group.vue';
Expand Down Expand Up @@ -70,7 +71,11 @@ const submit = () => {
user.email = data.email;
user.displayName = data.displayName;
user.updatedAt = data.updatedAt;
}
},
problemToAlert: ({ code, details }) =>
(code === 409.3 && equals(details.fields, ['email', 'deleted'])
? t('problem.409_3', { email: details.values[0] })
: null)
})
.then(() => { alert.success(t('alert.success')); })
.catch(noop);
Expand All @@ -86,6 +91,9 @@ const submit = () => {
"action": {
"update": "Update details"
},
"problem": {
"409_3": "You cannot change your email to {email} because this account already exists. Please try another email address."
},
"alert": {
"success": "User details saved!"
}
Expand Down
17 changes: 15 additions & 2 deletions src/components/user/new.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ except according to the terms contained in the LICENSE file.
</template>

<script>
import { equals } from 'ramda';

import FormGroup from '../form-group.vue';
import Modal from '../modal.vue';
import Spinner from '../spinner.vue';
Expand Down Expand Up @@ -80,7 +82,15 @@ export default {
submit() {
const postData = { email: this.email };
if (this.displayName !== '') postData.displayName = this.displayName;
this.request({ method: 'POST', url: '/v1/users', data: postData })
this.request({
method: 'POST',
url: '/v1/users',
data: postData,
problemToAlert: ({ code, details }) =>
(code === 409.3 && equals(details.fields, ['email', 'deleted'])
? this.$t('problem.409_3', { email: details.values[0] })
: null)
})
.then(response => {
this.$emit('success', response.data);
})
Expand All @@ -100,7 +110,10 @@ export default {
],
"oidcIntroduction": [
"Users on your login server must have a Central account to log in to Central. Once you create this account, the user on your login server with the email address you provide will be able to log in to Central."
]
],
"problem": {
"409_3": "It looks like {email} already has an account. Please try another email address."
}
}
}
</i18n>
Expand Down
45 changes: 45 additions & 0 deletions test/components/user/edit/basic-details.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,51 @@ describe('UserEditBasicDetails', () => {
});
});

describe('custom alert messages', () => {
beforeEach(() => {
mockLogin({ email: 'old@email.com' });
});

it('shows a message for a duplicate email', () =>
mockHttp()
.mount(UserEditBasicDetails, mountOptions())
.request(async (component) => {
await component.get('input[type="email"]').setValue('new@email.com');
return component.get('form').trigger('submit');
})
.respondWithProblem({
code: 409.3,
message: 'A resource already exists with email,deleted value(s) of new@email.com,false.',
details: {
fields: ['email', 'deleted'],
values: ['new@email.com', false]
}
})
.afterResponse(modal => {
modal.should.alert('danger', (message) => {
message.should.startWith('You cannot change your email to new@email.com');
});
}));

// I don't think a different uniqueness violation is currently possible.
// This is mostly about future-proofing.
it('does not show a message for a different uniqueness violation', () =>
mockHttp()
.mount(UserEditBasicDetails, mountOptions())
.request(async (component) => {
await component.get('input[type="email"]').setValue('new@email.com');
return component.get('form').trigger('submit');
})
.respondWithProblem({
code: 409.3,
message: 'A resource already exists with foo value(s) of bar.',
details: { fields: ['foo'], values: ['bar'] }
})
.afterResponse(modal => {
modal.should.alert('danger', 'A resource already exists with foo value(s) of bar.');
}));
});

describe('after a successful response', () => {
beforeEach(() => {
mockLogin({ displayName: 'Old Name' });
Expand Down
45 changes: 45 additions & 0 deletions test/components/user/new.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,51 @@ describe('UserNew', () => {
modal: true
}));

describe('custom alert messages', () => {
it('shows a message for a duplicate email', () =>
mockHttp()
.mount(UserNew, {
props: { state: true }
})
.request(async (modal) => {
await modal.get('input[type="email"]').setValue('new@email.com');
return modal.get('form').trigger('submit');
})
.respondWithProblem({
code: 409.3,
message: 'A resource already exists with email,deleted value(s) of new@email.com,false.',
details: {
fields: ['email', 'deleted'],
values: ['new@email.com', false]
}
})
.afterResponse(modal => {
modal.should.alert('danger', (message) => {
message.should.startWith('It looks like new@email.com already has an account.');
});
}));

// I don't think a different uniqueness violation is currently possible.
// This is mostly about future-proofing.
it('does not shows a message for a different uniqueness violation', () =>
mockHttp()
.mount(UserNew, {
props: { state: true }
})
.request(async (modal) => {
await modal.get('input[type="email"]').setValue('new@email.com');
return modal.get('form').trigger('submit');
})
.respondWithProblem({
message: 'A resource already exists with foo value(s) of bar.',
code: 409.3,
details: { fields: ['foo'], values: ['bar'] }
})
.afterResponse(modal => {
modal.should.alert('danger', 'A resource already exists with foo value(s) of bar.');
}));
});

describe('after a successful response', () => {
const submitWithSuccess = () => load('/users', { root: false })
.complete()
Expand Down
10 changes: 10 additions & 0 deletions transifex/strings_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4280,6 +4280,11 @@
"developer_comment": "This is the text for an action, for example, the text of a button."
}
},
"problem": {
"409_3": {
"string": "You cannot change your email to {email} because this account already exists. Please try another email address."
}
},
"alert": {
"success": {
"string": "User details saved!"
Expand Down Expand Up @@ -4366,6 +4371,11 @@
"0": {
"string": "Users on your login server must have a Central account to log in to Central. Once you create this account, the user on your login server with the email address you provide will be able to log in to Central."
}
},
"problem": {
"409_3": {
"string": "It looks like {email} already has an account. Please try another email address."
}
}
},
"UserResetPassword": {
Expand Down

0 comments on commit 8e5d279

Please sign in to comment.