Browse Source

SONAR-12322 Project Mark/Unmark as favorite button is not refreshing properly when switching of project

tags/8.0
philippe-perrin-sonarsource 4 years ago
parent
commit
7ae93382eb

+ 1
- 1
server/sonar-web/package.json View 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"

+ 53
- 11
server/sonar-web/src/main/js/components/controls/Favorite.tsx View 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}
/>
);
}
}
/* */

+ 5
- 5
server/sonar-web/src/main/js/components/controls/__tests__/Favorite-test.tsx View 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);
});

+ 2
- 3
server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/Favorite-test.tsx.snap View 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]}
/>
`;

Loading…
Cancel
Save