Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

added ability for admin to see others people projects

Blue Mouse3911c024c9f3
diff --git a/src/api/index.js b/src/api/index.js
index 3424545c0..ffb1712ab 100644
--- a/src/api/index.js
+++ b/src/api/index.js
@@ -153,7 +153,16 @@ export const verifyEmail = ({ path, id }) =>
export const getProjects = () =>
api
- .get(`/project`)
+ .get('/project')
+ .then(response => response.data)
+ .catch(error => {
+ debug('%s', error)
+ throw _isEmpty(error.response.data?.message) ? error.response.data : error.response.data.message
+ })
+
+export const getProject = (pid) =>
+ api
+ .get(`/project/${pid}`)
.then(response => response.data)
.catch(error => {
debug('%s', error)
diff --git a/src/pages/Dashboard/Dashboard.jsx b/src/pages/Dashboard/Dashboard.jsx
index f8760c4c8..81c737d3f 100644
--- a/src/pages/Dashboard/Dashboard.jsx
+++ b/src/pages/Dashboard/Dashboard.jsx
@@ -20,7 +20,7 @@ import { ActivePin, InactivePin } from 'ui/Pin'
import routes from 'routes'
const ProjectCart = ({ name, url, created, active, overall }) => {
- const statsDidGrowUp = overall.percChange >= 0
+ const statsDidGrowUp = overall?.percChange >= 0
return (
<li>
@@ -45,7 +45,7 @@ const ProjectCart = ({ name, url, created, active, overall }) => {
Pageviews:&nbsp;
<dd className='flex items-baseline'>
<p className='h-5 mr-1 text-gray-500'>
- {overall.thisWeek}
+ {overall?.thisWeek}
</p>
<p className={cx('flex text-xs -ml-1 items-baseline', {
'text-green-600': statsDidGrowUp,
@@ -66,7 +66,7 @@ const ProjectCart = ({ name, url, created, active, overall }) => {
</span>
</>
)}
- {overall.percChange}%
+ {overall?.percChange}%
</p>
</dd>
</div>
diff --git a/src/pages/Project/View/ViewProject.container.jsx b/src/pages/Project/View/ViewProject.container.jsx
index 0f3a17909..e6ca356ec 100644
--- a/src/pages/Project/View/ViewProject.container.jsx
+++ b/src/pages/Project/View/ViewProject.container.jsx
@@ -8,6 +8,7 @@ const mapStateToProps = (state) => ({
isLoading: state.ui.projects.isLoading,
cache: state.ui.cache.analytics,
projectViewPrefs: state.ui.cache.projectViewPrefs,
+ user: state.auth.user,
})
const mapDispatchToProps = (dispatch) => ({
@@ -20,6 +21,9 @@ const mapDispatchToProps = (dispatch) => ({
setProjectViewPrefs: (pid, period, timeBucket) => {
dispatch(UIActions.setProjectViewPrefs(pid, period, timeBucket))
},
+ setProject: (project) => {
+ dispatch(UIActions.setProject(project))
+ },
})
export default connect(mapStateToProps, mapDispatchToProps)(ViewProject)
diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsx
index 75ed0d688..b00bffbfb 100644
--- a/src/pages/Project/View/ViewProject.jsx
+++ b/src/pages/Project/View/ViewProject.jsx
@@ -22,7 +22,8 @@ import Title from 'components/Title'
import {
tbPeriodPairs, tbsFormatMapper, getProjectCacheKey,
} from 'redux/constants'
-import { isAuthenticated } from '../../../hoc/protected'
+import { isAuthenticated } from 'hoc/protected'
+import { isAdmin } from 'utils/validator'
import Button from 'ui/Button'
import Loader from 'ui/Loader'
import Dropdown from 'ui/Dropdown'
@@ -31,7 +32,7 @@ import {
Panel, Overview, CustomEvents,
} from './Panels'
import routes from 'routes'
-import { getProjectData } from 'api'
+import { getProjectData, getProject } from 'api'
countries.registerLocale(countriesEn)
@@ -135,7 +136,7 @@ const NoEvents = () => (
)
const ViewProject = ({
- projects, isLoading, showError, cache, setProjectCache, projectViewPrefs, setProjectViewPrefs,
+ projects, isLoading, showError, cache, setProjectCache, projectViewPrefs, setProjectViewPrefs, user, setProject,
}) => {
const dashboardRef = useRef(null)
const { id } = useParams()
@@ -233,8 +234,14 @@ const ViewProject = ({
}
if (!isLoading && _isEmpty(project)) {
- showError('The selected project does not exist')
- history.push(routes.dashboard)
+ if (isAdmin(user)) {
+ getProject(id)
+ .then(setProject)
+ .catch(console.error)
+ } else {
+ showError('The selected project does not exist')
+ history.push(routes.dashboard)
+ }
}
const openSettingsHandler = () => {
@@ -385,6 +392,8 @@ ViewProject.propTypes = {
setProjectCache: PropTypes.func.isRequired,
setProjectViewPrefs: PropTypes.func.isRequired,
isLoading: PropTypes.bool.isRequired,
+ user: PropTypes.object.isRequired,
+ setProject: PropTypes.func.isRequired,
}
export default isAuthenticated(memo(ViewProject))
diff --git a/src/redux/actions/ui/index.js b/src/redux/actions/ui/index.js
index 2bd0fc164..c67fa610a 100644
--- a/src/redux/actions/ui/index.js
+++ b/src/redux/actions/ui/index.js
@@ -11,6 +11,13 @@ const setProjects = (projects) => ({
},
})
+const setProject = (project) => ({
+ type: types.SET_PROJECT,
+ payload: {
+ project,
+ },
+})
+
const removeProject = (pid) => ({
type: types.REMOVE_PROJECT,
payload: {
@@ -62,6 +69,7 @@ const UIActions = {
setProjectCache,
deleteProjectCache,
setProjectViewPrefs,
+ setProject,
}
export default UIActions
diff --git a/src/redux/actions/ui/types.js b/src/redux/actions/ui/types.js
index 45930a8df..107f70ee8 100644
--- a/src/redux/actions/ui/types.js
+++ b/src/redux/actions/ui/types.js
@@ -7,4 +7,5 @@ export const types = {
DELETE_PROJECT_CACHE: 'DELETE_PROJECT_CACHE',
SET_PROJECT_VIEW_PREFS: 'SET_PROJECT_VIEW_PREFS',
SET_PROJECTS: 'SET_PROJECTS',
+ SET_PROJECT: 'SET_PROJECT',
}
diff --git a/src/redux/reducers/ui/projects.js b/src/redux/reducers/ui/projects.js
index ade41ad71..9869c6ece 100644
--- a/src/redux/reducers/ui/projects.js
+++ b/src/redux/reducers/ui/projects.js
@@ -1,5 +1,6 @@
import { types } from 'redux/actions/ui/types'
import _filter from 'lodash/filter'
+import _findIndex from 'lodash/findIndex'
const getInitialState = () => {
return {
@@ -20,6 +21,14 @@ const projectsReducer = (state = getInitialState(), { type, payload }) => {
}
}
+ case types.SET_PROJECT: {
+ const { project } = payload
+ return {
+ ...state,
+ projects: _findIndex(state.projects, (el) => el.id === project.id) >= 0 ? state.projects : [...state.projects, project],
+ }
+ }
+
case types.SET_PROJECTS_ERROR: {
const { error } = payload
return {
diff --git a/src/utils/validator.js b/src/utils/validator.js
index 2556cd2f8..f3bf59af1 100644
--- a/src/utils/validator.js
+++ b/src/utils/validator.js
@@ -1,3 +1,7 @@
+import _includes from 'lodash/includes'
+
export const isValidEmail = text => text.match(/^\w+([.+-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,7})+$/)
export const isValidPassword = text => text.length >= 8
+
+export const isAdmin = user => _includes(user.roles, 'admin')