2017-04-04 100 views
1

我想在自定義操作成功執行後獲取要刷新的列表。如何在管理員休息時刷新列表視圖

我使用的傳奇從管理上休息教程

function * actionApproveSuccess() { 
    yield put(showNotification('Executed')) 
    yield put(push('/comments')) 
    // does not refresh, because the route does not change 
    // react-redux-router also has no refresh() method, like react-router has... 
} 

其他想法,我不得不爲以某種方式觸發列表組件的刷新動作,但我不知道如何訪問或如何掛鉤到ACTION_SUCCESS事件。

回答

1

無法通過反應路由器刷新路由,這是一個known problem。組件的List組件有its own refresh mechanism,但不提供API。

我的建議是使用基於admin-on-rest的自定義<List>組件。如果您找到了公開refresh操作的方法,請隨時在aor存儲庫上打開PR!

0

肯定哈克,但一個變通辦法可以是:

push('/comments/1') //any path to change the current route 
push('/comments') //the path to refresh, which is now a new route 
1

我已經解決了通過操作面板的小黑客此任務。我敢肯定,這是不正確的解決辦法,但在某些情況下,它可以幫助:

class RefreshButton extends FlatButton { 
 
    componentDidMount() { 
 
    if (this.props.refreshInterval) { 
 
     this.interval = setInterval(() => { 
 
     this.props.refresh(new Event('refresh')) 
 
     }, this.props.refreshInterval) 
 
    } 
 
    } 
 

 
    componentWillUnmount() { 
 
    clearInterval(this.interval) 
 
    } 
 
} 
 

 
const StreamActions = ({ resource, filters, displayedFilters, filterValues, basePath, showFilter, refresh }) => (
 
    <CardActions> 
 
    {filters && React.cloneElement(filters, { resource, showFilter, displayedFilters, filterValues, context: 'button' }) } 
 
    <RefreshButton primary label="Refresh streams" onClick={refresh} refreshInterval={15000} refresh={refresh} icon={<NavigationRefresh />} /> 
 
    </CardActions> 
 
); 
 

 
export default class StreamsListPage extends Component { 
 
    render() { 
 
    return (
 
     <List 
 
     {...this.props} 
 
     perPage={20} 
 
     actions={<StreamActions />} 
 
     filter={{ active: true }} 
 
     title='Active Streams'> 
 
     <StreamsList /> 
 
     </List> 
 
    ) 
 
    } 
 
}

+0

剛剛嘗試過,它顯示一個錯誤,並且出現以下提示:「棄用警告:刷新List視圖的首選方法是將自定義按鈕與redux連接起來,並分派'refreshView'動作。 –

2

@Danila橫溢的回答上面顯示這個消息時,我現在使用它:

棄用警告:刷新List視圖的首選方法是將自定義按鈕與redux連接起來,並分派refreshView操作。

單擊刷新按鈕本身現在不工作。

這是我在我的工作過的調整版本。

編輯:修改它多一點,使其可重複使用。


RefreshListActions.js

import React, { Component } from 'react' 
import FlatButton from 'material-ui/FlatButton' 
import { CardActions } from 'material-ui/Card' 
import NavigationRefresh from 'material-ui/svg-icons/navigation/refresh' 
import { connect } from 'react-redux' 
import { REFRESH_VIEW } from 'admin-on-rest/src/actions/uiActions' 
import { refreshView as refreshViewAction } from 'admin-on-rest/src/actions/uiActions' 

class MyRefresh extends Component { 
    componentDidMount() { 
     const { refreshInterval, refreshView } = this.props 
     if (refreshInterval) { 
      this.interval = setInterval(() => { 
       refreshView() 
      }, refreshInterval) 
     } 
    } 

    componentWillUnmount() { 
     clearInterval(this.interval) 
    } 

    render() { 
     const { label, refreshView, icon } = this.props; 
     return (
      <FlatButton 
       primary 
       label={label} 
       onClick={refreshView} 
       icon={icon} 
      /> 
     ); 
    } 
} 

const RefreshButton = connect(null, { refreshView: refreshViewAction })(MyRefresh) 

const RefreshListActions = ({ resource, filters, displayedFilters, filterValues, basePath, showFilter, refreshInterval }) => (
    <CardActions> 
     {filters && React.cloneElement(filters, { resource, showFilter, displayedFilters, filterValues, context: 'button' }) } 
     <RefreshButton primary label="Refresh" refreshInterval={refreshInterval} icon={<NavigationRefresh />} /> 
    </CardActions> 
); 

export default RefreshListActions 

在我的名單,我要經常刷新:

import RefreshListActions from './RefreshListActions' 

export default (props) => (
    <List {...props} 
     actions={<RefreshListActions refreshInterval="10000" />} 
     > 
     <Datagrid> 
      ... 
     </Datagrid> 
    </List> 
) 
+0

這是行得通的。最好是創建一個具有自動更新間隔的下拉菜單的按鈕,比如disable,10,50,100等等。我看到有人在其他帖子中有類似的想法,https://stackoverflow.com/questions/44551957/IS-有-A-方式對模擬壓-的刷新按鈕到刷新一個列表。但沒有運氣讓它工作 –

0

通過終極版使用refreshView行動效果很好。

見例如....

import { refreshView as refreshViewAction } from 'admin-on-rest'; 
import { connect } from 'react-redux'; 

class MyReactComponent extends Component { 
    //... etc etc standard react stuff... 

doSomething() { 
    // etc etc do smt then trigger refreshView like below 
    this.props.refreshView(); 
} 
render() { 
    return <div>etc etc your stuff</div> 
} 
} 

export default connect(undefined, { refreshView: refreshViewAction })(
    MyReactComponent 
); 
0

push僅僅是AOR重定向這似乎並不爲我工作的。什麼guleryuz張貼在我的正確軌道上..這是我沒有建立在他的例子:

// Import Statement 
import { refreshView as refreshViewAction } from 'admin-on-rest'; 

class RemoveButton extends Component { 
handleClick =() => { 
    const { refreshView, record, showNotification } = this.props; 
    fetch(`http://localhost:33333/api/v1/batch/stage/${record.id}`, { method: 'DELETE' }) 
     .then(() => { 
      showNotification('Removed domain from current stage'); 
      refreshView(); 
     }) 
     .catch((e) => { 
      console.error(e); 
      showNotification('Error: could not find domain'); 
     }); 
} 

render() { 
    return <FlatButton secondary label="Delete" icon={<DeleteIcon />}onClick={this.handleClick} />; 
} 
} 

這些位也很重要:

RemoveButton.propTypes = { 
record: PropTypes.object, 
showNotification: PropTypes.func, 
refreshView: PropTypes.func, 
}; 

export default connect(null, { 
showNotification: showNotificationAction, 
refreshView: refreshViewAction, 
})(RemoveButton); 

所以這個工作的方式是它採用AOR的refreshViewAction爲道具的功能。這使用底層調用爲我填充數據網格,它是GET_LIST。這可能不適用於您的具體使用情況。如果您有任何問題,請告訴我。