[ML] Settings/Calendars: Prevent new calendar save if id already exists (#27104) (#27224)

* Prevent new calendar save if duplicate id

* add test for duplicate id detection

* Use danger notif for duplicate id error
This commit is contained in:
Melissa Alvarez 2018-12-14 11:34:48 -06:00 committed by GitHub
parent b7af462dc0
commit 5bb75a2b9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 10 deletions

View file

@ -103,17 +103,35 @@ export class NewCalendar extends Component {
}
}
onCreate = async () => {
const calendar = this.setUpCalendarForApi();
this.setState({ saving: true });
isDuplicateId = () => {
const { calendars, formCalendarId } = this.state;
try {
await ml.addCalendar(calendar);
window.location = `${chrome.getBasePath()}/app/ml#/settings/calendars_list`;
} catch (error) {
console.log('Error saving calendar', error);
this.setState({ saving: false });
toastNotifications.addDanger(`An error occurred creating calendar ${calendar.calendarId}`);
for (let i = 0; i < calendars.length; i++) {
if (calendars[i].calendar_id === formCalendarId) {
return true;
}
}
return false;
}
onCreate = async () => {
const { formCalendarId } = this.state;
if (this.isDuplicateId()) {
toastNotifications.addDanger(`Cannot create calendar with id [${formCalendarId}] as it already exists.`);
} else {
const calendar = this.setUpCalendarForApi();
this.setState({ saving: true });
try {
await ml.addCalendar(calendar);
window.location = `${chrome.getBasePath()}/app/ml#/settings/calendars_list`;
} catch (error) {
console.log('Error saving calendar', error);
this.setState({ saving: false });
toastNotifications.addDanger(`An error occurred creating calendar ${calendar.calendarId}`);
}
}
}

View file

@ -50,6 +50,32 @@ import { shallow, mount } from 'enzyme';
import React from 'react';
import { NewCalendar } from './new_calendar';
const calendars = [
{
'calendar_id': 'farequote-calendar',
'job_ids': ['farequote'],
'description': 'test ',
'events': [{
'description': 'Downtime feb 9 2017 10:10 to 10:30',
'start_time': 1486656600000,
'end_time': 1486657800000,
'calendar_id': 'farequote-calendar',
'event_id': 'Ee-YgGcBxHgQWEhCO_xj'
}]
},
{
'calendar_id': 'this-is-a-new-calendar',
'job_ids': ['test'],
'description': 'new calendar',
'events': [{
'description': 'New event!',
'start_time': 1544076000000,
'end_time': 1544162400000,
'calendar_id': 'this-is-a-new-calendar',
'event_id': 'ehWKhGcBqHkXuWNrIrSV'
}]
}];
describe('NewCalendar', () => {
test('Renders new calendar form', () => {
@ -84,4 +110,18 @@ describe('NewCalendar', () => {
expect(wrapper.state('isNewEventModalVisible')).toBe(true);
});
test('isDuplicateId returns true if form calendar id already exists in calendars', () => {
const wrapper = mount(
<NewCalendar />
);
const instance = wrapper.instance();
instance.setState({
calendars,
formCalendarId: calendars[0].calendar_id
});
wrapper.update();
expect(instance.isDuplicateId()).toBe(true);
});
});