Skip to content

Commit

Permalink
Update and FIxes
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsinht committed May 11, 2020
1 parent 51a7d0c commit be3e8a9
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 6 deletions.
71 changes: 69 additions & 2 deletions admin/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ const request = require('request');
const config = require('config');
const UserActivity = require('../models/UserActivity');
const User = require('../models/User');
const Task = require('../models/Task');

// @route GET api/admin/activities
// @desc Get all activities
// @access Private
// @access Admin

router.get('/activities', auth, async (req, res) => {
try {
if (!req.user.isAdmin) {
return res.json({ msg: 'you are not admin!' });
}
const user_obj = await User.findById(req.user.id);
if (!user_obj && user_obj.accessLevel !== '2') {
return res.json({ msg: 'you are not admin!' });
}
const acts = await UserActivity.aggregate([
{
$match: {
Expand Down Expand Up @@ -69,13 +74,17 @@ router.get('/activities', auth, async (req, res) => {

// @route GET api/admin/users
// @desc Get all users
// @access Private
// @access Admin

router.get('/users', auth, async (req, res) => {
try {
if (!req.user.isAdmin) {
return res.json({ msg: 'you are not admin!' });
}
const user_obj = await User.findById(req.user.id);
if (!user_obj && user_obj.accessLevel !== '2') {
return res.json({ msg: 'you are not admin!' });
}
const users = await User.aggregate([
{
$match: {
Expand Down Expand Up @@ -128,4 +137,62 @@ router.get('/users', auth, async (req, res) => {
}
});

// @route GET api/admin/users
// @desc Get all users
// @access Admin

router.get('/tasks', auth, async (req, res) => {
try {
if (!req.user.isAdmin) {
return res.json({ msg: 'you are not admin!' });
}
const user_obj = await User.findById(req.user.id);
if (!user_obj && user_obj.accessLevel !== '2') {
return res.json({ msg: 'you are not admin!' });
}
const users = await Task.aggregate([
{
$lookup: {
from: 'personaldetails',
localField: 'user',
foreignField: 'user',
as: 'user_details',
},
},
{
$lookup: {
from: 'proposals',
localField: '_id',
foreignField: 'task',
as: 'proposals',
},
},
{
$sort: {
updatedAt: -1,
},
},

{
$project: {
headline: 1,
taskpoints: 1,
state: 1,
user_details: {
first_name: 1,
second_name: 1,
},
description: 1,
proposals_received: { $size: '$proposals' },
createdAt: 1,
updatedAt: 1,
},
},
]);
res.json(users);
} catch (err) {
return res.status(400).json({ msg: 'Could not find activities.' });
}
});

module.exports = router;
3 changes: 2 additions & 1 deletion client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import Work from './components/Work/Work';
import Admin from './components/admin/Admin';
import AdminUsers from './components/admin/AdminUsers';
import AdminActivities from './components/admin/AdminActivities';

import AdminTasks from './components/admin/AdminTasks';
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import './style/header.css';
Expand Down Expand Up @@ -117,6 +117,7 @@ class App extends Component {
path='/admin/activity'
component={AdminActivities}
/>
<AdminRoute exact path='/admin/tasks' component={AdminTasks} />
<Route path='*' component={NotFound404} />
</Switch>

Expand Down
18 changes: 18 additions & 0 deletions client/src/actions/adminActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
RESET_SOCKET_CONNECTION,
ADMIN_ACTIVITIES,
ADMIN_USERS,
ADMIN_TASKS,
} from './types';

export const fetchActivities = () => async (dispatch) => {
Expand Down Expand Up @@ -46,3 +47,20 @@ export const fetchUsers = () => async (dispatch) => {
dispatch(addToast('Oops! Some error has occurred! ' + err.message));
}
};

export const fetchTasks = () => async (dispatch) => {
try {
const mtok = localStorage.jwtToken;
if (mtok) {
setAuthToken(mtok);
}
const res = await axios.get('/api/admin/tasks');
dispatch({
type: ADMIN_TASKS,
payload: res.data,
});
return res.data;
} catch (err) {
dispatch(addToast('Oops! Some error has occurred! ' + err.message));
}
};
1 change: 1 addition & 0 deletions client/src/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ export const ALL_NOTIFICATIONS_READ = 'ALL_NOTIFICATIONS_READ';

export const ADMIN_ACTIVITIES = 'ADMIN_ACTIVITIES';
export const ADMIN_USERS = 'ADMIN_USERS';
export const ADMIN_TASKS = 'ADMIN_TASKS';
86 changes: 86 additions & 0 deletions client/src/components/admin/AdminTasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React, { Component } from 'react';
import AdminNav from './subs/AdminNav';
import { fetchTasks } from '../../actions/adminActions';
import { connect } from 'react-redux';
import moment from 'moment';
import TaskState from '../task/subs/TaskState';

class AdminTasks extends Component {
componentDidMount() {
this.props.fetchTasks();
}
render() {
return (
<div>
<AdminNav selected={3} />
<div className='container explore-container' id='explore-container'>
<div className='task-list-section'>
<div className='task-list-title'>
Last Updated Tasks{' '}
<button
onClick={this.props.fetchTasks}
className='btn notification-btn fl-r'
>
Refresh Data
</button>
</div>
<div>
<hr />
<ol>
{this.props.tasks && !this.props.tasks.msg
? this.props.tasks.map((task, id) => {
return (
<li key={id}>
<strong>{task.headline}</strong>
<TaskState state={task.state} />
<div style={{ fontSize: 10 }}>
(From{' '}
{task.user_details.length ? (
<span>
{task.user_details[0].first_name}{' '}
{task.user_details[0].second_name}
</span>
) : (
'General'
)}
)
</div>{' '}
<div style={{ fontSize: 10 }} className='mt-2'>
Last Updated: {new Date(task.updatedAt).toString()}
<br />
Created: {new Date(task.createdAt).toString()}
</div>
<div className='mb-4 mt-2 ql-editor dt-description'>
<div>
Task Points: {task.taskpoints} <br />
Proposals Received: {task.proposals_received}{' '}
</div>
<div className='mb-4 mt-2 ql-editor dt-description'>
<div
dangerouslySetInnerHTML={{
__html: task.description,
}}
></div>
</div>
</div>
<hr />
</li>
);
})
: 'you are not an admin'}
</ol>
</div>
</div>
</div>
</div>
);
}
}

const mapStateToProps = (state) => ({
tasks: state.admin_data.tasks,
});

export default connect(mapStateToProps, {
fetchTasks,
})(AdminTasks);
10 changes: 9 additions & 1 deletion client/src/components/admin/subs/AdminNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ const AdminNav = (props) => {
</button>
</Link>
<Link to='/admin/activity'>
<button className='btn btn-primary' disabled={props.selected === 2}>
<button
className='btn btn-primary mr-2'
disabled={props.selected === 2}
>
View Users Activity
</button>
</Link>
<Link to='/admin/tasks'>
<button className='btn btn-primary' disabled={props.selected === 3}>
View Tasks
</button>
</Link>
</div>
</div>
);
Expand Down
7 changes: 7 additions & 0 deletions client/src/reducers/adminReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
GET_ERRORS,
ADMIN_ACTIVITIES,
ADMIN_USERS,
ADMIN_TASKS,
} from '../actions/types';

const initialState = {
activities: [],
users: [],
tasks: [],
};

export default function (state = initialState, action) {
Expand All @@ -23,6 +25,11 @@ export default function (state = initialState, action) {
...state,
users: action.payload,
};
case ADMIN_TASKS:
return {
...state,
tasks: action.payload,
};
default:
return state;
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/style/inc/bookmark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion client/src/style/inc/share.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions client/src/style/task.css
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@
-ms-flex: 1;
flex: 1;
margin-right: 0.75rem;
align-items: center;
}

.feed-card--footer-right {
Expand Down Expand Up @@ -434,6 +435,7 @@

.feed-card--footer-left .svg_icon {
width: 16px;
height: 16px;
cursor: pointer;
}

Expand Down

0 comments on commit be3e8a9

Please sign in to comment.