Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Commit

Permalink
Make OData sorting predictable and unique
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
Andrea Rosci authored and Andrea Rosci committed Mar 5, 2024
1 parent 2e768cb commit 6841a01
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 53 deletions.
12 changes: 6 additions & 6 deletions src/AutoUI/Filters/Filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ const removeFieldsWithNoFilter = (schema: JSONSchema): JSONSchema => {
continue;
}
// Extract x-no-filter if available
const xNoFilter =
sieve.parseDescriptionProperty<string[] | boolean | undefined>(
value,
'x-no-filter',
);
const xNoFilter = sieve.parseDescriptionProperty<
string[] | boolean | undefined
>(value, 'x-no-filter');

if (xNoFilter === true) {
// Exclude property entirely if xNoFilter is true
continue;
}

const newValue: JSONSchemaDefinition = { ...value };
const xNoFilterSet = Array.isArray(xNoFilter) ? new Set(xNoFilter) : undefined;
const xNoFilterSet = Array.isArray(xNoFilter)
? new Set(xNoFilter)
: undefined;

if ('properties' in value) {
newValue.properties = processProperties(value.properties, xNoFilterSet);
Expand Down
86 changes: 43 additions & 43 deletions src/AutoUI/spec_tmp.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
import React from "react";
import { mount } from "enzyme";
import React from 'react';
import { mount } from 'enzyme';
import {
autoUIRunTransformers,
autoUIGetModelForCollection,
AutoUIProps,
AutoUI,
} from "./index";
autoUIRunTransformers,
autoUIGetModelForCollection,
AutoUIProps,
AutoUI,
} from './index';
import {
dataExample,
AugmentedSshKey,
transformers,
model as sshKeyModel,
} from "./models/example";
import { Provider, Table } from "rendition";
dataExample,
AugmentedSshKey,
transformers,
model as sshKeyModel,
} from './models/example';
import { Provider, Table } from 'rendition';
const props = {} as AutoUIProps<AugmentedSshKey>;
const TestAutoUI = () => <Demo {...props} />;

const Demo = ({ data, model, ...otherProps }: AutoUIProps<AugmentedSshKey>) => {
const memoizedData = React.useMemo(
() =>
autoUIRunTransformers(dataExample, transformers, {
accessRole: "administrator",
}),
[dataExample]
) as AugmentedSshKey[];
const memoizedData = React.useMemo(
() =>
autoUIRunTransformers(dataExample, transformers, {
accessRole: 'administrator',
}),
[dataExample],
) as AugmentedSshKey[];

return (
<AutoUI<AugmentedSshKey>
data={data ?? memoizedData}
model={model ?? autoUIGetModelForCollection(sshKeyModel)}
actions={[]}
{...otherProps}
/>
);
return (
<AutoUI<AugmentedSshKey>
data={data ?? memoizedData}
model={model ?? autoUIGetModelForCollection(sshKeyModel)}
actions={[]}
{...otherProps}
/>
);
};

describe("AutoUI", () => {
describe("Collection component", () => {
it("should render the collection with N results", () => {
const component = mount(
<Provider>
<TestAutoUI />
</Provider>
);
const table = component.find(Table);
const tableRows = table.find(
'[data-display="table-body"] [data-display="table-row"]'
);
describe('AutoUI', () => {
describe('Collection component', () => {
it('should render the collection with N results', () => {
const component = mount(
<Provider>
<TestAutoUI />
</Provider>,
);
const table = component.find(Table);
const tableRows = table.find(
'[data-display="table-body"] [data-display="table-row"]',
);

expect(tableRows).toHaveLength(dataExample.length);
});
});
expect(tableRows).toHaveLength(dataExample.length);
});
});
});
6 changes: 2 additions & 4 deletions src/oData/jsonToOData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export const orderbyBuilder = <T>(
const direction = !reverse ? 'asc' : 'desc';
const customOrderByKey = customSort?.[field];
if (typeof customOrderByKey === 'string') {
return `${customOrderByKey} ${direction}`;
return [`${customOrderByKey} ${direction}`, `id ${direction}`];
} else if (customOrderByKey != null && typeof customOrderByKey !== 'string') {
throw new Error(
`Field ${field} error: custom sort for this field must be of type string, ${typeof customOrderByKey} is not accepted.`,
Expand All @@ -241,7 +241,5 @@ export const orderbyBuilder = <T>(
if (refScheme) {
fieldPath += `/${refScheme.replace(/\[(.*?)\]/g, '').replace(/\./g, '/')}`;
}
return {
[fieldPath]: direction,
};
return [`${fieldPath} ${direction}`, `id ${direction}`];
};

0 comments on commit 6841a01

Please sign in to comment.