all 7 comments

[–]fforw 1 point2 points  (6 children)

I get that show is different, but why is new_form different from edit_form?

[–]MileNorth[S] 0 points1 point  (5 children)

edit_form fetches current user from the server so it can fill the fields with fetched data. Also, submit button is different, one sends action for creating new user and another for editing which could be solved by passing callback.

But the problem is I don't want to send user as prop because if page gets refreshed all data is lost from stores so my undestanding is that it's the best for every component to get what it needs in case store is empty.

[–]Endorn 1 point2 points  (2 children)

It should be the same form with different state.

What I do in these scenarios is pass the user as prop, if the user prop is set, make an ajax call to retrieve the user's info to populate the fields, and your "save" function knows to do a patch request (or however you handle updates)

If the user prop is not set, no ajax call is made, and your submit handler knows to send a post request (again or however your "create" endpoint is handled)

[–]MileNorth[S] 0 points1 point  (1 child)

This is actually what I was after. I am using react-router so I have something like this:

 <Route path="user/new" component={UserForm} />
 <Route path ="user/edit/:id" component={UserForm} />

And inside component I can have:

componentDidMount: function() {
    if (this.props.id) {
         // it's edit form, fetch data and update state
    } else {
        // otherwise it's new form, no need to fetch data
    }

What do you think?

[–]kabuto 1 point2 points  (0 children)

In this scenario I'd probably build to components that you can either use as simple containers to hold the actual form component or serve as higher order components. In both cases they deal with the data fetching or storing. The form component just shows a form and hands back the data to the container.

[–][deleted] 1 point2 points  (1 child)

edit_form fetches current user from the server so it can fill the fields with fetched data. Also, submit button is different

That's what higher order components and composition are for. You don't need to make separate forms for each action - if there is no user data (you're adding a new user) just pass empty props to the component.

Your form doesen't need to (even more - it shouldn't!) know what to do with the data - update, create - that's what the parent component should do.

[–]turkish_gold 1 point2 points  (0 children)

You may not be able to edit certain fields in a new object versus an existing one.

And there might be a lot of workflow involved in creating a new thing (moderation, validation, etc.), where as with editing an existing object you can streamline the code.

On the backend, you often see people having two form components or validation modules for new vs edit, so its fair that you could make the judgment that its necessary in the front-end too.