react-admin按自定义表单刷新dataGrid

问题描述 投票:0回答:1

首先,我想说我是初学者的反应(我讨厌前面的发展,但是,你知道,有时我们不会选择工作的生活)....

因此,我使用react-admin创建一个自定义表单,而不使用react-admin的REST连接(这是一个特定的表单)。在表单验证之后,一个名为processingStatut的值会改变几个数据,并且需要在react-admin映射的<List><Datagrid>中显示这个新值。

所以我按照文档创建一个reducer动作,在我的dataGrid中更改一个名为processingStatut的布尔值,如下所示:

epassesReceived.js

export const EPASSES_RECEIVED = 'EPASSES_RECEIVED';
export const epassesReceived = (data) => ({
   type: EPASSES_RECEIVED,
   payload: { data },
});

我的customForm.js

import { epassesReceived as epassesReceivedAction } from './epassesReceived';
handleSubmit(event) {
    this.setState({
        post: this.post
    });
    const { fetchJson } = fetchUtils;
    const {
        showNotification,
        history,
        push,
        epassesReceived,
        fetchStart, fetchEnd
    } = this.props;
    const url = `${API_URL}/ePasses/update`;
    const datas = JSON.stringify(this.state);
    const options = {
        method: 'POST',
        body: datas
    };
    fetchStart();
    fetchJson(url, options)
        .then( response => epassesReceived(response.json) )
        .then(() => {
            showNotification('ra.notification.epasseRecorded');
            history.goBack();
        })
        .catch( error => {
            console.error(error);
            var message = error.message.replace(/ /g, '');
            showNotification(`ra.notification.${message}`, 'warning');
        })
        .finally(fetchEnd);
        event.preventDefault();
}

...

const mapStateToProps = state => ({
    customReducer: state.customReducer
});

export const EpassesUpdate = connect(mapStateToProps, {
    epassesReceived: epassesReceivedAction, 
    showNotification, 
    push,fetchStart, fetchEnd
})(translate(withStyles(formStyle)(EpassesUpdateView)));

在我的app.js

import { EPASSES_RECEIVED } from './epassesReceived';
const customReducer = (previousState = 0, { type, payload }) => {
    console.log(payload, type);
    if (type == EPASSES_RECEIVED) {
        // console.log('modif');
        // payload.data[0].processingStatut=1; this is the purpose of the script. To show de modification changed after form's validation
        return payload;
    }

    return previousState;
}

和viewDataGrid.js

<List 
        classes={props.classes} 
        {...props} 
        exporter={exporter} 
        title='ePass.pageTitle' 
        perPage={15} 
        pagination={<PostPagination />} 
        filters={<EPassFilter businessunit={businessUnit} />} 
        bulkActions={<EPassBulkActions businessunit={businessUnit} />} 
        actions={<PostActions businessUnit={businessUnit} />}
    >
        <Datagrid classes={props.classes}>
            { businessUnit === undefined || !businessUnit.companyName &&
                <TextField source="businessUnitName" label="ePass.businessUnitName" />
            }
            <StateField source="processingStatut" label="" translate={props.translate} />
.....

但是在我的控制台日志中我的值没有改变,我现在不知道为什么......当然,如果我用F5刷新我的网页,它是有效的,因为我的数据库中的值已经改变了。但是没有在react的dataGrid中......我迷失了......

也许日志输出可能会有所帮助:

log_output

我们可以看到类型“EPASSES_RECEIVED”并且数据已更改

javascript reactjs ecmascript-6 react-admin
1个回答
0
投票

我认为你的问题来自你的获取。试试这个 :

fetch(url, options)
        .then( response => response.json() )
        .then(data => {
            epassesReceived(data);
            showNotification('ra.notification.epasseRecorded');
            history.goBack();
        })
        .catch( error => {
            console.error(error);
            var message = error.message.replace(/ /g, '');
            showNotification(`ra.notification.${message}`, 'warning');
        })
        .finally(fetchEnd);
© www.soinside.com 2019 - 2024. All rights reserved.