From 6a3e9a0bbfb4ec42c2ed80e1111a63e55934f86d Mon Sep 17 00:00:00 2001 From: GammaMicrowave Date: Wed, 5 Jul 2023 21:15:03 +0530 Subject: [PATCH] added util function to convert JSONSchema back to options --- components/utils/API/index.js | 2 +- components/utils/JSONSchema/index.js | 102 ++++++++++-------- pages/dashboard/project/[id]/newform/index.js | 2 +- 3 files changed, 59 insertions(+), 47 deletions(-) diff --git a/components/utils/API/index.js b/components/utils/API/index.js index 1c3329f..65ef609 100644 --- a/components/utils/API/index.js +++ b/components/utils/API/index.js @@ -7,7 +7,7 @@ const API_URL = : process.env.NEXT_PUBLIC_ENVIORNMENT === "pro-dev" ? "http://localhost:8080" : "https://dev-api.savemyform.tk"; - + const getAccessToken = () => { return getLS("secret"); }; diff --git a/components/utils/JSONSchema/index.js b/components/utils/JSONSchema/index.js index 6d0a7a2..45a54d5 100644 --- a/components/utils/JSONSchema/index.js +++ b/components/utils/JSONSchema/index.js @@ -1,50 +1,62 @@ const options = { - email: { type: "string", format: "email" }, - checkbox: { type: "boolean" }, - color: { - type: "string", - pattern: "^#[0-9a-f]{3}([0-9a-f]{3})?$", - }, - date: { type: "string", format: "date" }, - "datetime-local": { type: "string", format: "date-time" }, - image: { - type: "string", - format: "uri", - }, - hidden: { type: "boolean" }, - month: { type: "integer", minimum: 1, maximum: 12 }, - number: { type: "number" }, - radio: { type: "boolean" }, - range: { type: "number" }, - reset: { type: "boolean" }, - search: { type: "string" }, - tel: { - type: "string", - minLength: 10, - maxLength: 20, - pattern: "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$", - }, - text: { type: "string" }, - time: { type: "string", format: "time" }, - url: { type: "string", format: "uri" }, - week: { type: "number", minimum: 1, maximum: 53 }, - file: { type: "string", format: "uri" }, - password: { type: "string" }, + email: { type: "string", format: "email" }, + checkbox: { type: "boolean" }, + color: { + type: "string", + pattern: "^#[0-9a-f]{3}([0-9a-f]{3})?$", + }, + date: { type: "string", format: "date" }, + "datetime-local": { type: "string", format: "date-time" }, + image: { + type: "string", + format: "uri", + }, + hidden: { type: "boolean" }, + month: { type: "integer", minimum: 1, maximum: 12 }, + number: { type: "number" }, + radio: { type: "boolean" }, + range: { type: "number" }, + reset: { type: "boolean" }, + search: { type: "string" }, + tel: { + type: "string", + minLength: 10, + maxLength: 20, + pattern: "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$", + }, + text: { type: "string" }, + time: { type: "string", format: "time" }, + url: { type: "string", format: "uri" }, + week: { type: "number", minimum: 1, maximum: 53 }, + file: { type: "string" }, + password: { type: "string" }, }; -const createJSONSchema = (inputs) => { - let JSONSchema = { - type: "object", - properties: {}, - required: [], - additionalProperties: false, +export const createJSONSchema = (inputs) => { + let JSONSchema = { + type: "object", + properties: {}, + required: [], + additionalProperties: false, + }; + inputs.forEach((input) => { + JSONSchema.properties[input.name] = options[input.type]; + if (input.isRequired) { + JSONSchema.required.push(input.name); + } + }); + return JSONSchema; +}; + +export const schemaToInputsConverter = (schema) => { + const inputs = []; + for (const [key, value] of Object.entries(schema.properties)) { + const input = { + name: key, + type: value.type, + isRequired: schema.required.includes(key), }; - inputs.forEach((input) => { - JSONSchema.properties[input.name] = options[input.type]; - if (input.isRequired) { - JSONSchema.required.push(input.name); - } - }); - return JSONSchema; + inputs.push(input); + } + return inputs; }; -export default createJSONSchema; diff --git a/pages/dashboard/project/[id]/newform/index.js b/pages/dashboard/project/[id]/newform/index.js index 7dff681..b6532e5 100644 --- a/pages/dashboard/project/[id]/newform/index.js +++ b/pages/dashboard/project/[id]/newform/index.js @@ -6,7 +6,7 @@ import { useQuery, dehydrate, QueryClient } from "@tanstack/react-query"; import DashboardVector from "../../../../../assets/svgs/dashboardsVector.svg"; import FormInput from "../../../../../components/elements/FormInput"; import Footer from "../../../../../components/elements/Footer"; -import createJSONSchema from "../../../../../components/utils/JSONSchema"; +import {createJSONSchema} from "../../../../../components/utils/JSONSchema"; import { useRouter } from "next/router"; import { AppbarContext, UserContext } from "../../../../../components/context"; import { get, post, API_URL } from "../../../../../components/utils/API";