Commit 647575e4 authored by Paul Kaplan's avatar Paul Kaplan

Move permissions into its own reducer

parent 82da633e
const {selectUserId, selectIsAdmin, selectIsSocial, selectIsLoggedIn} = require('./session');
// Fine-grain selector helpers - not exported, use the higher level selectors below
const isCreator = state => selectUserId(state) === state.studio.owner;
const isCurator = state => state.studio.curator;
const isManager = state => state.studio.manager || isCreator(state);
// Action-based permissions selectors
const selectCanEditInfo = state => selectIsAdmin(state) || isManager(state);
const selectCanAddProjects = state =>
isManager(state) ||
isCurator(state) ||
(selectIsSocial(state) && state.studio.openToAll);
// This isn't "canComment" since they could be muted, but comment composer handles that
const selectShowCommentComposer = state => selectIsSocial(state);
const selectCanReportComment = state => selectIsSocial(state);
const selectCanRestoreComment = state => selectIsAdmin(state);
// On the project page, project owners can delete comments with a confirmation,
// and admins can delete comments without a confirmation. For now, only admins
// can delete studio comments, so the following two are the same.
const selectCanDeleteComment = state => selectIsAdmin(state);
const selectCanDeleteCommentWithoutConfirm = state => selectIsAdmin(state);
const selectCanFollowStudio = state => selectIsLoggedIn(state);
export {
selectCanEditInfo,
selectCanAddProjects,
selectCanFollowStudio,
selectShowCommentComposer,
selectCanDeleteComment,
selectCanDeleteCommentWithoutConfirm,
selectCanReportComment,
selectCanRestoreComment
};
......@@ -3,10 +3,7 @@ const keyMirror = require('keymirror');
const api = require('../lib/api');
const log = require('../lib/log');
const {
selectUserId, selectIsAdmin, selectIsSocial, selectUsername, selectToken,
selectIsLoggedIn
} = require('./session');
const {selectUsername, selectToken} = require('./session');
const Status = keyMirror({
FETCHED: null,
......@@ -68,7 +65,6 @@ const studioReducer = (state, action) => {
};
// Action Creators
const setFetchStatus = (fetchType, fetchStatus, error) => ({
type: 'SET_FETCH_STATUS',
fetchType,
......@@ -86,36 +82,12 @@ const setRoles = roles => ({
roles: roles
});
// Selectors
// Fine-grain selector helpers - not exported, use the higher level selectors below
const isCreator = state => selectUserId(state) === state.studio.owner;
const isCurator = state => state.studio.curator;
const isManager = state => state.studio.manager || isCreator(state);
// Action-based permissions selectors
const selectCanEditInfo = state => selectIsAdmin(state) || isManager(state);
const selectCanAddProjects = state =>
isManager(state) ||
isCurator(state) ||
(selectIsSocial(state) && state.studio.openToAll);
const selectShowCommentComposer = state => selectIsSocial(state);
const selectCanReportComment = state => selectIsSocial(state);
const selectCanRestoreComment = state => selectIsAdmin(state);
// On the project page, project owners can delete comments with a confirmation,
// and admins can delete comments without a confirmation. For now, only admins
// can delete studio comments, so the following two are the same.
const selectCanDeleteComment = state => selectIsAdmin(state);
const selectCanDeleteCommentWithoutConfirm = state => selectIsAdmin(state);
// Data selectors
const selectStudioId = state => state.studio.id;
const selectStudioTitle = state => state.studio.title;
const selectStudioDescription = state => state.studio.description;
const selectIsLoadingInfo = state => state.studio.infoStatus === Status.FETCHING;
const selectIsFollowing = state => state.studio.following;
const selectCanFollowStudio = state => selectIsLoggedIn(state);
const selectIsLoadingRoles = state => state.studio.rolesStatus === Status.FETCHING;
// Thunks
......@@ -180,13 +152,5 @@ module.exports = {
selectStudioDescription,
selectIsLoadingInfo,
selectIsLoadingRoles,
selectIsFollowing,
selectCanEditInfo,
selectCanAddProjects,
selectShowCommentComposer,
selectCanDeleteComment,
selectCanDeleteCommentWithoutConfirm,
selectCanReportComment,
selectCanRestoreComment,
selectCanFollowStudio
selectIsFollowing
};
......@@ -14,7 +14,7 @@ import {
selectCanDeleteCommentWithoutConfirm,
selectCanReportComment,
selectCanRestoreComment
} from '../../redux/studio.js';
} from '../../redux/studio-permissions';
const StudioComments = ({
comments,
......
......@@ -3,7 +3,8 @@ import React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {selectStudioDescription, selectIsLoadingInfo, selectCanEditInfo} from '../../redux/studio';
import {selectStudioDescription, selectIsLoadingInfo} from '../../redux/studio';
import {selectCanEditInfo} from '../../redux/studio-permissions';
import {
mutateStudioDescription, selectIsMutatingDescription, selectDescriptionMutationError
} from '../../redux/studio-mutations';
......
......@@ -2,8 +2,8 @@
import React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {selectIsFollowing, selectCanFollowStudio, selectIsLoadingRoles} from '../../redux/studio';
import {selectIsFollowing, selectIsLoadingRoles} from '../../redux/studio';
import {selectCanFollowStudio} from '../../redux/studio-permissions';
import {
mutateFollowingStudio, selectIsMutatingFollowing, selectFollowingMutationError
} from '../../redux/studio-mutations';
......
......@@ -36,10 +36,7 @@ const StudioInfo = ({
StudioInfo.propTypes = {
isLoggedIn: PropTypes.bool,
studio: PropTypes.shape({
title: PropTypes.string,
description: PropTypes.description
}),
studio: PropTypes.shape({}), // TODO remove, just for <Debug />
onLoadInfo: PropTypes.func,
onLoadRoles: PropTypes.func
};
......
......@@ -5,10 +5,10 @@ import {connect} from 'react-redux';
import {projectFetcher} from './lib/fetchers';
import {projects} from './lib/redux-modules';
import {selectCanAddProjects} from '../../redux/studio-permissions';
import Debug from './debug.jsx';
const {actions, selector: projectsSelector} = projects;
import {selectCanAddProjects} from '../../redux/studio';
const StudioProjects = ({
canAddProjects, items, error, loading, moreToLoad, onLoadMore
......
......@@ -3,7 +3,8 @@ import React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {selectStudioTitle, selectIsLoadingInfo, selectCanEditInfo} from '../../redux/studio';
import {selectStudioTitle, selectIsLoadingInfo} from '../../redux/studio';
import {selectCanEditInfo} from '../../redux/studio-permissions';
import {mutateStudioTitle, selectIsMutatingTitle, selectTitleMutationError} from '../../redux/studio-mutations';
const StudioTitle = ({
......
import {
getInitialState as getInitialStudioState,
selectCanEditInfo,
selectCanAddProjects,
selectShowCommentComposer,
selectCanDeleteComment,
selectCanDeleteCommentWithoutConfirm,
selectCanReportComment,
selectCanRestoreComment
} from '../../../src/redux/studio';
import {
getInitialState as getInitialSessionState
} from '../../../src/redux/session';
selectCanRestoreComment,
selectCanFollowStudio
} from '../../../src/redux/studio-permissions';
import {getInitialState as getInitialStudioState} from '../../../src/redux/studio';
import {getInitialState as getInitialSessionState} from '../../../src/redux/session';
import {sessions, studios} from '../../helpers/state-fixtures.json';
let state;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment