Skip to content

Commit

Permalink
add create list
Browse files Browse the repository at this point in the history
  • Loading branch information
craigrbarnes committed Oct 3, 2024
1 parent 64cd7c1 commit 0b28c01
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 15 deletions.
6 changes: 6 additions & 0 deletions docs/LocalNotes/JSONSchema_support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The typescript configuration interfaces can be used to generate JSON Schema using something like:
```
ts-json-schema-generator --path src/features/Discovery/types.ts --type DiscoveryConfig --tsconfig tsconfig.json --out DiscoveryConfig_schema.json
```

Something like this: https://github.com/json-editor/json-editor can be used to develop editors/validators for configuration files
77 changes: 77 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"targetDefaults": {
"lint": {
"cache": true,
"dependsOn": [
"^lint"
]
},
"compile": {
"cache": true,
"dependsOn": [
"^compile"
]
},
"clean": {
"dependsOn": []
},
"types": {
"cache": true,
"dependsOn": []
},
"build": {
"cache": true,
"dependsOn": []
},
"build:clean": {
"dependsOn": [
]
},
"build:watch": {
"dependsOn": [
]
},
"test": {
"cache": true,
"dependsOn": []
},
"test:watch": {
"cache": true,
"dependsOn": []
},
"test:int": {
"cache": true,
"dependsOn": []
},
"test:all": {
"cache": true,
"dependsOn": []
},
"dev": {
"dependsOn": [
]
},
"rollup": {
"dependsOn": [
]
},
"copy-tailwind": {
"dependsOn": []
},
"start": {
"dependsOn": []
},
"build:colors": {
"dependsOn": []
},
"build:icons": {
"dependsOn": []
},
"getSchema": {
"dependsOn": []
},
"getDRSToHostname": {
"dependsOn": []
}
}
}
4 changes: 2 additions & 2 deletions packages/core/src/features/dataLibrary/dataLibraryApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export const dataLibraryApi = dataLibraryTags.injectEndpoints({
}),
invalidatesTags: [TAGS],
}),
addDataLibraryList: builder.mutation<void, DataList>({
addDataLibraryList: builder.mutation<void, Partial<DataList> | undefined>({
query: (list) => ({
url: `${GEN3_DATA_LIBRARY_API}/${nanoid()}`,
method: 'POST',
body: list,
body: list ?? {},
}),
invalidatesTags: [TAGS],
}),
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/features/dataLibrary/dataLibraryIndexDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const deleteAll = async () => {
};

export const addListToDataLibraryIndexDB = async (
body: DataList,
body?: Partial<DataList>,
): Promise<ReturnStatus> => {
const timestamp = new Date().toJSON();
try {
Expand All @@ -87,12 +87,13 @@ export const addListToDataLibraryIndexDB = async (
tx.objectStore(STORE_NAME).put({
id,
version: 0,
items: body,
items: body?.items ?? {},
creator: '{{subject_id}}',
authz: {
version: 0,
authz: [`/users/{{subject_id}}/user-library/lists/${id}`],
},
name: body?.name ?? 'New List',
created_time: timestamp,
updated_time: timestamp,
});
Expand Down
27 changes: 24 additions & 3 deletions packages/core/src/features/dataLibrary/useDataLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ export const useDataLibrary = (useApi: boolean) => {

let hasError = false;

const generateUniqueName = (baseName: string = 'List') => {
let uniqueName = baseName;
let counter = 1;

const existingNames = dataLibraryItems?.lists
? Object.values(dataLibraryItems?.lists).map((x) => x.name)
: [];

while (existingNames.includes(uniqueName)) {
uniqueName = `${baseName} ${counter}`;
counter++;
}

return uniqueName;
};

const refetchLocalLists = async () => {
const { isError, lists } = await getDataLibraryListIndexDB();
setLocalLibrary(lists ?? {});
Expand All @@ -52,12 +68,17 @@ export const useDataLibrary = (useApi: boolean) => {
fetchLibrary();
}, [useApi]);

const addListToDataLibrary = async (item: DataList) => {
const addListToDataLibrary = async (item?: Partial<DataList>) => {
const adjustedData = {
...(item ?? {}),
name: generateUniqueName(item?.name ?? 'List'),
};

if (useApi) {
await addItemToLibraryApi(item);
await addItemToLibraryApi(adjustedData);
refetchLibraryFromApi();
} else {
const { isError } = await addListToDataLibraryIndexDB(item);
const { isError } = await addListToDataLibraryIndexDB(adjustedData);
await refetchLocalLists();
hasError = isError === true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const DataLibraryPanel = () => {

const {
dataLibraryItems,
addListToDataLibrary,
setAllListsInDataLibrary,
clearLibrary,
updateListInDataLibrary,
Expand All @@ -29,7 +30,6 @@ const DataLibraryPanel = () => {

const updateList = async (listId: string, update: Record<string, any>) => {
if (!dataLibraryItems) return;
console.log('updateList from list', listId);
await updateListInDataLibrary(listId, {
...dataLibraryItems.lists[listId],
...update,
Expand All @@ -40,7 +40,6 @@ const DataLibraryPanel = () => {
const removeItemFromList = async (listId: string, itemId: string) => {
if (!dataLibraryItems) return;

console.log('remove from list', listId, itemId);
const { [itemId]: removedKey, ...newObject } =
dataLibraryItems.lists[listId].items;
await updateListInDataLibrary(listId, {
Expand All @@ -51,7 +50,6 @@ const DataLibraryPanel = () => {
};

useDeepCompareEffect(() => {
console.log('DataLibraryPanel: dataLibraryItems', dataLibraryItems);
const savedLists = Object.entries(dataLibraryItems?.lists ?? {}).map(
// for each List
([listId, dataList]) => {
Expand Down Expand Up @@ -104,7 +102,7 @@ const DataLibraryPanel = () => {

return (
<div className="flex flex-col w-full ml-2">
<SearchAndActions />
<SearchAndActions createList={addListToDataLibrary} />
<div className="flex flex-col">
{currentLists.map(({ id, name, datasetItems }) => {
return (
Expand Down
17 changes: 13 additions & 4 deletions packages/frontend/src/features/DataLibrary/SearchAndActions.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import React from 'react';
import { Button, Group, TextInput, Text } from '@mantine/core';
import { Button, Group, TextInput } from '@mantine/core';
import {
MdAdd as PlusIcon,
MdDelete as DeleteIcon,
MdSearch as SearchIcon,
} from 'react-icons/md';
import { DataList } from '@gen3/core';

const SearchAndActions = () => {
interface SearchAndActionsProps {
createList: (item?: Partial<DataList>) => Promise<void>;
}

const SearchAndActions: React.FC<SearchAndActionsProps> = ({ createList }) => {
return (
<div className="flex flex-col w-full ml-2">
<Group grow>
Expand All @@ -16,11 +21,15 @@ const SearchAndActions = () => {
leftSection={<SearchIcon size="1.45em" />}
/>

<Button variant="outline" onClick={() => {}}>
<Button
variant="outline"
onClick={() => createList()}
aria-label="create a new list"
>
<PlusIcon size="1.5em" />
</Button>

<Button variant="outline">
<Button variant="outline" aria-label="delete selected list">
<DeleteIcon size="1.5em" />
</Button>
</Group>
Expand Down

0 comments on commit 0b28c01

Please sign in to comment.