Skip to content
This repository has been archived by the owner on Dec 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #192 from GammaMicrowave/main
Browse files Browse the repository at this point in the history
added util function to convert JSONSchema back to options
  • Loading branch information
BuddyLongLegs committed Jul 5, 2023
2 parents 2b8cc8c + 6a3e9a0 commit 1184bce
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 47 deletions.
2 changes: 1 addition & 1 deletion components/utils/API/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
};
Expand Down
102 changes: 57 additions & 45 deletions components/utils/JSONSchema/index.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion pages/dashboard/project/[id]/newform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

1 comment on commit 1184bce

@vercel
Copy link

@vercel vercel bot commented on 1184bce Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.