]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12322 Project Mark/Unmark as favorite button is not refreshing properly when...
authorphilippe-perrin-sonarsource <philippe.perrin@sonarsource.com>
Wed, 7 Aug 2019 09:16:44 +0000 (11:16 +0200)
committerSonarTech <sonartech@sonarsource.com>
Fri, 9 Aug 2019 18:21:25 +0000 (20:21 +0200)
server/sonar-web/package.json
server/sonar-web/src/main/js/components/controls/Favorite.tsx
server/sonar-web/src/main/js/components/controls/__tests__/Favorite-test.tsx
server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/Favorite-test.tsx.snap

index d31730a77ac5a2f5f0288ed6a669f179d4eeca8c..e909a0cfcad8ed425479a30a3245aafeb6b5bab6 100644 (file)
@@ -35,7 +35,7 @@
     "regenerator-runtime": "0.13.2",
     "remark-custom-blocks": "2.3.0",
     "remark-slug": "5.1.0",
-    "sonar-ui-common": "0.0.19",
+    "sonar-ui-common": "0.0.20",
     "unist-util-visit": "1.4.0",
     "valid-url": "1.0.9",
     "whatwg-fetch": "2.0.4"
index a75341bbe11ea3c93d15111bcf0e315971a782e5..b070a9e04e2108a1db21ced7513c02e5cb7354d6 100644 (file)
@@ -18,7 +18,7 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 import * as React from 'react';
-import FavoriteBase from 'sonar-ui-common/components/controls/FavoriteBase';
+import FavoriteButton from 'sonar-ui-common/components/controls/FavoriteButton';
 import { addFavorite, removeFavorite } from '../../api/favorites';
 
 interface Props {
@@ -29,20 +29,62 @@ interface Props {
   handleFavorite?: (component: string, isFavorite: boolean) => void;
 }
 
-export default class Favorite extends React.PureComponent<Props> {
-  callback = (isFavorite: boolean) =>
-    this.props.handleFavorite && this.props.handleFavorite(this.props.component, isFavorite);
+interface State {
+  favorite: boolean;
+}
 
-  add = () => {
-    return addFavorite(this.props.component).then(() => this.callback(true));
-  };
+export default class Favorite extends React.PureComponent<Props, State> {
+  mounted = false;
+
+  constructor(props: Props) {
+    super(props);
+
+    this.state = {
+      favorite: props.favorite
+    };
+  }
+
+  componentDidMount() {
+    this.mounted = true;
+  }
+
+  componentDidUpdate(_prevProps: Props, prevState: State) {
+    if (prevState.favorite !== this.props.favorite) {
+      this.setState({ favorite: this.props.favorite });
+    }
+  }
 
-  remove = () => {
-    return removeFavorite(this.props.component).then(() => this.callback(false));
+  componentWillUnmount() {
+    this.mounted = false;
+  }
+
+  toggleFavorite = () => {
+    const newFavorite = !this.state.favorite;
+    const apiMethod = newFavorite ? addFavorite : removeFavorite;
+
+    return apiMethod(this.props.component).then(() => {
+      if (this.mounted) {
+        this.setState({ favorite: newFavorite }, () => {
+          if (this.props.handleFavorite) {
+            this.props.handleFavorite(this.props.component, newFavorite);
+          }
+        });
+      }
+    });
   };
 
   render() {
-    const { component, handleFavorite, ...other } = this.props;
-    return <FavoriteBase {...other} addFavorite={this.add} removeFavorite={this.remove} />;
+    const { className, qualifier } = this.props;
+    const { favorite } = this.state;
+
+    return (
+      <FavoriteButton
+        className={className}
+        favorite={favorite}
+        qualifier={qualifier}
+        toggleFavorite={this.toggleFavorite}
+      />
+    );
   }
 }
+/*  */
index 21f3af2c980a5560b879a52a20a82fe4ab34ece3..8432d71ecd0710f0300ac370b80cbb6924f647ba 100644 (file)
@@ -19,6 +19,7 @@
  */
 import { shallow } from 'enzyme';
 import * as React from 'react';
+import FavoriteButton from 'sonar-ui-common/components/controls/FavoriteButton';
 import Favorite from '../Favorite';
 
 jest.mock('../../../api/favorites', () => ({
@@ -33,15 +34,14 @@ it('renders', () => {
 it('calls handleFavorite when given', async () => {
   const handleFavorite = jest.fn();
   const wrapper = shallowRender(handleFavorite);
-  const favoriteBase = wrapper.find('FavoriteBase');
-  const addFavorite = favoriteBase.prop<Function>('addFavorite');
-  const removeFavorite = favoriteBase.prop<Function>('removeFavorite');
+  const favoriteBase = wrapper.find(FavoriteButton);
+  const toggleFavorite = favoriteBase.prop<Function>('toggleFavorite');
 
-  removeFavorite();
+  toggleFavorite();
   await new Promise(setImmediate);
   expect(handleFavorite).toHaveBeenCalledWith('foo', false);
 
-  addFavorite();
+  toggleFavorite();
   await new Promise(setImmediate);
   expect(handleFavorite).toHaveBeenCalledWith('foo', true);
 });
index 592afbf93983631a7aef22c393883c065886ea04..d3419ee44ae77dccb69d51a350c7daba2bfbd41b 100644 (file)
@@ -1,10 +1,9 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
 exports[`renders 1`] = `
-<FavoriteBase
-  addFavorite={[Function]}
+<FavoriteButton
   favorite={true}
   qualifier="TRK"
-  removeFavorite={[Function]}
+  toggleFavorite={[Function]}
 />
 `;