Merge "web: refactor change page to use a reducer"
This commit is contained in:
commit
566bc2173f
|
@ -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 PropTypes from 'prop-types'
|
||||
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 Refreshable from '../containers/Refreshable'
|
||||
|
||||
|
||||
class ChangeStatusPage extends React.Component {
|
||||
class ChangeStatusPage extends Refreshable {
|
||||
static propTypes = {
|
||||
match: PropTypes.object.isRequired,
|
||||
tenant: PropTypes.object
|
||||
tenant: PropTypes.object,
|
||||
remoteData: PropTypes.object,
|
||||
dispatch: PropTypes.func
|
||||
}
|
||||
|
||||
state = {
|
||||
change: null,
|
||||
error: null,
|
||||
}
|
||||
|
||||
updateData = () => {
|
||||
updateData = (force) => {
|
||||
this.props.dispatch(fetchChangeIfNeeded(
|
||||
this.props.tenant, this.props.match.params.changeId, force))
|
||||
.then(() => {this.timer = setTimeout(this.updateData, 5000)})
|
||||
// Clear any running timer
|
||||
if (this.timer) {
|
||||
clearTimeout(this.timer)
|
||||
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 () {
|
||||
document.title = this.props.match.params.changeId + ' | Zuul Status'
|
||||
if (this.props.tenant.name) {
|
||||
this.updateData()
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate (prevProps) {
|
||||
if (this.props.tenant.name !== prevProps.tenant.name) {
|
||||
this.updateData()
|
||||
}
|
||||
super.componentDidMount()
|
||||
}
|
||||
|
||||
componentWillUnmount () {
|
||||
|
@ -73,12 +54,13 @@ class ChangeStatusPage extends React.Component {
|
|||
}
|
||||
|
||||
render () {
|
||||
const { error, change } = this.state
|
||||
if (error) {
|
||||
return (<Alert>{this.state.error}</Alert>)
|
||||
}
|
||||
const { remoteData } = this.props
|
||||
const change = remoteData.change
|
||||
return (
|
||||
<React.Fragment>
|
||||
<div style={{float: 'right'}}>
|
||||
{this.renderSpinner()}
|
||||
</div><br />
|
||||
{change && change.map((item, idx) => (
|
||||
<div className='row' key={idx}>
|
||||
<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 configErrors from './configErrors'
|
||||
import change from './change'
|
||||
import errors from './errors'
|
||||
import info from './info'
|
||||
import job from './job'
|
||||
|
@ -24,6 +25,7 @@ import tenant from './tenant'
|
|||
import tenants from './tenants'
|
||||
|
||||
const reducers = {
|
||||
change,
|
||||
info,
|
||||
job,
|
||||
jobs,
|
||||
|
|
Loading…
Reference in New Issue