From 9744cf0117d73b64b14ebfc7765fa93aafbd82e4 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 14 Jan 2022 13:00:27 +0100 Subject: [PATCH] #193 - Added data type support --- package.json | 44 ++++++++++++++++++- src/constants/settings.ts | 1 + .../components/DataView/DataView.tsx | 26 +++++++++-- .../components/DataView/SortableItem.tsx | 9 ++-- src/dashboardWebView/models/Settings.ts | 2 + src/helpers/DashboardSettings.ts | 6 ++- src/models/DataFile.ts | 3 +- src/models/DataType.ts | 4 ++ 8 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 src/models/DataType.ts diff --git a/package.json b/package.json index 3d7e2ce5..752057cb 100644 --- a/package.json +++ b/package.json @@ -358,6 +358,11 @@ "default": {}, "description": "The JSON schema for your data which will be used to render the data form.", "additionalProperties": true + }, + "type": { + "type": "string", + "default": "content", + "description": "If you are using data types, you can specify your type ID." } }, "additionalProperties": false, @@ -365,8 +370,45 @@ "id", "title", "file", - "schema", "labelField" + ], + "anyOf": [ + { + "required": [ + "schema" + ] + }, + { + "required": [ + "type" + ] + } + ] + }, + "scope": "Data" + }, + "frontMatter.data.types": { + "type": "array", + "default": [], + "markdownDescription": "Specify the data types. These types can be used in for your data files. [Check in the docs](https://frontmatter.codes/docs/settings#frontmatter.data.types)", + "items": { + "type": "object", + "default": {}, + "properties": { + "id": { + "type": "string", + "description": "Your unique ID you want to use for your data type." + }, + "schema": { + "type": "object", + "default": {}, + "description": "The JSON schema for your data which will be used to render the data form.", + "additionalProperties": true + } + }, + "required": [ + "id", + "schema" ] }, "scope": "Data" diff --git a/src/constants/settings.ts b/src/constants/settings.ts index 166fa47e..45c612e4 100644 --- a/src/constants/settings.ts +++ b/src/constants/settings.ts @@ -56,6 +56,7 @@ export const SETTINGS_DASHBOARD_OPENONSTART = "dashboard.openOnStart"; export const SETTINGS_DASHBOARD_MEDIA_SNIPPET = "dashboard.mediaSnippet"; export const SETTINGS_DATA_FILES = "data.files"; +export const SETTINGS_DATA_TYPES = "data.types"; export const SETTINGS_FRAMEWORK_ID = "framework.id"; diff --git a/src/dashboardWebView/components/DataView/DataView.tsx b/src/dashboardWebView/components/DataView/DataView.tsx index bb619e70..4c107d34 100644 --- a/src/dashboardWebView/components/DataView/DataView.tsx +++ b/src/dashboardWebView/components/DataView/DataView.tsx @@ -18,6 +18,7 @@ import { SortableItem } from './SortableItem'; import { ChevronRightIcon } from '@heroicons/react/outline'; import { ToastContainer, toast, Slide } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; +import { DataType } from '../../../models/DataType'; export interface IDataViewProps {} @@ -75,7 +76,7 @@ export const DataView: React.FunctionComponent = (props: React.P }); // Show toast message - toast.success("Wow so easy!", { + toast.success("Your entry was submitted", { position: "top-right", autoClose: 2000, hideProgressBar: true, @@ -115,6 +116,25 @@ export const DataView: React.FunctionComponent = (props: React.P Messenger.unlisten(messageListener); } }, []); + + // Retrieve the data files, check if they have a schema or ID, if not, they shouldn't be shown + const dataFiles = (settings?.dataFiles || []).map((dataFile: DataFile) => { + if (!dataFile.schema && !dataFile.id) { + return null; + } + + const clonedFile = Object.assign({}, dataFile); + + if (clonedFile.type) { + const dataType = settings?.dataTypes?.find((dataType: DataType) => dataType.id === clonedFile.type); + if (!dataType) { + return null; + } + clonedFile.schema = Object.assign({}, dataType.schema); + } + + return clonedFile; + }).filter(d => d !== null) as DataFile[]; return (
@@ -130,8 +150,8 @@ export const DataView: React.FunctionComponent = (props: React.P