From 3014c0029d0cd991fd03c973669aeb8b49f9b19f Mon Sep 17 00:00:00 2001 From: Christopher Kolstad Date: Thu, 21 Jan 2021 15:27:24 +0100 Subject: [PATCH] Basic implementation of deprecation/reactivation of strategies --- .../feature/strategy/strategies-add.jsx | 12 ++++---- .../list-component-test.jsx.snap | 16 ++++++++++ .../strategy-details-component-test.jsx.snap | 1 + .../__tests__/list-component-test.jsx | 4 +++ .../component/strategies/list-component.jsx | 19 ++++++++++-- .../component/strategies/list-container.jsx | 14 ++++++++- .../strategies/show-strategy-component.js | 7 +++++ frontend/src/store/strategy/actions.js | 29 +++++++++++++++++++ frontend/src/store/strategy/api.js | 18 ++++++++++++ frontend/src/store/strategy/index.js | 26 ++++++++++++++++- 10 files changed, 136 insertions(+), 10 deletions(-) diff --git a/frontend/src/component/feature/strategy/strategies-add.jsx b/frontend/src/component/feature/strategy/strategies-add.jsx index fa3a028eff..30edc9e967 100644 --- a/frontend/src/component/feature/strategy/strategies-add.jsx +++ b/frontend/src/component/feature/strategy/strategies-add.jsx @@ -61,11 +61,13 @@ class AddStrategy extends React.Component { /> Add Strategy: - {this.props.strategies.map(s => ( - this.addStrategy(s.name)}> - {s.name} - - ))} + {this.props.strategies + .filter(s => !s.deprecated) + .map(s => ( + this.addStrategy(s.name)}> + {s.name} + + ))} ); diff --git a/frontend/src/component/strategies/__tests__/__snapshots__/list-component-test.jsx.snap b/frontend/src/component/strategies/__tests__/__snapshots__/list-component-test.jsx.snap index bf855a0b17..0f0e06bf6d 100644 --- a/frontend/src/component/strategies/__tests__/__snapshots__/list-component-test.jsx.snap +++ b/frontend/src/component/strategies/__tests__/__snapshots__/list-component-test.jsx.snap @@ -67,9 +67,17 @@ exports[`renders correctly with one strategy 1`] = ` > Another + + + + Deprecate + Another + + + + Deprecate + diff --git a/frontend/src/component/strategies/__tests__/__snapshots__/strategy-details-component-test.jsx.snap b/frontend/src/component/strategies/__tests__/__snapshots__/strategy-details-component-test.jsx.snap index 6949b2efc8..a7fee260d9 100644 --- a/frontend/src/component/strategies/__tests__/__snapshots__/strategy-details-component-test.jsx.snap +++ b/frontend/src/component/strategies/__tests__/__snapshots__/strategy-details-component-test.jsx.snap @@ -58,6 +58,7 @@ exports[`renders correctly with one strategy 1`] = ` >
+ diff --git a/frontend/src/component/strategies/__tests__/list-component-test.jsx b/frontend/src/component/strategies/__tests__/list-component-test.jsx index f2a2295fdc..aefbabd742 100644 --- a/frontend/src/component/strategies/__tests__/list-component-test.jsx +++ b/frontend/src/component/strategies/__tests__/list-component-test.jsx @@ -18,6 +18,8 @@ test('renders correctly with one strategy', () => { strategies={[strategy]} fetchStrategies={jest.fn()} removeStrategy={jest.fn()} + deprecateStrategy={jest.fn()} + reactivateStrategy={jest.fn()} history={{}} hasPermission={permission => [CREATE_STRATEGY, DELETE_STRATEGY].indexOf(permission) !== -1} /> @@ -38,6 +40,8 @@ test('renders correctly with one strategy without permissions', () => { strategies={[strategy]} fetchStrategies={jest.fn()} removeStrategy={jest.fn()} + deprecateStrategy={jest.fn()} + reactivateStrategy={jest.fn()} history={{}} hasPermission={() => false} /> diff --git a/frontend/src/component/strategies/list-component.jsx b/frontend/src/component/strategies/list-component.jsx index 48f0fb17db..aaf2e0d09f 100644 --- a/frontend/src/component/strategies/list-component.jsx +++ b/frontend/src/component/strategies/list-component.jsx @@ -2,7 +2,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { Link } from 'react-router-dom'; -import { List, ListItem, ListItemContent, IconButton, Card } from 'react-mdl'; +import { List, ListItem, ListItemContent, IconButton, Card, Button } from 'react-mdl'; import { HeaderTitle, styles as commonStyles } from '../common'; import { CREATE_STRATEGY, DELETE_STRATEGY } from '../../permissions'; @@ -11,6 +11,8 @@ class StrategiesListComponent extends Component { strategies: PropTypes.array.isRequired, fetchStrategies: PropTypes.func.isRequired, removeStrategy: PropTypes.func.isRequired, + deprecateStrategy: PropTypes.func.isRequired, + reactivateStrategy: PropTypes.func.isRequired, history: PropTypes.object.isRequired, hasPermission: PropTypes.func.isRequired, }; @@ -20,7 +22,7 @@ class StrategiesListComponent extends Component { } render() { - const { strategies, removeStrategy, hasPermission } = this.props; + const { strategies, removeStrategy, hasPermission, reactivateStrategy, deprecateStrategy } = this.props; return ( @@ -45,9 +47,20 @@ class StrategiesListComponent extends Component { - {strategy.name} + + {strategy.name} {strategy.deprecated ? - Deprecated : ''} + + {strategy.deprecated ? ( + + ) : ( + + )} {strategy.editable === false || !hasPermission(DELETE_STRATEGY) ? ( '' ) : ( diff --git a/frontend/src/component/strategies/list-container.jsx b/frontend/src/component/strategies/list-container.jsx index a71e7431f4..00ab786876 100644 --- a/frontend/src/component/strategies/list-container.jsx +++ b/frontend/src/component/strategies/list-container.jsx @@ -1,6 +1,6 @@ import { connect } from 'react-redux'; import StrategiesListComponent from './list-component.jsx'; -import { fetchStrategies, removeStrategy } from './../../store/strategy/actions'; +import { fetchStrategies, removeStrategy, deprecateStrategy, reactivateStrategy } from './../../store/strategy/actions'; import { hasPermission } from '../../permissions'; const mapStateToProps = state => { @@ -19,6 +19,18 @@ const mapDispatchToProps = dispatch => ({ removeStrategy(strategy)(dispatch); } }, + deprecateStrategy: strategy => { + // eslint-disable-next-line no-alert + if (window.confirm('Are you sure you want to deprecate this strategy?')) { + deprecateStrategy(strategy)(dispatch); + } + }, + reactivateStrategy: strategy => { + // eslint-disable-next-line no-alert + if (window.confirm('Are you sure you want to reactivate this strategy?')) { + reactivateStrategy(strategy)(dispatch); + } + }, fetchStrategies: () => fetchStrategies()(dispatch), }); diff --git a/frontend/src/component/strategies/show-strategy-component.js b/frontend/src/component/strategies/show-strategy-component.js index 6899e4f096..fac964b68f 100644 --- a/frontend/src/component/strategies/show-strategy-component.js +++ b/frontend/src/component/strategies/show-strategy-component.js @@ -32,6 +32,13 @@ class ShowStrategyComponent extends PureComponent { return (
+ {strategy.deprecated ? ( + +
Deprecated
+
+ ) : ( + '' + )}
Parameters

diff --git a/frontend/src/store/strategy/actions.js b/frontend/src/store/strategy/actions.js index 68aa55ff00..a2ef8a23d4 100644 --- a/frontend/src/store/strategy/actions.js +++ b/frontend/src/store/strategy/actions.js @@ -5,14 +5,20 @@ import { dispatchAndThrow } from '../util'; export const ADD_STRATEGY = 'ADD_STRATEGY'; export const UPDATE_STRATEGY = 'UPDATE_STRATEGY'; export const REMOVE_STRATEGY = 'REMOVE_STRATEGY'; +export const DEPRECATE_STRATEGY = 'DEPRECATE_STRATEGY'; +export const REACTIVATE_STRATEGY = 'REACTIVATE_STRATEGY'; export const REQUEST_STRATEGIES = 'REQUEST_STRATEGIES'; export const START_CREATE_STRATEGY = 'START_CREATE_STRATEGY'; export const START_UPDATE_STRATEGY = 'START_UPDATE_STRATEGY'; +export const START_DEPRECATE_STRATEGY = 'START_DEPRECATE_STRATEGY'; +export const START_REACTIVATE_STRATEGY = 'START_REACTIVATE_STRATEGY'; export const RECEIVE_STRATEGIES = 'RECEIVE_STRATEGIES'; export const ERROR_RECEIVE_STRATEGIES = 'ERROR_RECEIVE_STRATEGIES'; export const ERROR_CREATING_STRATEGY = 'ERROR_CREATING_STRATEGY'; export const ERROR_UPDATING_STRATEGY = 'ERROR_UPDATING_STRATEGY'; export const ERROR_REMOVING_STRATEGY = 'ERROR_REMOVING_STRATEGY'; +export const ERROR_DEPRECATING_STRATEGY = 'ERROR_DEPRECATING_STRATEGY'; +export const ERROR_REACTIVATING_STRATEGY = 'ERROR_REACTIVATING_STRATEGY'; const addStrategy = strategy => ({ type: ADD_STRATEGY, strategy }); const createRemoveStrategy = strategy => ({ type: REMOVE_STRATEGY, strategy }); @@ -26,6 +32,11 @@ const startCreate = () => ({ type: START_CREATE_STRATEGY }); const startUpdate = () => ({ type: START_UPDATE_STRATEGY }); +const startDeprecate = () => ({ type: START_DEPRECATE_STRATEGY }); +const deprecateStrategyEvent = strategy => ({ type: DEPRECATE_STRATEGY, strategy }); +const startReactivate = () => ({ type: START_REACTIVATE_STRATEGY }); +const reactivateStrategyEvent = strategy => ({ type: REACTIVATE_STRATEGY, strategy }); + export function fetchStrategies() { return dispatch => { dispatch(startRequest()); @@ -70,3 +81,21 @@ export function removeStrategy(strategy) { export function getApplicationsWithStrategy(strategyName) { return applicationApi.fetchApplicationsWithStrategyName(strategyName); } + +export function deprecateStrategy(strategy) { + return dispatch => { + dispatch(startDeprecate()); + api.deprecate(strategy) + .then(() => dispatch(deprecateStrategyEvent(strategy))) + .catch(dispatchAndThrow(dispatch, ERROR_DEPRECATING_STRATEGY)); + }; +} + +export function reactivateStrategy(strategy) { + return dispatch => { + dispatch(startReactivate()); + api.reactivate(strategy) + .then(() => dispatch(reactivateStrategyEvent(strategy))) + .catch(dispatchAndThrow(dispatch, ERROR_REACTIVATING_STRATEGY)); + }; +} diff --git a/frontend/src/store/strategy/api.js b/frontend/src/store/strategy/api.js index 8a3553154f..2387fb550f 100644 --- a/frontend/src/store/strategy/api.js +++ b/frontend/src/store/strategy/api.js @@ -34,9 +34,27 @@ function remove(strategy) { }).then(throwIfNotSuccess); } +function deprecate(strategy) { + return fetch(`${URI}/${strategy.name}/deprecate`, { + method: 'POST', + headers, + credentials: 'include', + }).then(throwIfNotSuccess); +} + +function reactivate(strategy) { + return fetch(`${URI}/${strategy.name}/reactivate`, { + method: 'POST', + headers, + credentials: 'include', + }).then(throwIfNotSuccess); +} + export default { fetchAll, create, update, remove, + deprecate, + reactivate, }; diff --git a/frontend/src/store/strategy/index.js b/frontend/src/store/strategy/index.js index f8fe4783c6..900ae3259c 100644 --- a/frontend/src/store/strategy/index.js +++ b/frontend/src/store/strategy/index.js @@ -1,5 +1,12 @@ import { List, Map as $Map } from 'immutable'; -import { RECEIVE_STRATEGIES, REMOVE_STRATEGY, ADD_STRATEGY, UPDATE_STRATEGY } from './actions'; +import { + RECEIVE_STRATEGIES, + REMOVE_STRATEGY, + ADD_STRATEGY, + UPDATE_STRATEGY, + DEPRECATE_STRATEGY, + REACTIVATE_STRATEGY, +} from './actions'; function getInitState() { return new $Map({ list: new List() }); @@ -25,6 +32,19 @@ function updateStrategy(state, action) { ); } +function setDeprecationStatus(state, action, status) { + return state.update('list', list => + list.map(strategy => { + if (strategy.name === action.strategy.name) { + action.strategy.deprecated = status; + return action.strategy; + } else { + return strategy; + } + }) + ); +} + const strategies = (state = getInitState(), action) => { switch (action.type) { case RECEIVE_STRATEGIES: @@ -35,6 +55,10 @@ const strategies = (state = getInitState(), action) => { return state.update('list', list => list.push(action.strategy)); case UPDATE_STRATEGY: return updateStrategy(state, action); + case DEPRECATE_STRATEGY: + return setDeprecationStatus(state, action, true); + case REACTIVATE_STRATEGY: + return setDeprecationStatus(state, action, false); default: return state; }