web: refactor change page to use a reducer
This change updates the change page component to dispatch reducer action instead of direct axios call. This enables using the generic error reducers. Change-Id: If5fedff89e55eddfd08ea224c0124f3b2f9255c4
This commit is contained in:
parent
f61e19900f
commit
e5da1e1149
|
@ -0,0 +1,61 @@
|
||||||
|
/* global Promise */
|
||||||
|
// Copyright 2018 Red Hat, Inc
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
// not use this file except in compliance with the License. You may obtain
|
||||||
|
// a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
// License for the specific language governing permissions and limitations
|
||||||
|
// under the License.
|
||||||
|
|
||||||
|
import * as API from '../api'
|
||||||
|
|
||||||
|
export const CHANGE_FETCH_REQUEST = 'CHANGE_FETCH_REQUEST'
|
||||||
|
export const CHANGE_FETCH_SUCCESS = 'CHANGE_FETCH_SUCCESS'
|
||||||
|
export const CHANGE_FETCH_FAIL = 'CHANGE_FETCH_FAIL'
|
||||||
|
|
||||||
|
export const requestChange = () => ({
|
||||||
|
type: CHANGE_FETCH_REQUEST
|
||||||
|
})
|
||||||
|
|
||||||
|
export const receiveChange = json => ({
|
||||||
|
type: CHANGE_FETCH_SUCCESS,
|
||||||
|
change: json,
|
||||||
|
receivedAt: Date.now()
|
||||||
|
})
|
||||||
|
|
||||||
|
const failedChange = error => ({
|
||||||
|
type: CHANGE_FETCH_FAIL,
|
||||||
|
error
|
||||||
|
})
|
||||||
|
|
||||||
|
const fetchChange = (tenant, changeId) => dispatch => {
|
||||||
|
dispatch(requestChange())
|
||||||
|
return API.fetchChangeStatus(tenant.apiPrefix, changeId)
|
||||||
|
.then(response => dispatch(receiveChange(response.data)))
|
||||||
|
.catch(error => dispatch(failedChange(error)))
|
||||||
|
}
|
||||||
|
|
||||||
|
const shouldFetchChange = state => {
|
||||||
|
const change = state.change
|
||||||
|
if (!change) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (change.isFetching) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
export const fetchChangeIfNeeded = (tenant, change, force) => (
|
||||||
|
dispatch, getState) => {
|
||||||
|
if (force || shouldFetchChange(getState())) {
|
||||||
|
return dispatch(fetchChange(tenant, change))
|
||||||
|
}
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
|
@ -16,53 +16,34 @@
|
||||||
import * as React from 'react'
|
import * as React from 'react'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import { connect } from 'react-redux'
|
import { connect } from 'react-redux'
|
||||||
import {
|
|
||||||
Alert,
|
|
||||||
} from 'patternfly-react'
|
|
||||||
|
|
||||||
import { fetchChangeStatus } from '../api'
|
import { fetchChangeIfNeeded } from '../actions/change'
|
||||||
import ChangePanel from '../containers/status/ChangePanel'
|
import ChangePanel from '../containers/status/ChangePanel'
|
||||||
|
import Refreshable from '../containers/Refreshable'
|
||||||
|
|
||||||
|
|
||||||
class ChangeStatusPage extends React.Component {
|
class ChangeStatusPage extends Refreshable {
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
match: PropTypes.object.isRequired,
|
match: PropTypes.object.isRequired,
|
||||||
tenant: PropTypes.object
|
tenant: PropTypes.object,
|
||||||
|
remoteData: PropTypes.object,
|
||||||
|
dispatch: PropTypes.func
|
||||||
}
|
}
|
||||||
|
|
||||||
state = {
|
updateData = (force) => {
|
||||||
change: null,
|
this.props.dispatch(fetchChangeIfNeeded(
|
||||||
error: null,
|
this.props.tenant, this.props.match.params.changeId, force))
|
||||||
}
|
.then(() => {this.timer = setTimeout(this.updateData, 5000)})
|
||||||
|
|
||||||
updateData = () => {
|
|
||||||
// Clear any running timer
|
// Clear any running timer
|
||||||
if (this.timer) {
|
if (this.timer) {
|
||||||
clearTimeout(this.timer)
|
clearTimeout(this.timer)
|
||||||
this.timer = null
|
this.timer = null
|
||||||
}
|
}
|
||||||
this.setState({error: null})
|
|
||||||
fetchChangeStatus(
|
|
||||||
this.props.tenant.apiPrefix, this.props.match.params.changeId)
|
|
||||||
.then(response => {
|
|
||||||
this.setState({change: response.data})
|
|
||||||
}).catch(error => {
|
|
||||||
this.setState({error: error.message, change: null})
|
|
||||||
})
|
|
||||||
this.timer = setTimeout(this.updateData, 5000)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount () {
|
componentDidMount () {
|
||||||
document.title = this.props.match.params.changeId + ' | Zuul Status'
|
document.title = this.props.match.params.changeId + ' | Zuul Status'
|
||||||
if (this.props.tenant.name) {
|
super.componentDidMount()
|
||||||
this.updateData()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidUpdate (prevProps) {
|
|
||||||
if (this.props.tenant.name !== prevProps.tenant.name) {
|
|
||||||
this.updateData()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount () {
|
componentWillUnmount () {
|
||||||
|
@ -73,12 +54,13 @@ class ChangeStatusPage extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
const { error, change } = this.state
|
const { remoteData } = this.props
|
||||||
if (error) {
|
const change = remoteData.change
|
||||||
return (<Alert>{this.state.error}</Alert>)
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
|
<div style={{float: 'right'}}>
|
||||||
|
{this.renderSpinner()}
|
||||||
|
</div><br />
|
||||||
{change && change.map((item, idx) => (
|
{change && change.map((item, idx) => (
|
||||||
<div className='row' key={idx}>
|
<div className='row' key={idx}>
|
||||||
<ChangePanel
|
<ChangePanel
|
||||||
|
@ -91,4 +73,7 @@ class ChangeStatusPage extends React.Component {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default connect(state => ({tenant: state.tenant}))(ChangeStatusPage)
|
export default connect(state => ({
|
||||||
|
tenant: state.tenant,
|
||||||
|
remoteData: state.change
|
||||||
|
}))(ChangeStatusPage)
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
// Copyright 2018 Red Hat, Inc
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
// not use this file except in compliance with the License. You may obtain
|
||||||
|
// a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
// License for the specific language governing permissions and limitations
|
||||||
|
// under the License.
|
||||||
|
|
||||||
|
import {
|
||||||
|
CHANGE_FETCH_FAIL,
|
||||||
|
CHANGE_FETCH_REQUEST,
|
||||||
|
CHANGE_FETCH_SUCCESS
|
||||||
|
} from '../actions/change'
|
||||||
|
|
||||||
|
export default (state = {
|
||||||
|
isFetching: false,
|
||||||
|
change: null
|
||||||
|
}, action) => {
|
||||||
|
switch (action.type) {
|
||||||
|
case CHANGE_FETCH_REQUEST:
|
||||||
|
return {
|
||||||
|
isFetching: true,
|
||||||
|
change: state.change
|
||||||
|
}
|
||||||
|
case CHANGE_FETCH_SUCCESS:
|
||||||
|
return {
|
||||||
|
isFetching: false,
|
||||||
|
change: action.change,
|
||||||
|
}
|
||||||
|
case CHANGE_FETCH_FAIL:
|
||||||
|
return {
|
||||||
|
isFetching: false,
|
||||||
|
change: state.change,
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return state
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,6 +15,7 @@
|
||||||
import { combineReducers } from 'redux'
|
import { combineReducers } from 'redux'
|
||||||
|
|
||||||
import configErrors from './configErrors'
|
import configErrors from './configErrors'
|
||||||
|
import change from './change'
|
||||||
import errors from './errors'
|
import errors from './errors'
|
||||||
import info from './info'
|
import info from './info'
|
||||||
import job from './job'
|
import job from './job'
|
||||||
|
@ -24,6 +25,7 @@ import tenant from './tenant'
|
||||||
import tenants from './tenants'
|
import tenants from './tenants'
|
||||||
|
|
||||||
const reducers = {
|
const reducers = {
|
||||||
|
change,
|
||||||
info,
|
info,
|
||||||
job,
|
job,
|
||||||
jobs,
|
jobs,
|
||||||
|
|
Loading…
Reference in New Issue