Skip to content

Commit

Permalink
fix(react-scheduler): use defaults when resource id is not specified …
Browse files Browse the repository at this point in the history
…in instances but is specified in an appointment (#2699)
  • Loading branch information
AryamnovEugeniy committed Feb 25, 2020
1 parent 2020ad2 commit 3ec51af
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
Expand Up @@ -53,7 +53,7 @@ export const ensureColor = (level, color) => (color[level] || PRIMARY_COLOR[leve

export const getResourceColor = (resources) => {
if (resources && resources.length) {
return resources.find(resource => resource.isMain).color;
return resources.find(resource => resource.isMain)?.color;
} return undefined;
};

Expand Down
@@ -1,6 +1,7 @@
import {
getWidthInPixels,
getViewCellKey,
getResourceColor,
} from './utils';

describe('Utils', () => {
Expand All @@ -20,4 +21,25 @@ describe('Utils', () => {
.toBe('test12');
});
});
describe('#getResourceColor', () => {
it('should return undefined if resources are not provided', () => {
expect(getResourceColor(undefined))
.toBeUndefined();
expect(getResourceColor([]))
.toBeUndefined();
});
it('should return undefined if it cannot find the main resource', () => {
const resources = [{ id: 1, isMain: false }, { id: 2, isMain: false }];
expect(getResourceColor(resources))
.toBeUndefined();
});
it('should return color of the main resource', () => {
const resources = [
{ id: 1, isMain: false, color: 'First color' },
{ id: 2, isMain: true, color: 'Main color' },
];
expect(getResourceColor(resources))
.toBe('Main color');
});
});
});
25 changes: 25 additions & 0 deletions packages/dx-scheduler-core/src/plugins/resources/helpers.test.ts
Expand Up @@ -126,5 +126,30 @@ describe('Resources helpers', () => {
{ id: 1, fieldName: 'locationId', allowMultiple: false },
]);
});
it('should return an empty array if resource instances do not contain the id specified in the appointment', () => {
const appointment = { ownerId: 'Invalid id' };
const resources = [{
fieldName: 'ownerId',
instances: [{ id: 0, fieldName: 'ownerId', allowMultiple: false }],
}];
const plainResources = [{ id: 0, fieldName: 'ownerId', allowMultiple: false }];

expect(getAppointmentResources(appointment, resources, plainResources))
.toEqual([]);
});
it('should return an empty without instances that are not provided in the instances when allowMultiple is true', () => {
const appointment = { ownerId: [0, 'Invalid id'] };
const resources = [{
fieldName: 'ownerId',
allowMultiple: true,
instances: [{ id: 0, fieldName: 'ownerId', allowMultiple: true }],
}];
const plainResources = [{ id: 0, fieldName: 'ownerId', allowMultiple: true }];

expect(getAppointmentResources(appointment, resources, plainResources))
.toEqual([
{ id: 0, fieldName: 'ownerId', allowMultiple: true },
]);
});
});
});
33 changes: 23 additions & 10 deletions packages/dx-scheduler-core/src/plugins/resources/helpers.ts
@@ -1,4 +1,5 @@
import { GetAppointmentResources, ValidResourceInstance } from '../../types';
import { GetAppointmentResources, ValidResourceInstance, ValidResource } from '../../types';
import { PureComputed } from '@devexpress/dx-core';

export const getAppointmentResources: GetAppointmentResources = (
appointment, resources, plainResources,
Expand All @@ -21,17 +22,29 @@ export const getAppointmentResources: GetAppointmentResources = (
if (resource.allowMultiple) {
return [
...acc,
...(appointmentResourceId as Array<number | string>).map(itemId => plainResources.find(
plainItem => resource.fieldName === plainItem.fieldName && plainItem.id === itemId),
),
...(appointmentResourceId as Array<number | string>)
.reduce((prevResources, itemId) => addResourceToAppointmentResources(
plainResources, prevResources, resource, itemId,
) as Array<ValidResourceInstance>, [] as Array<ValidResourceInstance>),
];
}

return [
...acc,
...(plainResources as Array<any>).find(plainItem =>
resource.fieldName === plainItem.fieldName && plainItem.id === appointmentResourceId,
),
];
return addResourceToAppointmentResources(
plainResources, acc, resource, appointmentResourceId,
) as Array<ValidResourceInstance>;
}, [] as Array<ValidResourceInstance>);
};

const addResourceToAppointmentResources: PureComputed<
[Array<ValidResourceInstance>, Array<ValidResourceInstance>, ValidResource,
number | string], Array<ValidResourceInstance>
> = (plainResources, appointmentResources, resource, resourceId) => {
const currentResource = plainResources.find(
plainItem => resource.fieldName === plainItem.fieldName && plainItem.id === resourceId,
);

return currentResource ? [
...appointmentResources,
currentResource!,
] : appointmentResources;
};

0 comments on commit 3ec51af

Please sign in to comment.