Skip to content

Commit 94906d5

Browse files
committed
rolled back tsup split for react-ui and use submitForm cb correctly
1 parent b61365f commit 94906d5

File tree

11 files changed

+153
-106
lines changed

11 files changed

+153
-106
lines changed
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import React, { HTMLAttributes, PropsWithChildren } from "react";
2-
import { useFormContext } from '@vulcanjs/react-ui';
3-
import { Form } from 'react-bootstrap';
2+
import { useFormContext } from "@vulcanjs/react-ui";
3+
import { Form } from "react-bootstrap";
44

55
// TODO: not sure of the expected props
66
export const FormElement = /*Form*/ React.forwardRef<HTMLFormElement>(
7-
({ children, ...otherProps }: PropsWithChildren<any/*FormElementProps*/>, ref) => {
8-
const { submitForm } = useFormContext();
9-
return (
10-
<Form
11-
{...otherProps}
12-
// @ts-ignore
13-
onSubmit={submitForm}
14-
ref={ref}
15-
>
16-
{children}
17-
</Form>
18-
);
19-
}
20-
);
7+
(
8+
{ children, ...otherProps }: PropsWithChildren<any /*FormElementProps*/>,
9+
ref
10+
) => {
11+
const { submitForm } = useFormContext();
12+
return (
13+
<Form {...otherProps} onSubmit={submitForm} ref={ref}>
14+
{children}
15+
</Form>
16+
);
17+
}
18+
);

packages/react-ui-lite/components/form/elements/FormSubmit.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ export const FormSubmit = ({
3434
}) => {
3535
const VulcanComponents = useVulcanComponents();
3636
const { clearForm, disabled } = useFormContext();
37-
console.log(
38-
"VULCAN",
39-
VulcanComponents.Button,
40-
VulcanComponents.FormattedMessage,
41-
VulcanComponents.Icon
42-
);
4337
return (
4438
<div className="form-submit">
4539
<VulcanComponents.Button

packages/react-ui-lite/components/form/tests/Form.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
defaultFormComponents,
77
Form,
88
FormProps,
9+
VulcanComponentsProvider,
910
} from "@vulcanjs/react-ui";
1011
import { createModel } from "@vulcanjs/model";
1112
import * as models from "./fixtures/models";
@@ -14,10 +15,10 @@ import {
1415
basicFieldsSchema,
1516
withDefaultFieldSchema,
1617
} from "./fixtures/schemas";
17-
import { VulcanComponentsProvider } from "@vulcanjs/react-ui";
1818
import { action } from "@storybook/addon-actions";
1919
import { liteCoreComponents } from "../../VulcanComponents/liteVulcanComponents/coreComponents";
2020
import { liteFormComponents } from "../../VulcanComponents/liteVulcanComponents/formComponents";
21+
console.log("Form comps", defaultFormComponents, liteFormComponents);
2122

2223
export default {
2324
component: Form,

packages/react-ui/components/PaginatedList/PaginatedList.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export const PaginatedList = ({ className, options, Components }) => {
3434
...useMultiResults,
3535
};
3636

37-
// console.log(Components)
3837

3938
return (
4039
<Components.PaginatedListLayout {...props}>

packages/react-ui/components/VulcanComponents/Context.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Dummy } from "./Dummy";
44

55
const dummyHandler = {
66
get(target, property) {
7-
//console.log("target", property);
87
if (property in target) {
98
return target[property];
109
}

packages/react-ui/components/form/core/Form/Form.tsx

Lines changed: 93 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ export const Form = (props: FormProps) => {
434434

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

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

452452
// --------------------------------------------------------------------- //
@@ -659,12 +659,72 @@ export const Form = (props: FormProps) => {
659659
//Utils.scrollIntoView(".flash-message");
660660
};
661661

662-
/*
662+
const getSubmitData = () => {
663+
// complete the data with values from custom components
664+
// note: it follows the same logic as SmartForm's getDocument method
665+
let data = getData(
666+
{ replaceIntlFields: true, addExtraFields: false, mutableFields },
667+
props,
668+
{
669+
currentDocument,
670+
deletedValues,
671+
},
672+
{ form: formRef.current, submitFormCallbacks }
673+
);
674+
675+
// if there's a submit callback, run it
676+
if (props.submitCallback) {
677+
data = props.submitCallback(data) || data;
678+
}
679+
return data;
680+
};
681+
/**
663682
664683
Submit form handler
665684
685+
On success/failure, will call the relevant callbacks
686+
666687
*/
667-
const submitForm = (formType: FormType) => async (event?: Event) => {
688+
const submitFormCreate = async (event?: Event) => {
689+
event && event.preventDefault();
690+
event && event.stopPropagation();
691+
const { contextName } = props;
692+
// if form is disabled (there is already a submit handler running) don't do anything
693+
if (disabled) {
694+
return;
695+
}
696+
// clear errors and disable form while it's submitting
697+
setErrors([]);
698+
setDisabled(true);
699+
700+
const data = getSubmitData();
701+
702+
// create document form
703+
try {
704+
const result = await createDocument({
705+
input: {
706+
data,
707+
contextName,
708+
},
709+
});
710+
if (result.errors?.length) {
711+
// TODO: previously got from meta, we could have more than 1 error
712+
mutationErrorCallback(document, result.errors[0]);
713+
} else {
714+
newMutationSuccessCallback(result);
715+
}
716+
} catch (error) {
717+
mutationErrorCallback(document, error);
718+
}
719+
};
720+
/**
721+
722+
Submit form handler
723+
724+
On success/failure, will call the relevant callbacks
725+
726+
*/
727+
const submitFormUpdate = async (event?: Event) => {
668728
event && event.preventDefault();
669729
event && event.stopPropagation();
670730

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

682742
// complete the data with values from custom components
683743
// note: it follows the same logic as SmartForm's getDocument method
684-
let data = getData(
685-
{ replaceIntlFields: true, addExtraFields: false, mutableFields },
686-
props,
687-
{
688-
currentDocument,
689-
deletedValues,
690-
},
691-
{ form: formRef.current, submitFormCallbacks }
692-
);
693-
694-
// if there's a submit callback, run it
695-
if (props.submitCallback) {
696-
data = props.submitCallback(data) || data;
697-
}
698-
699-
if (formType === "new") {
700-
// create document form
701-
try {
702-
const result = await createDocument({
703-
input: {
704-
data,
705-
contextName,
706-
},
707-
});
708-
if (result.errors?.length) {
709-
// TODO: previously got from meta, we could have more than 1 error
710-
mutationErrorCallback(document, result.errors[0]);
711-
} else {
712-
newMutationSuccessCallback(result);
713-
}
714-
} catch (error) {
715-
mutationErrorCallback(document, error);
716-
}
717-
} else {
718-
// update document form
719-
try {
720-
const documentId = currentDocument._id;
721-
const result = await updateDocument({
722-
input: {
723-
id: documentId,
724-
data,
725-
contextName,
726-
},
727-
});
728-
// TODO: handle more than 1 error
729-
if (result.errors?.length) {
730-
mutationErrorCallback(document, result.errors[0]);
731-
} else {
732-
editMutationSuccessCallback(result);
733-
}
734-
} catch (error) {
735-
mutationErrorCallback(document, error);
744+
const data = getSubmitData();
745+
746+
// update document form
747+
try {
748+
const documentId = currentDocument._id;
749+
const result = await updateDocument({
750+
input: {
751+
id: documentId,
752+
data,
753+
contextName,
754+
},
755+
});
756+
// TODO: handle more than 1 error
757+
if (result.errors?.length) {
758+
mutationErrorCallback(document, result.errors[0]);
759+
} else {
760+
editMutationSuccessCallback(result);
736761
}
762+
} catch (error) {
763+
mutationErrorCallback(document, error);
737764
}
738765
};
739766

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

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

811+
/**
812+
813+
Submit form handler
814+
815+
On success/failure, will call the relevant callbacks
816+
817+
*/
818+
const submitForm = formType === "new" ? submitFormCreate : submitFormUpdate;
819+
784820
// Fields computation
785821
const mutableFields =
786822
formType === "edit"
@@ -812,7 +848,7 @@ export const Form = (props: FormProps) => {
812848
clearForm,
813849
refetchForm,
814850
isChanged,
815-
submitForm: submitFormContext(formType), //Change in name because we already have a function
851+
submitForm: submitFormContext, //Change in name because we already have a function
816852
// called submitForm, but no reason for the user to know
817853
// about that
818854
addToDeletedValues: addToDeletedValues,

packages/react-ui/components/form/core/FormContainer.tsx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,6 @@ export const FormContainer = (props: FormContainerProps) => {
270270
skip: formType === "new",
271271
},
272272
};
273-
/* debug
274-
console.log(
275-
"MUT",
276-
(mutationFragment as any).loc.source.body,
277-
mutationFragmentName
278-
);
279-
console.log(
280-
"QUERY",
281-
(queryFragment as any).loc.source.body,
282-
queryFragmentName
283-
);*/
284273
const { data, document, loading, refetch } = useSingle(queryOptions);
285274
if (formType !== "new") {
286275
debugForm(

packages/react-ui/components/form/core/FormContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface FormContextValue {
1919
// TODO: we deprecate this, it doesn't make sense to allow a child to setState this way
2020
// setFormState: Function;
2121
// FIXME: this type doesn't work, it doesn't necessarily have the event + it has to be defined
22-
submitForm: Function; //React.HTMLAttributes<HTMLFormElement>["onSubmit"];
22+
submitForm: (evt?: any) => Promise<void>; //React.HTMLAttributes<HTMLFormElement>["onSubmit"];
2323
throwError: Function;
2424
updateCurrentValues: Function;
2525
disabled: boolean;

packages/react-ui/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export * from "./components/core";
2-
export * from "./components/form";
3-
export * from "./components/VulcanComponents";
4-
export * from "./components/VulcanCurrentUser";
5-
export * from "./componentsHelpers";
1+
export * from "./components/core/index.js";
2+
export * from "./components/form/index.js";
3+
export * from "./components/VulcanComponents/index.js";
4+
export * from "./components/VulcanCurrentUser/index.js";
5+
export * from "./componentsHelpers.js";

packages/react-ui/tsup.config.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineConfig } from "tsup";
1+
import { defineConfig, Format } from "tsup";
22
import path from "path";
33

44
const commonConfig = {
@@ -8,20 +8,51 @@ const commonConfig = {
88
// dts: true,
99
sourcemap: true,
1010
tsconfig: path.resolve(__dirname, "./tsconfig.build.json"),
11+
outDir: "dist",
12+
format: ["esm" as Format],
1113
};
1214
export default defineConfig([
15+
/*
16+
FIXME: this splitted config won't work with context
17+
useVulcanComponents won't use the user-defined context, but it's own unitialized context for some reason
18+
as if a context was bundled
19+
Surprisingly, this won't happen with the old big bundle
20+
{
21+
entry: [
22+
// TODO: get more granular for components, namely SmartForm
23+
"./components/core/index.ts",
24+
"./components/form/index.ts",
25+
"./components/VulcanComponents/index.ts",
26+
"./components/VulcanCurrentUser/index.ts",
27+
"./componentsHelpers.tsx",
28+
],
29+
...commonConfig,
30+
// For debugging, will output ESbuild metafile
31+
// metafile: true,
32+
esbuildOptions(options, context) {
33+
// the directory structure will be the same as the source
34+
options.outbase = "./";
35+
},
36+
},
37+
{
38+
entry: ["index.ts"],
39+
...commonConfig,
40+
esbuildOptions(options, context) {
41+
options.outbase = "./";
42+
},
43+
bundle: false,
44+
},*/
1345
{
1446
entry: ["index.ts"],
1547
...commonConfig,
16-
format: ["esm"],
17-
outDir: "dist",
48+
esbuildOptions(options, context) {
49+
options.outbase = "./";
50+
},
1851
},
1952
// testing utils use a separated entry point
2053
{
2154
entry: ["testing.ts"],
2255
...commonConfig,
23-
format: ["esm"],
24-
outDir: "dist",
2556
},
2657
/*
2758
There is no server/client specific code in this package yet,

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11042,7 +11042,7 @@ __metadata:
1104211042
languageName: unknown
1104311043
linkType: soft
1104411044

11045-
"@vulcanjs/react-ui-lite@^0.7.2-alpha.5, @vulcanjs/react-ui-lite@workspace:packages/react-ui-lite":
11045+
"@vulcanjs/react-ui-lite@^0.7.2-alpha.6, @vulcanjs/react-ui-lite@workspace:packages/react-ui-lite":
1104611046
version: 0.0.0-use.local
1104711047
resolution: "@vulcanjs/react-ui-lite@workspace:packages/react-ui-lite"
1104811048
dependencies:
@@ -37701,7 +37701,7 @@ __metadata:
3770137701
"@vulcanjs/mongo-apollo": ^0.7.2-alpha.3
3770237702
"@vulcanjs/react-hooks": ^0.7.2-alpha.3
3770337703
"@vulcanjs/react-ui": ^0.7.2-alpha.5
37704-
"@vulcanjs/react-ui-lite": ^0.7.2-alpha.5
37704+
"@vulcanjs/react-ui-lite": ^0.7.2-alpha.6
3770537705
"@vulcanjs/schema": ^0.7.2-alpha.3
3770637706
apollo-datasource-mongodb: ^0.5.2
3770737707
apollo-server: 3.9

0 commit comments

Comments
 (0)