Search code examples
reactjsadmin-on-restapi-platform.comreact-admin

Using react-admin , How to create a comment from a post


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.


Solution

  • 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