Skip to content

Commit

Permalink
rolled back tsup split for react-ui and use submitForm cb correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-burel committed Oct 3, 2022
1 parent b61365f commit 94906d5
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import React, { HTMLAttributes, PropsWithChildren } from "react";
import { useFormContext } from '@vulcanjs/react-ui';
import { Form } from 'react-bootstrap';
import { useFormContext } from "@vulcanjs/react-ui";
import { Form } from "react-bootstrap";

// TODO: not sure of the expected props
export const FormElement = /*Form*/ React.forwardRef<HTMLFormElement>(
({ children, ...otherProps }: PropsWithChildren<any/*FormElementProps*/>, ref) => {
const { submitForm } = useFormContext();
return (
<Form
{...otherProps}
// @ts-ignore
onSubmit={submitForm}
ref={ref}
>
{children}
</Form>
);
}
);
(
{ children, ...otherProps }: PropsWithChildren<any /*FormElementProps*/>,
ref
) => {
const { submitForm } = useFormContext();
return (
<Form {...otherProps} onSubmit={submitForm} ref={ref}>
{children}
</Form>
);
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ export const FormSubmit = ({
}) => {
const VulcanComponents = useVulcanComponents();
const { clearForm, disabled } = useFormContext();
console.log(
"VULCAN",
VulcanComponents.Button,
VulcanComponents.FormattedMessage,
VulcanComponents.Icon
);
return (
<div className="form-submit">
<VulcanComponents.Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
defaultFormComponents,
Form,
FormProps,
VulcanComponentsProvider,
} from "@vulcanjs/react-ui";
import { createModel } from "@vulcanjs/model";
import * as models from "./fixtures/models";
Expand All @@ -14,10 +15,10 @@ import {
basicFieldsSchema,
withDefaultFieldSchema,
} from "./fixtures/schemas";
import { VulcanComponentsProvider } from "@vulcanjs/react-ui";
import { action } from "@storybook/addon-actions";
import { liteCoreComponents } from "../../VulcanComponents/liteVulcanComponents/coreComponents";
import { liteFormComponents } from "../../VulcanComponents/liteVulcanComponents/formComponents";
console.log("Form comps", defaultFormComponents, liteFormComponents);

export default {
component: Form,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const PaginatedList = ({ className, options, Components }) => {
...useMultiResults,
};

// console.log(Components)

return (
<Components.PaginatedListLayout {...props}>
Expand Down
1 change: 0 additions & 1 deletion packages/react-ui/components/VulcanComponents/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Dummy } from "./Dummy";

const dummyHandler = {
get(target, property) {
//console.log("target", property);
if (property in target) {
return target[property];
}
Expand Down
150 changes: 93 additions & 57 deletions packages/react-ui/components/form/core/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export const Form = (props: FormProps) => {

const [currentValues, setCurrentValues] = useState<Object>({});

const submitFormContext = (formType: FormType) => (event /*newValues*/) => {
const submitFormContext = async (event /*newValues*/) => {
/*
TODO: previously this callback was updating the current values with new values after this call
Need to check how this worked in Vulcan initially
Expand All @@ -446,7 +446,7 @@ export const Form = (props: FormProps) => {
// TODO: previously, this was using a callback from setCurrentValues
// this needs to be rearchitectured to work without, will need some check
// https://stackoverflow.com/questions/56247433/how-to-use-setstate-callback-on-react-hooks
submitForm(formType)(event);
await submitForm(event);
};

// --------------------------------------------------------------------- //
Expand Down Expand Up @@ -659,12 +659,72 @@ export const Form = (props: FormProps) => {
//Utils.scrollIntoView(".flash-message");
};

/*
const getSubmitData = () => {
// complete the data with values from custom components
// note: it follows the same logic as SmartForm's getDocument method
let data = getData(
{ replaceIntlFields: true, addExtraFields: false, mutableFields },
props,
{
currentDocument,
deletedValues,
},
{ form: formRef.current, submitFormCallbacks }
);

// if there's a submit callback, run it
if (props.submitCallback) {
data = props.submitCallback(data) || data;
}
return data;
};
/**
Submit form handler
On success/failure, will call the relevant callbacks
*/
const submitForm = (formType: FormType) => async (event?: Event) => {
const submitFormCreate = async (event?: Event) => {
event && event.preventDefault();
event && event.stopPropagation();
const { contextName } = props;
// if form is disabled (there is already a submit handler running) don't do anything
if (disabled) {
return;
}
// clear errors and disable form while it's submitting
setErrors([]);
setDisabled(true);

const data = getSubmitData();

// create document form
try {
const result = await createDocument({
input: {
data,
contextName,
},
});
if (result.errors?.length) {
// TODO: previously got from meta, we could have more than 1 error
mutationErrorCallback(document, result.errors[0]);
} else {
newMutationSuccessCallback(result);
}
} catch (error) {
mutationErrorCallback(document, error);
}
};
/**
Submit form handler
On success/failure, will call the relevant callbacks
*/
const submitFormUpdate = async (event?: Event) => {
event && event.preventDefault();
event && event.stopPropagation();

Expand All @@ -681,59 +741,26 @@ export const Form = (props: FormProps) => {

// complete the data with values from custom components
// note: it follows the same logic as SmartForm's getDocument method
let data = getData(
{ replaceIntlFields: true, addExtraFields: false, mutableFields },
props,
{
currentDocument,
deletedValues,
},
{ form: formRef.current, submitFormCallbacks }
);

// if there's a submit callback, run it
if (props.submitCallback) {
data = props.submitCallback(data) || data;
}

if (formType === "new") {
// create document form
try {
const result = await createDocument({
input: {
data,
contextName,
},
});
if (result.errors?.length) {
// TODO: previously got from meta, we could have more than 1 error
mutationErrorCallback(document, result.errors[0]);
} else {
newMutationSuccessCallback(result);
}
} catch (error) {
mutationErrorCallback(document, error);
}
} else {
// update document form
try {
const documentId = currentDocument._id;
const result = await updateDocument({
input: {
id: documentId,
data,
contextName,
},
});
// TODO: handle more than 1 error
if (result.errors?.length) {
mutationErrorCallback(document, result.errors[0]);
} else {
editMutationSuccessCallback(result);
}
} catch (error) {
mutationErrorCallback(document, error);
const data = getSubmitData();

// update document form
try {
const documentId = currentDocument._id;
const result = await updateDocument({
input: {
id: documentId,
data,
contextName,
},
});
// TODO: handle more than 1 error
if (result.errors?.length) {
mutationErrorCallback(document, result.errors[0]);
} else {
editMutationSuccessCallback(result);
}
} catch (error) {
mutationErrorCallback(document, error);
}
};

Expand Down Expand Up @@ -781,6 +808,15 @@ export const Form = (props: FormProps) => {

const formType: "edit" | "new" = document ? "edit" : "new";

/**
Submit form handler
On success/failure, will call the relevant callbacks
*/
const submitForm = formType === "new" ? submitFormCreate : submitFormUpdate;

// Fields computation
const mutableFields =
formType === "edit"
Expand Down Expand Up @@ -812,7 +848,7 @@ export const Form = (props: FormProps) => {
clearForm,
refetchForm,
isChanged,
submitForm: submitFormContext(formType), //Change in name because we already have a function
submitForm: submitFormContext, //Change in name because we already have a function
// called submitForm, but no reason for the user to know
// about that
addToDeletedValues: addToDeletedValues,
Expand Down
11 changes: 0 additions & 11 deletions packages/react-ui/components/form/core/FormContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,6 @@ export const FormContainer = (props: FormContainerProps) => {
skip: formType === "new",
},
};
/* debug
console.log(
"MUT",
(mutationFragment as any).loc.source.body,
mutationFragmentName
);
console.log(
"QUERY",
(queryFragment as any).loc.source.body,
queryFragmentName
);*/
const { data, document, loading, refetch } = useSingle(queryOptions);
if (formType !== "new") {
debugForm(
Expand Down
2 changes: 1 addition & 1 deletion packages/react-ui/components/form/core/FormContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface FormContextValue {
// TODO: we deprecate this, it doesn't make sense to allow a child to setState this way
// setFormState: Function;
// FIXME: this type doesn't work, it doesn't necessarily have the event + it has to be defined
submitForm: Function; //React.HTMLAttributes<HTMLFormElement>["onSubmit"];
submitForm: (evt?: any) => Promise<void>; //React.HTMLAttributes<HTMLFormElement>["onSubmit"];
throwError: Function;
updateCurrentValues: Function;
disabled: boolean;
Expand Down
10 changes: 5 additions & 5 deletions packages/react-ui/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./components/core";
export * from "./components/form";
export * from "./components/VulcanComponents";
export * from "./components/VulcanCurrentUser";
export * from "./componentsHelpers";
export * from "./components/core/index.js";
export * from "./components/form/index.js";
export * from "./components/VulcanComponents/index.js";
export * from "./components/VulcanCurrentUser/index.js";
export * from "./componentsHelpers.js";
41 changes: 36 additions & 5 deletions packages/react-ui/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from "tsup";
import { defineConfig, Format } from "tsup";
import path from "path";

const commonConfig = {
Expand All @@ -8,20 +8,51 @@ const commonConfig = {
// dts: true,
sourcemap: true,
tsconfig: path.resolve(__dirname, "./tsconfig.build.json"),
outDir: "dist",
format: ["esm" as Format],
};
export default defineConfig([
/*
FIXME: this splitted config won't work with context
useVulcanComponents won't use the user-defined context, but it's own unitialized context for some reason
as if a context was bundled
Surprisingly, this won't happen with the old big bundle
{
entry: [
// TODO: get more granular for components, namely SmartForm
"./components/core/index.ts",
"./components/form/index.ts",
"./components/VulcanComponents/index.ts",
"./components/VulcanCurrentUser/index.ts",
"./componentsHelpers.tsx",
],
...commonConfig,
// For debugging, will output ESbuild metafile
// metafile: true,
esbuildOptions(options, context) {
// the directory structure will be the same as the source
options.outbase = "./";
},
},
{
entry: ["index.ts"],
...commonConfig,
esbuildOptions(options, context) {
options.outbase = "./";
},
bundle: false,
},*/
{
entry: ["index.ts"],
...commonConfig,
format: ["esm"],
outDir: "dist",
esbuildOptions(options, context) {
options.outbase = "./";
},
},
// testing utils use a separated entry point
{
entry: ["testing.ts"],
...commonConfig,
format: ["esm"],
outDir: "dist",
},
/*
There is no server/client specific code in this package yet,
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11042,7 +11042,7 @@ __metadata:
languageName: unknown
linkType: soft

"@vulcanjs/react-ui-lite@^0.7.2-alpha.5, @vulcanjs/react-ui-lite@workspace:packages/react-ui-lite":
"@vulcanjs/react-ui-lite@^0.7.2-alpha.6, @vulcanjs/react-ui-lite@workspace:packages/react-ui-lite":
version: 0.0.0-use.local
resolution: "@vulcanjs/react-ui-lite@workspace:packages/react-ui-lite"
dependencies:
Expand Down Expand Up @@ -37701,7 +37701,7 @@ __metadata:
"@vulcanjs/mongo-apollo": ^0.7.2-alpha.3
"@vulcanjs/react-hooks": ^0.7.2-alpha.3
"@vulcanjs/react-ui": ^0.7.2-alpha.5
"@vulcanjs/react-ui-lite": ^0.7.2-alpha.5
"@vulcanjs/react-ui-lite": ^0.7.2-alpha.6
"@vulcanjs/schema": ^0.7.2-alpha.3
apollo-datasource-mongodb: ^0.5.2
apollo-server: 3.9
Expand Down

2 comments on commit 94906d5

@vercel
Copy link

@vercel vercel bot commented on 94906d5 Oct 3, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

vulcan-docs – ./docusaurus

vulcan-docs-vulcan.vercel.app
vulcan-docs.vercel.app
vulcan-docs-git-main-vulcan.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 94906d5 Oct 3, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

vulcan-npm – ./

vulcan-npm-git-main-vulcan.vercel.app
vulcan-npm.vercel.app
vulcan-npm-vulcan.vercel.app

Please sign in to comment.