Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use setInitialState for async initialization #84

Merged
merged 2 commits into from
Jul 12, 2019

Conversation

amythos
Copy link
Contributor

@amythos amythos commented Jun 27, 2019

usage


module StateLenses = [%lenses
  type state = {
    description: string,
    title: string,
    acceptTerms: bool,
  }
];
module PostAddForm = ReFormNext.Make(StateLenses);

[@react.component]
let make = () => {
  let {state, submit, getFieldState, handleChange, setValues}: PostAddForm.api =
    PostAddForm.use(
      ~schema={
        PostAddForm.Validation.Schema([|
          Custom(
            Title,
            values =>
              Js.String.length(values.title) > 20
                ? Error("Keep it short!") : Valid,
          ),
          Custom(
            Description,
            values =>
              values.description == "" ? Error("Can't be empty") : Valid,
          ),
          Custom(
            AcceptTerms,
            values =>
              values.acceptTerms == false
                ? Error("You must accept all the terms") : Valid,
          ),
        |]);
      },
      ~onSubmit=
        ({state}) => {
          mutate(
            ~variables=
              PostAddMutationConfig.make(
                ~title=state.values.title,
                ~description=state.values.description,
                (),
              )##variables,
            (),
          )
          |> Js.Promise.then_(result =>
               setResult(_ => Some(result)) |> Js.Promise.resolve
             )
          |> ignore;

          None;
        },
      ~initialState={title: "", description: "", acceptTerms: false},
      (),
    );
  
  React.useEffect0(() => {
      fetchDefaultState(fetchUrl, state => setValues(state));
  });
};

@fakenickels
Copy link
Member

Thanks, this makes a lot of sense!

@@ -127,6 +147,7 @@ module Make = (Config: Config) => {
arrayRemoveBy: 'a. (Config.field(array('a)), 'a => bool) => unit,
submit: unit => unit,
resetForm: unit => unit,
setInitialState: Config.state => unit,
Copy link
Member

Choose a reason for hiding this comment

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

I think we can actually create a helper called setValues

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think that setValues means set all values passed in, and setInitialState means won't set the value when its fieldState is not Pristine, maybe we can call it setinitialValues

Copy link
Member

Choose a reason for hiding this comment

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

that makes sense, I'll take a better look soon so we can get it merged

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hello @fakenickels would you have time to merge this

Copy link
Member

Choose a reason for hiding this comment

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

Sorry about the delay, the past few days have we have been a bit busy. I'll gonna finish the review today and give you a better feedback soon

@@ -144,6 +165,7 @@ module Make = (Config: Config) => {
let (state, send) =
ReactUpdate.useReducer(
{
initValues: initialState,
Copy link
Member

Choose a reason for hiding this comment

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

With setValues I don't think this is necessary

Copy link
Member

Choose a reason for hiding this comment

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

hmm looking the ResetForm branch changes I got it now

@fakenickels
Copy link
Member

@amythos This looks good, but for the next releases we are going to try to make ReForm as similar as possible to Formik to ease adoption. So in Formik there is no setInitialValues, we just have a setValues. So before merging this PR we would like to have the handlers also named setValues and no initValues on the state, if the user needs to resetTheForm and apply the initialState the user will just call reset and call setValues as well.

@@ -86,6 +86,24 @@ module Make = (Config: Config) => {
->Belt.Array.get(0);
};

let setInitialValues =
Copy link
Member

Choose a reason for hiding this comment

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

let's call this setValues now

@amythos
Copy link
Contributor Author

amythos commented Jul 11, 2019

@fakenickels now we have setValues for set the whole state, and setFieldValue for the single field

@fakenickels
Copy link
Member

Thank you for your time and effort @amythos , we'll be releasing this to NPM soon

@fakenickels fakenickels merged commit d6db0f8 into rescriptbr:master Jul 12, 2019
@fakenickels
Copy link
Member

@all-contributors please add @amythos for code

@allcontributors
Copy link
Contributor

@fakenickels

I've put up a pull request to add @amythos! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants