-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
--------- Co-authored-by: Lorenzo Natali <[email protected]>
- Loading branch information
1 parent
ec8726a
commit 77bd26e
Showing
34 changed files
with
1,019 additions
and
589 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -787,6 +787,7 @@ | |
] | ||
} | ||
}, | ||
{ "name": "Favorites" }, | ||
{ | ||
"name": "ResourcesFiltersForm", | ||
"cfg": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2025, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { createPlugin } from '../../utils/PluginsUtils'; | ||
import { connect } from 'react-redux'; | ||
import { createStructuredSelector } from 'reselect'; | ||
import { userSelector } from '../../selectors/security'; | ||
import { getRouterLocation } from './selectors/resources'; | ||
import { searchResources } from './actions/resources'; | ||
import Favorites from './containers/Favorites'; | ||
|
||
const ConnectedFavorites = connect( | ||
createStructuredSelector({ | ||
user: userSelector, | ||
location: getRouterLocation | ||
}), | ||
{ | ||
onSearch: searchResources | ||
} | ||
)(Favorites); | ||
|
||
/** | ||
* renders a button inside the resource card to add/remove a resource to user favorites | ||
* @name Favorites. | ||
*/ | ||
export default createPlugin('Favorites', { | ||
component: () => null, | ||
containers: { | ||
ResourcesGrid: { | ||
target: 'card-buttons', | ||
position: 0, | ||
Component: ConnectedFavorites | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
web/client/plugins/ResourcesCatalog/containers/Favorites.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2025, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import url from 'url'; | ||
import useIsMounted from '../hooks/useIsMounted'; | ||
import GeoStoreDAO from '../../../api/GeoStoreDAO'; | ||
import { castArray } from 'lodash'; | ||
|
||
/** | ||
* Favorites button component | ||
* @prop {object} user user properties | ||
* @prop {class|function} component a valid component | ||
* @prop {object} resource resource properties | ||
* @prop {object} location router location | ||
* @prop {function} onSearch trigger a refresh request after changing the favorite association | ||
* @prop {number} delayTime delay time to complete the request | ||
* @prop {string} renderType define the component type (eg. menuItem) | ||
*/ | ||
function Favorites({ | ||
user, | ||
component, | ||
resource, | ||
location, | ||
onSearch, | ||
delayTime, | ||
renderType | ||
}) { | ||
const { query } = url.parse(location?.search || '', true); | ||
const f = castArray(query.f || []); | ||
const isMounted = useIsMounted(); | ||
const [loading, setLoading] = useState(false); | ||
const [isFavorite, setIsFavorite] = useState(!!resource?.isFavorite); | ||
|
||
function handleOnClick() { | ||
if (!loading) { | ||
setLoading(true); | ||
const promise = isFavorite | ||
? GeoStoreDAO.removeFavoriteResource | ||
: GeoStoreDAO.addFavoriteResource; | ||
promise(user?.id, resource?.id) | ||
.then(() => isMounted(() => { | ||
setIsFavorite(!isFavorite); | ||
})) | ||
.finally(() => | ||
setTimeout(() => isMounted(() => { | ||
// apply a delay to show the spinner | ||
// and give a feedback to the user | ||
setLoading(false); | ||
if (f.includes('favorite')) { | ||
onSearch({ refresh: true }); | ||
} | ||
}), delayTime) | ||
); | ||
} | ||
} | ||
const Component = component; | ||
return Component && resource?.id && user?.id | ||
? ( | ||
<Component | ||
glyph={isFavorite ? 'heart' : 'heart-o'} | ||
iconType="glyphicon" | ||
labelId={!loading || renderType === 'menuItem' ? `resourcesCatalog.${isFavorite ? 'removeFromFavorites' : 'addToFavorites'}` : undefined} | ||
square | ||
onClick={handleOnClick} | ||
loading={loading} | ||
/> | ||
) | ||
: null; | ||
} | ||
|
||
Favorites.propTypes = { | ||
user: PropTypes.object, | ||
component: PropTypes.any, | ||
resource: PropTypes.object, | ||
location: PropTypes.object, | ||
onSearch: PropTypes.func, | ||
delayTime: PropTypes.number | ||
}; | ||
|
||
Favorites.defaultProps = { | ||
onSearch: () => {}, | ||
delayTime: 500 | ||
}; | ||
|
||
export default Favorites; |
Oops, something went wrong.