如何自定义react-admin的Edit SaveButton,以便添加自定义通知之类的内容

时间:2019-09-13 18:40:15

标签: react-admin

我已经阅读了react-admin文档,特别是https://marmelab.com/react-admin/Actions.html#using-the-withdataprovider-decorator,但是看不到如何从表单中获取数据来填充操作。在下面的代码中,记录永远不会是表单中的最新数据。

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
// import Button from '@material-ui/core/Button';
import { showNotification, Button } from 'react-admin';
import { push } from 'react-router-redux';

class ApproveButton extends Component {
    handleClick = () => {
        const { push, record, showNotification } = this.props;
        const updatedRecord = { ...record, is_approved: true };

        console.log("ApproveButton record", record)
        fetch(`/comments/${record.id}`, { method: 'PUT', body: updatedRecord })
            .then(() => {
                showNotification('Comment approved');
                push('/comments');
            })
            .catch((e) => {
                showNotification('Error: comment not approved', 'warning')
            });
    }

    render() {
        return <Button label="Approve" onClick={this.handleClick} />;
    }
}

ApproveButton.propTypes = {
    push: PropTypes.func,
    record: PropTypes.object,
    showNotification: PropTypes.func,
};

export default connect(null, {
    showNotification,
    push,
})(ApproveButton);

0 个答案:

没有答案