changes schema editor to jotai

This commit is contained in:
Emmaline Autumn 2024-09-04 04:28:57 -06:00
parent f87a759048
commit b529445851
5 changed files with 67 additions and 47 deletions

View File

@ -1,14 +1,16 @@
import { useRecoilValue } from "recoil"; // import { useRecoilValue } from "recoil";
import { SchemaEditAtom } from "../../recoil/atoms/schema"; import { SchemaEditAtom } from "../../recoil/atoms/schema";
import { TEMPLATE_TYPES } from "../../constants/TemplateTypes"; import { TEMPLATE_TYPES } from "../../constants/TemplateTypes";
import { FC, PropsWithChildren } from "react"; import { FC, PropsWithChildren } from "react";
import { useAtom } from "jotai";
interface IProps { interface IProps {
bind: InputBinder; bind: InputBinder;
} }
export const FieldTypeInput: FC<PropsWithChildren<IProps>> = ({ bind }) => { export const FieldTypeInput: FC<PropsWithChildren<IProps>> = ({ bind }) => {
const schema = useRecoilValue(SchemaEditAtom); // const schema = useRecoilValue(SchemaEditAtom);
const [schema] = useAtom(SchemaEditAtom);
return ( return (
<label className="w-min"> <label className="w-min">

View File

@ -5,7 +5,6 @@ import AnimatedPageContainer from "@/components/AnimatedPageContainer";
import { TypeEditor } from "./type-editor"; import { TypeEditor } from "./type-editor";
import { useObjectStateWrapper } from "@/hooks/useObjectState"; import { useObjectStateWrapper } from "@/hooks/useObjectState";
import { useInput } from "../../hooks/useInput"; import { useInput } from "../../hooks/useInput";
import { useRecoilState } from "recoil";
import { SchemaEditAtom } from "@/recoil/atoms/schema"; import { SchemaEditAtom } from "@/recoil/atoms/schema";
import { SchemaViewer } from "./schema-viewer"; import { SchemaViewer } from "./schema-viewer";
import { TemplateEditor } from "./template-editor"; import { TemplateEditor } from "./template-editor";
@ -14,9 +13,11 @@ import { useParams } from "next/navigation";
import { FieldTypes } from "./fieldtypes"; import { FieldTypes } from "./fieldtypes";
import { findSchema, saveSchemaDb } from "@/actions/Schemas/index"; import { findSchema, saveSchemaDb } from "@/actions/Schemas/index";
import { useToast } from "../toast"; import { useToast } from "../toast";
import { useAtom } from "jotai";
import { Schema } from "@/types";
export const SchemaBuilder: FC = () => { export const SchemaBuilder: FC = () => {
const [schema, setSchema] = useRecoilState(SchemaEditAtom); const [schema, setSchema] = useAtom(SchemaEditAtom);
// const resetSchema = useResetRecoilState(SchemaEditAtom); // const resetSchema = useResetRecoilState(SchemaEditAtom);
const { createToast } = useToast(); const { createToast } = useToast();
const { update: updateSchema, bindProperty: bindSchemaProperty } = const { update: updateSchema, bindProperty: bindSchemaProperty } =
@ -84,8 +85,8 @@ export const SchemaBuilder: FC = () => {
} = useInput("", { disallowSpaces: true }); } = useInput("", { disallowSpaces: true });
const addSchemaField = useCallback(() => { const addSchemaField = useCallback(() => {
updateSchema((s) => ({ updateSchema((s) => ({
schema: { fields: {
...s.schema, ...s.fields,
[schemaFieldName]: FieldTypes.any, [schemaFieldName]: FieldTypes.any,
}, },
})); }));
@ -93,11 +94,11 @@ export const SchemaBuilder: FC = () => {
}, [resetSchemaFieldName, schemaFieldName, updateSchema]); }, [resetSchemaFieldName, schemaFieldName, updateSchema]);
const updateSchemaField = useCallback( const updateSchemaField = useCallback(
(key: string, template: Template) => { (key: string, fieldType: FieldTypes) => {
updateSchema((s) => ({ updateSchema((s) => ({
schema: { fields: {
...s.schema, ...s.fields,
[key]: template, [key]: fieldType,
}, },
})); }));
}, },
@ -134,12 +135,12 @@ export const SchemaBuilder: FC = () => {
</button> </button>
</div> </div>
<ul className="rounded-lg overflow-hidden"> <ul className="rounded-lg overflow-hidden">
{Object.entries(schema.schema).map( {Object.entries(schema.fields).map(
([schemaFieldKey, schemaField]) => ( ([schemaFieldKey, schemaField]) => (
<TemplateEditor <TemplateEditor
key={schemaFieldKey} key={schemaFieldKey}
templateKey={schemaFieldKey} templateKey={schemaFieldKey}
template={schemaField} fieldType={schemaField as FieldTypes}
update={updateSchemaField} update={updateSchemaField}
/> />
), ),

View File

@ -1,39 +1,45 @@
import { FC, useCallback } from "react"; import { FC, useCallback, useEffect } from "react";
import { useObjectStateWrapper } from "@/hooks/useObjectState";
import { TEMPLATE_TYPES } from "@/constants/TemplateTypes"; import { TEMPLATE_TYPES } from "@/constants/TemplateTypes";
import { SchemaEditAtom } from "@/recoil/atoms/schema"; import { SchemaEditAtom } from "@/recoil/atoms/schema";
import { useRecoilState } from "recoil";
import { Icon } from "@/components/Icon"; import { Icon } from "@/components/Icon";
import { FieldTypes } from "./fieldtypes";
import { useAtom } from "jotai";
import { useInput } from "@/hooks/useInput";
import { Schema } from "@/types";
interface IProps { interface IProps {
templateKey: string; templateKey: string;
update: (arg0: string, arg1: Template) => void; update: (arg0: string, arg1: FieldTypes) => void;
template: Template; fieldType: FieldTypes;
} }
export const TemplateEditor: FC<IProps> = ( export const TemplateEditor: FC<IProps> = ({
{ templateKey, update, template }, templateKey,
) => { update,
const [schema, setSchema] = useRecoilState(SchemaEditAtom); fieldType,
const updateTemplate = useCallback( }) => {
(t: Template | ((arg: Template) => Template)) => { const [schema, setSchema] = useAtom(SchemaEditAtom);
update(templateKey, typeof t === "function" ? t(template) : t); // const updateTemplate = useCallback(
}, // (t: FieldTypes | ((arg: FieldTypes) => FieldTypes)) => {
[templateKey, update, template], // update(templateKey, typeof t === "function" ? t(template) : t);
); // },
// [templateKey, update, template],
// );
const { bindProperty } = useObjectStateWrapper( // const { bindProperty } = useObjectStateWrapper(template, updateTemplate);
template, const { bind: bindFieldType, value } = useInput(fieldType);
updateTemplate,
);
const deleteTemplate = useCallback(() => { useEffect(() => {
update(templateKey, value);
});
const deleteField = useCallback(() => {
setSchema((s: Schema) => { setSchema((s: Schema) => {
const templates = { ...s.schema }; const fields = { ...s.fields };
delete templates[templateKey]; delete fields[templateKey];
return { return {
...s, ...s,
schema: templates, schema: fields,
}; };
}); });
}, [setSchema, templateKey]); }, [setSchema, templateKey]);
@ -46,24 +52,24 @@ export const TemplateEditor: FC<IProps> = (
Type: Type:
<input <input
type="text" type="text"
{...bindProperty("type", { disallowSpaces: true })} {...bindFieldType}
list="type-editor-type-list" list="type-editor-type-list"
/> />
<datalist id="type-editor-type-list"> <datalist id="type-editor-type-list">
{Object.keys(TEMPLATE_TYPES).map((k) => ( {Object.keys(TEMPLATE_TYPES).map((k) => (
<option key={"templatetype" + k} value={k}>{k}</option> <option key={"templatetype" + k} value={k}>
{k}
</option>
))} ))}
{Object.keys(schema.types).map((k) => ( {Object.keys(schema.types).map((k) => (
<option key={"schematype" + k} value={k}>{k}</option> <option key={"schematype" + k} value={k}>
{k}
</option>
))} ))}
</datalist> </datalist>
</label> </label>
<textarea {...bindProperty("display")} cols={30} rows={10}></textarea>
</div> </div>
<button <button className="no-default" onClick={deleteField}>
className="no-default"
onClick={deleteTemplate}
>
<Icon <Icon
icon="Trash" icon="Trash"
className="svg-red-700 hover:svg-red-500 trash-can w-6 h-6" className="svg-red-700 hover:svg-red-500 trash-can w-6 h-6"

View File

@ -1,6 +1,15 @@
import { atom } from "recoil"; // import { atom } from "recoil";
import { atom } from "jotai";
// export const SchemaEditAtom = atom<Schema>({
// key: "schema-edit",
// default: { name: "", types: {}, schema: {}, id: "" },
// });
export const SchemaEditAtom = atom<Schema>({ export const SchemaEditAtom = atom<Schema>({
key: "schema-edit", name: "",
default: { name: "", types: {}, schema: {}, id: "" }, id: "",
types: {},
fields: {},
}); });

4
types.d.ts vendored
View File

@ -1,3 +1,5 @@
import { FieldTypes } from "./components/schema/fieldtypes";
// MD Parser // MD Parser
type IdentifiedToken<M> = { type IdentifiedToken<M> = {
metadata: M; metadata: M;
@ -76,7 +78,7 @@ type Template = {
display: string; display: string;
}; };
type SchemaFields = Record<string, string>; type SchemaFields = Record<string, FieldTypes>;
type SchemaTypes = Record<string, TypeType>; type SchemaTypes = Record<string, TypeType>;