* 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
b7af462dc0
commit
5bb75a2b9e
|
@ -103,17 +103,35 @@ export class NewCalendar extends Component {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onCreate = async () => {
|
isDuplicateId = () => {
|
||||||
const calendar = this.setUpCalendarForApi();
|
const { calendars, formCalendarId } = this.state;
|
||||||
this.setState({ saving: true });
|
|
||||||
|
|
||||||
try {
|
for (let i = 0; i < calendars.length; i++) {
|
||||||
await ml.addCalendar(calendar);
|
if (calendars[i].calendar_id === formCalendarId) {
|
||||||
window.location = `${chrome.getBasePath()}/app/ml#/settings/calendars_list`;
|
return true;
|
||||||
} catch (error) {
|
}
|
||||||
console.log('Error saving calendar', error);
|
}
|
||||||
this.setState({ saving: false });
|
|
||||||
toastNotifications.addDanger(`An error occurred creating calendar ${calendar.calendarId}`);
|
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 React from 'react';
|
||||||
import { NewCalendar } from './new_calendar';
|
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', () => {
|
describe('NewCalendar', () => {
|
||||||
|
|
||||||
test('Renders new calendar form', () => {
|
test('Renders new calendar form', () => {
|
||||||
|
@ -84,4 +110,18 @@ describe('NewCalendar', () => {
|
||||||
expect(wrapper.state('isNewEventModalVisible')).toBe(true);
|
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