我正在构建一个事件管理系统的前端。一个特征";批准";,将创建一个新的";工作流";对象,然后打开新创建对象的编辑视图。
做一个改变对象的动作很容易(在本例中是创建新的工作流对象(,但我不清楚如何做,以便用户真正看到新创建的对象。
到目前为止,我得到的是:
const WorkflowButton = ({ data, label, ...props }) => {
const icon = props.icon;
const current_state = data && data.workflow && data.workflow && data.workflow.workflow_state
const valid_states = props.valid_states || []
const current_state_is_valid = valid_states.includes(current_state)
const payload = {
data: {
communication_id: data && data.id,
workflow_state: props.workflow_state,
data:"{}",
author:get_current_user()
}
}
const refresh_function = useRefresh()
const [mutate_the_thing, { loading }] =
useMutation({
type: 'create',
resource: 'workflows',
payload: payload,
});
const mutate_and_refresh = ()=>{
mutate_the_thing();
refresh_function();
}
const disabled = loading || (! current_state_is_valid)
return <Button label={label} onClick={mutate_and_refresh} disabled={disabled} icon={icon}/>;
};
实际上,我不想刷新当前视图,而是想跳到一个全新的视图,即与刚刚插入的对象相对应的视图。那么我该怎么做呢?
使用重定向((,文档中有一个示例:https://marmelab.com/react-admin/Actions.html#handling-其他挂钩的副作用
import * as React from "react";
import { useMutation, useNotify, useRedirect, Button } from 'react-admin';
const ApproveButton = ({ record }) => {
const notify = useNotify();
const redirect = useRedirect();
const [approve, { loading }] = useMutation(
{
type: 'update',
resource: 'comments',
payload: { id: record.id, data: { isApproved: true } },
},
{
onSuccess: ({ data }) => {
redirect('/comments');
notify('Comment approved');
},
onFailure: (error) => notify(`Comment approval error: ${error.message}`, 'warning'),
}
);
return <Button label="Approve" onClick={approve} disabled={loading} />;