From f4159d1c5c09508bb008434e06a56f0c229908b4 Mon Sep 17 00:00:00 2001 From: mcbouslog Date: Wed, 12 Dec 2018 17:24:16 -0600 Subject: [PATCH 1/2] Fetch all project roles --- src/context/ExplorerContext.jsx | 49 +++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/context/ExplorerContext.jsx b/src/context/ExplorerContext.jsx index a11f0e3..1480f8f 100644 --- a/src/context/ExplorerContext.jsx +++ b/src/context/ExplorerContext.jsx @@ -41,30 +41,39 @@ export class ExplorerProvider extends Component { } } - fetchRoles() { - const { project, user } = this.props; - project - .get('project_roles') - .then(roles => { - if (roles.length) { - const collaboratorRoles = roles.filter( - role => - role.roles.includes('collaborator') || - role.roles.includes('owner') - ); - return collaboratorRoles.some( - role => role.links.owner.id === user.id - ); + async fetchAllRoles(project, roles = [], _page = 1) { + return apiClient + .type('project_roles') + .get({ project_id: project.id, page: _page }) + .then(projRoles => { + const meta = projRoles[0].getMeta(); + const newProjRoles = roles.concat(projRoles); + + if (meta.next_page) { + return this.fetchAllRoles(project, newProjRoles, meta.next_page); } - return false; + return newProjRoles; }) - .catch(() => console.warn('Failed to fetch project roles')); + .catch(error => console.error('Error loading roles.', error)); } - checkPermission() { - const { user } = this.props; - if ((user && user.admin) || this.fetchRoles()) { + async checkPermission() { + const { project, user } = this.props; + + if (user && user.admin === 'purple') { this.fetchExplorer(); + } else if (user) { + const roles = await this.fetchAllRoles(project); + const collaboratorRoles = roles.filter( + role => + role.roles.includes('collaborator') || role.roles.includes('owner') + ); + const isCollab = collaboratorRoles.some( + role => role.links.owner.id === user.id + ); + if (isCollab) { + this.fetchExplorer(); + } } } @@ -77,7 +86,7 @@ export class ExplorerProvider extends Component { const matchesUser = this.props.user.id === userResponse.id; this.setState({ explorer: userResponse, matchesUser }); }) - .catch(() => console.warn('Failed to fetch explorer')); + .catch(() => console.warn('Failed to fetch explorer.')); } render() { From 10af03e6e02c6fcffa99c29f8ae54016772bcdf4 Mon Sep 17 00:00:00 2001 From: mcbouslog Date: Wed, 12 Dec 2018 17:26:20 -0600 Subject: [PATCH 2/2] Refactor explorer change --- src/components/App.jsx | 1 - src/containers/FavoritesContainer.jsx | 17 ++++++++++------- src/containers/RecentsContainer.jsx | 9 +++++++-- src/containers/StatsContainer.jsx | 2 +- src/context/FavoritesContext.jsx | 8 +++++++- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/components/App.jsx b/src/components/App.jsx index 5d19b5a..0749764 100644 --- a/src/components/App.jsx +++ b/src/components/App.jsx @@ -60,7 +60,6 @@ const App = () => ( diff --git a/src/containers/FavoritesContainer.jsx b/src/containers/FavoritesContainer.jsx index d24578b..9d96745 100644 --- a/src/containers/FavoritesContainer.jsx +++ b/src/containers/FavoritesContainer.jsx @@ -32,9 +32,9 @@ class FavoritesContainer extends React.Component { } fetchFavoriteSubjects(page = 1) { - const { favoriteCollection, linkedSubjects, matchesUser } = this.props; + const { favoriteCollection, linkedSubjects } = this.props; - if (matchesUser && favoriteCollection && linkedSubjects.length) { + if (favoriteCollection && linkedSubjects.length) { const query = { page, page_size: 3 @@ -48,11 +48,16 @@ class FavoritesContainer extends React.Component { favoriteSubjects, meta: favoriteSubjects[0].getMeta() }); + } else { + this.setState({ + favoriteSubjects: null, + meta: null + }); } }) .catch(() => { if (console) { - console.warn('Failed to fetch favorites'); + console.warn('Failed to fetch favorites.'); } }); } else { @@ -90,14 +95,12 @@ FavoritesContainer.propTypes = { favoriteCollection: PropTypes.shape({ id: PropTypes.string }), - linkedSubjects: PropTypes.arrayOf(PropTypes.string), - matchesUser: PropTypes.bool + linkedSubjects: PropTypes.arrayOf(PropTypes.string) }; FavoritesContainer.defaultProps = { favoriteCollection: null, - linkedSubjects: [], - matchesUser: false + linkedSubjects: [] }; export default FavoritesContainer; diff --git a/src/containers/RecentsContainer.jsx b/src/containers/RecentsContainer.jsx index e0dfff8..4e969a6 100644 --- a/src/containers/RecentsContainer.jsx +++ b/src/containers/RecentsContainer.jsx @@ -45,11 +45,16 @@ class RecentsContainer extends React.Component { explorer .get('recents', query) .then(recents => { - this.setState({ meta: recents[0].getMeta(), recents }); + if (recents.length) { + this.setState({ meta: recents[0].getMeta(), recents }); + } else { + this.setState({ meta: null, recents: null }); + console.warn('Recents empty.'); + } }) .catch(() => { if (console) { - console.warn('Failed to fetch recents'); + console.warn('Failed to fetch recents.'); } }); } else { diff --git a/src/containers/StatsContainer.jsx b/src/containers/StatsContainer.jsx index 65a3065..f1b6497 100644 --- a/src/containers/StatsContainer.jsx +++ b/src/containers/StatsContainer.jsx @@ -62,7 +62,7 @@ class StatsContainer extends React.Component { }) .catch(() => { if (console) { - console.warn('Failed to fetch stats'); + console.warn('Failed to fetch stats.'); } }); } else { diff --git a/src/context/FavoritesContext.jsx b/src/context/FavoritesContext.jsx index 08860ed..b498b93 100644 --- a/src/context/FavoritesContext.jsx +++ b/src/context/FavoritesContext.jsx @@ -42,7 +42,7 @@ export class FavoritesProvider extends Component { favorite: true }) .then(collections => { - if (collections && collections.length > 0) { + if (collections.length) { const [collection] = collections; this.setState({ favoriteCollection: collection, @@ -51,6 +51,12 @@ export class FavoritesProvider extends Component { ? collection.links.subjects : [] }); + } else { + this.setState({ + favoriteCollection: null, + linkedSubjects: [] + }); + console.warn('Favorites empty.'); } }); }