Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/jr/fagomrade #338

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ export const CheckboxTreeFilter: FC<Props> = ({ nodes, onCheck, filters }) => {
const [checked, setChecked] = React.useState<string[]>([]);
const [expanded, setExpanded] = React.useState<string[]>([]);
const [collapsed, setCollapsed] = React.useState(true);
const [searchOption, setSearchOption] = React.useState('');

useEffect(() => {
setChecked(filters);
}, [filters]);

const handleChecked = ({ value }) => {
setSearchOption('');

const path = getPath(nodes, value).map((item) => item.value);
if (checked.includes(value)) {
const newChecked = path.slice(0, -1);
Expand All @@ -70,11 +73,12 @@ export const CheckboxTreeFilter: FC<Props> = ({ nodes, onCheck, filters }) => {
};
const handleSearchOnChange = (value: any) => {
if (value !== null) {
setSearchOption(value);
const path = getPath(nodes, value).map((item) => item.value);
setExpanded(path);
onCheck?.(path);

const index = nodes.findIndex((item) => checked.includes(item.value));
const index = nodes.findIndex((item) => item.value === path[0]);
if (index >= 9 && collapsed) {
setCollapsed(false);
}
Expand All @@ -92,6 +96,7 @@ export const CheckboxTreeFilter: FC<Props> = ({ nodes, onCheck, filters }) => {
return (
<div>
<Select
value={searchOption}
options={getSearchOptions(nodes)}
onChange={handleSearchOnChange}
/>
Expand Down
19 changes: 14 additions & 5 deletions apps/concept-catalog/pages/[catalogId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,26 @@ export const SearchPage = ({

const getInternalFields = (fieldId) => fieldsResult?.internal?.find((field) => field.id === fieldId);

const getSubjectChildren = (subjectId: number) => {
const children = [];
subjectCodeList?.codes
.filter((code) => code.parentID === subjectId)
.map((code) => code.id)
.forEach((childId) => {
children.push(childId);
children.concat(getSubjectChildren(childId));
});

return children;
};

const getSubjectFilterWithChildren = (subjects) => {
// Fetch lowest level code and add its children to the filter
// Subjects will always be a path like: Code 1 -> Code 1.1 -> Code 1.1.1
// The lowest level code will always be the last code in the path, Code 1.1.1 in this example.
if (subjects.length > 0) {
const lowestLevelCodeId = subjects[subjects.length - 1];
const codes = [
...subjects,
...(subjectCodeList?.codes.filter((code) => `${code.parentID}` === lowestLevelCodeId).map((code) => code.id) ??
[]),
];
const codes = [...subjects, ...getSubjectChildren(+lowestLevelCodeId)];
return codes;
}
return [];
Expand Down
1 change: 0 additions & 1 deletion libs/utils/src/lib/code-list/code-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Children } from 'react';
import { convertCodeListToTreeNodes } from '.';

describe('convertCodeListToTreeNodes', () => {
Expand Down
11 changes: 11 additions & 0 deletions libs/utils/src/lib/validation/uuid.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { validUUID } from './uuid';

describe('validUUID', () => {
test('validate valid uuid', () => {
expect(validUUID('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b')).toStrictEqual(true);
expect(validUUID('596547fb-f496-804e-933c-1fc948dd6ada')).toStrictEqual(true);
});
test('validate invalid uuid', () => {
expect(validUUID('invalid-uuid')).toStrictEqual(false);
});
});
2 changes: 1 addition & 1 deletion libs/utils/src/lib/validation/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const validUUID = (uuid?: string) => {
return uuid?.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i) !== null;
return uuid?.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{3,4}-[0-9a-f]{3,4}-[0-9a-f]{12}$/i) !== null;
};