[ML] Settings/Calendars: Prevent new calendar save if id already exists (#27104)
* 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:
parent
6d96983745
commit
1108fcbc6a
|
@ -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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue