Skip to content

Commit

Permalink
Merge pull request #27 from zooniverse/fetch-all-roles
Browse files Browse the repository at this point in the history
Fetch all roles
  • Loading branch information
mcbouslog authored Dec 12, 2018
2 parents 2bef9b8 + 10af03e commit 9605cb4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const App = () => (
<FavoritesContainer
favoriteCollection={favoriteCollection}
linkedSubjects={linkedSubjects}
matchesUser={matchesUser}
/>
</div>
</Box>
Expand Down
17 changes: 10 additions & 7 deletions src/containers/FavoritesContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
9 changes: 7 additions & 2 deletions src/containers/RecentsContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/containers/StatsContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
49 changes: 29 additions & 20 deletions src/context/ExplorerContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand All @@ -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() {
Expand Down
8 changes: 7 additions & 1 deletion src/context/FavoritesContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -51,6 +51,12 @@ export class FavoritesProvider extends Component {
? collection.links.subjects
: []
});
} else {
this.setState({
favoriteCollection: null,
linkedSubjects: []
});
console.warn('Favorites empty.');
}
});
}
Expand Down

0 comments on commit 9605cb4

Please sign in to comment.