1

I am using react-admin for a new project . One of the challenge that I have right is to create something like a comment from a post . Here is the way I try to do it <CreateButton basePath='/prescriptions' label="prescriptions" record={data}/>. The problem That I am facing right is to use the data record in the post form to create the comment , that means I want to send the post_id with the others data from the commentForm . Thanks in advance for helping me.

thecassion
  • 506
  • 1
  • 7
  • 19
  • Possible duplicate of [How can I add a (seeding the foreign key) to a ReferenceManyField in admin-on-rest](https://stackoverflow.com/questions/45669460/how-can-i-add-a-createbutton-seeding-the-foreign-key-to-a-referencemanyfield) – Christiaan Westerbeek Jun 26 '18 at 06:53
  • @ChristiaanWesterbeek how did you handle that? I can read in the SimpleForm the data that I pass in the CreateButton. – thecassion Jun 26 '18 at 10:10
  • I'm still in the process of implementing it... Don't bother commenting on my planning or asking me for any promise that I will post an answer – Christiaan Westerbeek Jun 26 '18 at 10:37
  • I'm working on blog post about this. Stay tuned :) – Gildas Garcia Jun 27 '18 at 17:35

1 Answers1

3

Well, the post I mentioned in this comment will be published soon. Besides, we're going to support this by default in 2.2.0. In the mean time, here's what you can do:

import { parse } from "query-string";

const CommentCreate = props => {
    // Read the post_id from the location which is injected by React Router and passed to our component by react-admin automatically
    const { post_id: post_id_string } = parse(props.location.search);

    // Optional if you're not using integers as ids
    const post_id = post_id_string ? parseInt(post_id_string, 10) : '';

    return (
        <Create {...props}>
            <SimpleForm
                defaultValue={{ created_at: today, post_id }}
            >
                // ...
            </SimpleForm>
        </Create>
    );
}

Here's the button to create a new comment from an existing post:

import CardActions from '@material-ui/core/CardActions';
import ChatBubbleIcon from "@material-ui/icons/ChatBubble";
import { Button } from 'react-admin';

const AddNewCommentButton = ({ record }) => (
    <Button
        component={Link}
        to={{
            pathname: '/comments/create',
            search: `?post_id=${record.id}`
        }}
        label="Add a comment"
    >
        <ChatBubbleIcon />
    </Button>
);

And finally, how we use it alongside the ReferenceManyField in the post Show component (would work in Edit too):

const PostShow = props => (
    <Show {...props}>
        <TabbedShowLayout>
        ...
            <Tab label="Comments">
                <ReferenceManyField
                    addLabel={false}
                    reference="comments"
                    target="post_id"
                    sort={{ field: "created_at", order: "DESC" }}
                >
                    <Datagrid>
                        <DateField source="created_at" />
                        <TextField source="body" />
                        <EditButton />
                    </Datagrid>
                </ReferenceManyField>
                <AddNewCommentButton />
            </Tab>
        </TabbedShowLayout>
    </Show>
);

You can see this in action in this codesandbox

Edit: post published https://marmelab.com/blog/2018/07/09/react-admin-tutorials-form-for-related-records.html

Gildas Garcia
  • 6,966
  • 3
  • 15
  • 29
  • Great stuff Gildas! Will the solution you provided here need to change once this feature is supported by default in 2.2? – Shawn K Jul 06 '18 at 17:10
  • Not all of it and it also depends on your needs. Create will be able to initialize its values from either the location state or search. However, we won't support default integer parsing of the id for instance, so this solution will still be relevant for some cases. – Gildas Garcia Jul 07 '18 at 05:53
  • Post published, I added the link in my answer – Gildas Garcia Jul 09 '18 at 08:54