Skip to content

Commit

Permalink
[Fixes #1136] The client should only depend on the new favorite field…
Browse files Browse the repository at this point in the history
… in resource object (#1145)
  • Loading branch information
DavidQuartz authored Aug 18, 2022
1 parent d54a9b9 commit 7c4cf92
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 101 deletions.
16 changes: 0 additions & 16 deletions geonode_mapstore_client/client/js/actions/gnresource.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export const ENABLE_MAP_THUMBNAIL_VIEWER = 'GEONODE_ENABLE_MAP_THUMBNAIL_VIEWER'
export const DOWNLOAD_RESOURCE = 'GEONODE_DOWNLOAD_RESOURCE';
export const DOWNLOAD_COMPLETE = 'GEONODE_DOWNLOAD_COMPLETE';
export const UPDATE_SINGLE_RESOURCE = 'GEONODE_UPDATE_SINGLE_RESOURCE';
export const SET_FAVORITE_RESOURCES = 'GEONODE_SET_FAVORITE_RESOURCES';
export const REMOVE_FAVORITE_RESOURCE = 'GEONODE_REMOVE_FAVORITE_RESOURCE';


/**
Expand Down Expand Up @@ -311,17 +309,3 @@ export function downloadComplete(resource) {
resource
};
}

export function setFavoriteResources(favorites) {
return {
type: SET_FAVORITE_RESOURCES,
favorites
};
}

export function removeFavoriteResource(pk) {
return {
type: REMOVE_FAVORITE_RESOURCE,
pk
};
}
5 changes: 0 additions & 5 deletions geonode_mapstore_client/client/js/api/geonode/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,6 @@ export const setFavoriteResource = (pk, favorite) => {
.then(({ data }) => data );
};

export const getUserFavoriteResources = () => {
return axios.get(parseDevHostname(`${endpoints[RESOURCES]}/favorites`))
.then(({ data }) => data.favorites.map(({ pk }) => pk));
};

export const getResourceByPk = (pk) => {
return axios.get(parseDevHostname(`${endpoints[RESOURCES]}/${pk}`), {
params: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import Loader from '@mapstore/framework/components/misc/Loader';
import { getUserName } from '@js/utils/SearchUtils';
import ZoomTo from '@js/components/ZoomTo';
import { boundsToExtentString } from '@js/utils/CoordinatesUtils';
import { getUserFavoriteResources } from '@js/api/geonode/v2';

const Map = mapTypeHOC(BaseMap);
Map.displayName = 'Map';
Expand Down Expand Up @@ -206,10 +205,7 @@ function DetailsPanel({
enableMapViewer,
onClose,
onAction,
canDownload,
setFavorites,
removeFavorite,
resourceId
canDownload
}) {
const detailsContainerNode = useRef();
const isMounted = useRef();
Expand All @@ -223,10 +219,6 @@ function DetailsPanel({
};
}, []);

useEffect(() => {
getUserFavoriteResources().then(favorites => setFavorites(favorites));
}, [resourceId]);

if (!resource && !loading) {
return null;
}
Expand All @@ -240,9 +232,8 @@ function DetailsPanel({
}, 700);
};

const handleFavorite = () => {
favorite ? removeFavorite(resourceId) : setFavorites(resourceId);
onFavorite(!favorite);
const handleFavorite = (fav) => {
onFavorite(!fav);
};

const handleResourceThumbnailUpdate = () => {
Expand Down Expand Up @@ -549,7 +540,7 @@ function DetailsPanel({
enableFavorite &&
<Button
variant="default"
onClick={debounce(handleFavorite, 500)}>
onClick={debounce(() => handleFavorite(favorite), 500)}>
<FaIcon name={favorite ? 'star' : 'star-o'} />
</Button>
}
Expand Down
37 changes: 19 additions & 18 deletions geonode_mapstore_client/client/js/epics/favorite.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import { Observable } from 'rxjs';
import {
updateResourceProperties,
SET_FAVORITE_RESOURCE,
removeFavoriteResource,
setFavoriteResources
SET_FAVORITE_RESOURCE
} from '@js/actions/gnresource';
import {
updateResources
Expand All @@ -37,21 +35,24 @@ export const gnSaveFavoriteContent = (action$, store) =>
: [...resources, resource]) : resources;


return Observable
.defer(() => setFavoriteResource(pk, favorite))
.switchMap(() => {
return Observable.of(
updateResourceProperties({
'favorite': favorite
}),
updateResources(newResources, true)
);
})
.catch((error) => {
return Observable.of(
action.favorite ? removeFavoriteResource(pk) : setFavoriteResources(pk),
errorNotification({ title: "gnviewer.cannotPerfomAction", message: error?.data?.message || error?.data?.detail || error?.originalError?.message || "gnviewer.syncErrorDefault" }));
});
return Observable.concat(
Observable.of(updateResourceProperties({'favorite': favorite})),
Observable.defer(() => setFavoriteResource(pk, favorite))
.switchMap(() => {
return Observable.of(
updateResources(newResources, true)
);
})
.catch((error) => {
return Observable.of(
action.favorite ? updateResourceProperties({
'favorite': false
}) : updateResourceProperties({
'favorite': true
}),
errorNotification({ title: "gnviewer.cannotPerfomAction", message: error?.data?.message || error?.data?.detail || error?.originalError?.message || "gnviewer.syncErrorDefault" }));
})
);

});

Expand Down
15 changes: 5 additions & 10 deletions geonode_mapstore_client/client/js/plugins/DetailViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import {
setMapThumbnail,
setResourceThumbnail,
enableMapThumbnailViewer,
downloadResource,
setFavoriteResources,
removeFavoriteResource
downloadResource
} from '@js/actions/gnresource';
import { processingDownload } from '@js/selectors/resourceservice';
import FaIcon from '@js/components/FaIcon/FaIcon';
Expand Down Expand Up @@ -57,14 +55,13 @@ const ConnectedDetailsPanel = connect(
updatingThumbnailResource,
mapSelector,
state => state?.gnresource?.showMapThumbnail || false,
processingDownload,
state => state?.gnresource?.favoriteResources || []
], (resource, loading, favorite, savingThumbnailMap, layers, thumbnailChanged, resourceThumbnailUpdating, mapData, showMapThumbnail, downloading, favorites) => ({
processingDownload
], (resource, loading, favorite, savingThumbnailMap, layers, thumbnailChanged, resourceThumbnailUpdating, mapData, showMapThumbnail, downloading) => ({
layers: layers,
resource,
loading,
savingThumbnailMap,
favorite: favorite || favorites.includes(resource?.pk),
favorite: favorite,
isThumbnailChanged: thumbnailChanged,
resourceThumbnailUpdating,
initialBbox: mapData?.bbox,
Expand All @@ -79,9 +76,7 @@ const ConnectedDetailsPanel = connect(
onMapThumbnail: setMapThumbnail,
onResourceThumbnail: setResourceThumbnail,
onClose: enableMapThumbnailViewer,
onAction: downloadResource,
setFavorites: setFavoriteResources,
removeFavorite: removeFavoriteResource
onAction: downloadResource
}
)(DetailsPanel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
resourceError,
updateResourceProperties,
setResourceType,
setNewResource,
setResourceId,
setResourcePermissions,
editThumbnailResource,
Expand Down Expand Up @@ -81,16 +80,6 @@ describe('gnresource reducer', () => {
type: resourceType
});
});
it('should test setNewResource', () => {
const state = gnresource({}, setNewResource());
expect(state).toEqual({
selectedLayerPermissions: [],
data: {},
permissions: [],
favoriteResources: [],
isNew: true
});
});
it('should test setResourceId', () => {
const id = 1;
const state = gnresource({}, setResourceId(id));
Expand Down
23 changes: 3 additions & 20 deletions geonode_mapstore_client/client/js/reducers/gnresource.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {isEqual, uniq} from 'lodash';
import {isEqual } from 'lodash';
import {
RESOURCE_LOADING,
SET_RESOURCE,
Expand All @@ -27,9 +27,7 @@ import {
SET_RESOURCE_COMPACT_PERMISSIONS,
UPDATE_RESOURCE_COMPACT_PERMISSIONS,
RESET_GEO_LIMITS,
ENABLE_MAP_THUMBNAIL_VIEWER,
SET_FAVORITE_RESOURCES,
REMOVE_FAVORITE_RESOURCE
ENABLE_MAP_THUMBNAIL_VIEWER
} from '@js/actions/gnresource';

import {
Expand All @@ -40,8 +38,7 @@ import {
const defaultState = {
selectedLayerPermissions: [],
data: {},
permissions: [],
favoriteResources: []
permissions: []
};

function gnresource(state = defaultState, action) {
Expand Down Expand Up @@ -211,20 +208,6 @@ function gnresource(state = defaultState, action) {
};
}
return state;
case SET_FAVORITE_RESOURCES: {
const favoriteResources = Array.isArray(action.favorites) ? action.favorites : uniq([...state.favoriteResources, action.favorites]);
return {
...state,
favoriteResources: [...favoriteResources]
};
}
case REMOVE_FAVORITE_RESOURCE: {
const newFavorites = state.favoriteResources?.filter(fav => fav !== action.pk);
return {
...state,
favoriteResources: [...newFavorites]
};
}
default:
return state;
}
Expand Down
13 changes: 5 additions & 8 deletions geonode_mapstore_client/client/js/routes/Detail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
loadFeaturedResources
} from '@js/actions/gnsearch';

import { downloadResource, setFavoriteResource, setFavoriteResources, removeFavoriteResource } from '@js/actions/gnresource';
import { downloadResource, setFavoriteResource } from '@js/actions/gnresource';
import {
hashLocationToHref,
clearQueryParams,
Expand All @@ -51,20 +51,17 @@ const ConnectedDetailsPanel = connect(
state => state?.gnresource?.loading || false,
state => state?.gnresource?.data?.favorite || false,
processingDownload,
state => state?.gnresource?.data || null,
state => state?.gnresource?.favoriteResources || []
], (loading, favorite, downloading, resource, favorites) => ({
state => state?.gnresource?.data || null
], (loading, favorite, downloading, resource) => ({
loading,
favorite: favorite || favorites.includes(resource?.pk),
favorite: favorite,
downloading,
canDownload: resourceHasPermission(resource, 'download_resourcebase'),
resourceId: resource.pk
})),
{
onFavorite: setFavoriteResource,
onAction: downloadResource,
setFavorites: setFavoriteResources,
removeFavorite: removeFavoriteResource
onAction: downloadResource
}
)(DetailsPanel);
function Detail({
Expand Down

0 comments on commit 7c4cf92

Please sign in to comment.