Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/actions/adminAction.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
import axios from 'axios'
import { errorHandler } from '../utils/errorHandler'
import { setRequestStatus } from '../utils/setRequestStatus'
import { SET_ADMIN } from './types'
import { SET_ADMIN, GET_ADMIN } from './types'
import { setAuthToken } from '../utils/setAuthToken'
import jwt_decode from 'jwt-decode';
import { setCurrentUser } from './authAction'


export const createAdmin = (adminInfo) => async (dispatch) => {
try {
const res = await axios.post('/user/', adminInfo)
setRequestStatus(false)
if (res.status === 201) {
setRequestStatus(true)
dispatch({
type: GET_ADMIN,
payload: res.data.user
})
}
} catch (error) {
dispatch(errorHandler(error))
}
}

export const loginAdmin = (adminInfo) => async (dispatch) => {
export const loginAdmin = (adminInfo, history) => async (dispatch) => {
try {
const res = await axios.post('/auth/login/', adminInfo)
dispatch(setRequestStatus(false))
if (res.status === 200) {
dispatch(setRequestStatus(true))
localStorage.setItem('admin', true)
dispatch({
type: SET_ADMIN,
payload: true
})
}
dispatch(setRequestStatus(false));
if (res.status === 200) {

const token = res.data.token;
dispatch(setRequestStatus(true));

localStorage.setItem("jwtToken", JSON.stringify(token));
setAuthToken(token);

// update state with user
const decodedData = await jwt_decode(token);
localStorage.setItem('userId', decodedData._id)
dispatch(setCurrentUser(decodedData));

dispatch({
type: SET_ADMIN,
payload: true
})

history.push("/dashboard");
}
} catch (error) {
dispatch(errorHandler(error))
}
Expand Down
18 changes: 15 additions & 3 deletions src/actions/authAction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SET_CURRENT_USER } from './types';
import { SET_CURRENT_USER, GET_USER_PROFILE, PASSWORD_SUCCESSFULLY_CHANGED, PASSWORD_CHANGE_REQUEST_SUCCESS } from './types';
import axios from 'axios';
import { setAuthToken } from '../utils/setAuthToken';
import jwt_decode from 'jwt-decode';
Expand All @@ -14,6 +14,10 @@ export const registerUser = (userInfo, history) => async (dispatch) => {

if(res.status === 201) {
dispatch(setRequestStatus(true));
dispatch({
type: GET_USER_PROFILE,
payload: res.data.user
})
history.push('/');
}

Expand Down Expand Up @@ -53,13 +57,17 @@ export const loginUser = (userInfo, history) => async (dispatch) => {
// forgot password
export const forgotPassword = (email) => async (dispatch) => {
try {
const res = await axios.post('/user/password_reset', email);
const res = await axios.patch('/user/password_reset/request/', email);
dispatch(setRequestStatus(false));

if(res.status === 200){
dispatch(setRequestStatus(true));
console.log("Forgot password request sent!!");
forgotPasswordToken = res.data.token;
dispatch({
type: PASSWORD_CHANGE_REQUEST_SUCCESS,
payload: res.data.token
})
}

} catch (error) {
Expand All @@ -70,13 +78,17 @@ export const forgotPassword = (email) => async (dispatch) => {
// update password
export const changePassword = (passObj) => async (dispatch) => {
try {
const res = await axios.post(`/user/password_reset/${forgotPasswordToken}`, passObj);
const res = await axios.patch(`/user/password_reset/${forgotPasswordToken}`, passObj);
dispatch(setRequestStatus(false));

if(res.status === 200){
dispatch(setRequestStatus(true));
console.log("Password updated!", res.data);
// show password updated notification from here
dispatch({
type: PASSWORD_SUCCESSFULLY_CHANGED,
payload: res.data.updated
})
}

} catch(error) {
Expand Down
67 changes: 67 additions & 0 deletions src/actions/commentAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { GET_COMMENTS_OF_A_POST } from './types'
import { errorHandler } from '../utils/errorHandler'
import axios from 'axios'
import { setRequestStatus } from '../utils/setRequestStatus'

// CREATE COMMENT ON A PARTICULAR POST
export const createComment = (postId, comment) => async (dispatch) => {
try {
const res = await axios.post(`/comment/${postId}`, comment)
dispatch(setRequestStatus(false));
if(res.status === 201) {
dispatch(setRequestStatus(true))
console.log('created comment ', res.data.comment)
dispatch(getAllCommentsOfPost());
}
} catch(error) {
dispatch(errorHandler(error))
}
}

// GET ALL COMMENTS OF A POST
export const getAllCommentsOfPost = (postId) => async (dispatch) => {
try {
const res = await axios.get(`/comment/${postId}`)
dispatch(setRequestStatus(false))
if(res.status === 200) {
dispatch(setRequestStatus(true));
console.log('fetching comments of ', postId, res.data.comments);
dispatch({
type: GET_COMMENTS_OF_A_POST,
payload: res.data.comments
})
}
} catch(error) {
dispatch(errorHandler(error))
}
}

// UPDATE COMMENT OF A POST
export const updateComment = (commentId, updatedComment) => async (dispatch) => {
try {
const res = await axios.patch(`/comment/${commentId}`, updatedComment)
dispatch(setRequestStatus(false))
if(res.status === 200) {
dispatch(setRequestStatus(true))
console.log('comment updated ', res.data.comment)
dispatch(getAllCommentsOfPost())
}
} catch(error) {
errorHandler(error)
}
}

// DELETE COMMENT
export const deleteComment = (commentId) => async (dispatch) => {
try {
const res = await axios.delete(`/comment/${commentId}`)
dispatch(setRequestStatus(false))
if(res.status === 200) {
dispatch(setRequestStatus(true));
console.log('comment deleted ', res.data)
dispatch(getAllCommentsOfPost())
}
} catch(error) {
dispatch(errorHandler(error))
}
}
6 changes: 6 additions & 0 deletions src/actions/dashboardAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import axios from 'axios'
import { setRequestStatus } from '../utils/setRequestStatus'
import { errorHandler } from '../utils/errorHandler'
import { GET_ALL_UPCOMING_EVENTS } from './types'
import { getAllEvents } from './eventAction'
import { getAllPosts } from './postAction'
import { getAllProjects } from './projectAction'

// GET UPCOMING EVENTS
export const upcomingEvents = () => async (dispatch) => {
Expand Down Expand Up @@ -29,6 +32,7 @@ export const createPost = (postInfo) => async (dispatch) => {
if (res.status === 201) {
dispatch(setRequestStatus(true))
console.log('post created ', res.data)
dispatch(getAllPosts())
}
} catch (error) {
dispatch(errorHandler(error))
Expand All @@ -43,6 +47,7 @@ export const createEvent = (eventInfo) => async (dispatch) => {
if (res.status === 201) {
dispatch(setRequestStatus(true))
console.log('event created ', res.data)
dispatch(getAllEvents())
}
} catch (error) {
dispatch(errorHandler(error))
Expand All @@ -58,6 +63,7 @@ export const createProject = (projectInfo) => async (dispatch) => {
if (res.status === 201) {
dispatch(setRequestStatus(true))
console.log('project created ', res.data)
dispatch(getAllProjects())
}
} catch (error) {
dispatch(errorHandler(error))
Expand Down
23 changes: 22 additions & 1 deletion src/actions/eventAction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { errorHandler } from '../utils/errorHandler';
import { setRequestStatus } from '../utils/setRequestStatus';
import { GET_ALL_EVENTS } from './types';
import { GET_ALL_EVENTS, GET_EVENT_BY_ID } from './types';

// DELETE EVENT REQUEST
export const deleteEvent = (eventId) => async (dispatch) => {
Expand All @@ -10,6 +10,7 @@ export const deleteEvent = (eventId) => async (dispatch) => {
dispatch(setRequestStatus(false));
if(res.status === 200){
dispatch(setRequestStatus(true));
dispatch(getAllEvents())
}
} catch(error) {
dispatch(errorHandler(error))
Expand All @@ -23,6 +24,7 @@ export const updateEvent = (eventId, updatedInfo) => async (dispatch) => {
dispatch(setRequestStatus(false));
if(res.status === 200){
dispatch(setRequestStatus(true));
dispatch(getAllEvents())
}
} catch(error) {
dispatch(errorHandler(error))
Expand Down Expand Up @@ -60,3 +62,22 @@ export const getAllEvents = (pagination = 10, page = 1) => async (dispatch) => {
dispatch(errorHandler(error))
}
}

// GET EVENT BY ID
export const getEventById = (eventId) => async (dispatch) => {
try {
console.log('fetching event ', eventId)
const res = await axios.get(`/event/${eventId}`)
dispatch(setRequestStatus(false))
if(res.status === 200){
dispatch(setRequestStatus(true))
console.log('fetching event by id ', res.data.event)
dispatch({
type: GET_EVENT_BY_ID,
payload: res.data.event
})
}
} catch(error) {
dispatch(errorHandler(error))
}
}
11 changes: 7 additions & 4 deletions src/actions/orgAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const registerCommunity = (orgInfo) => async (dispatch) => {
if (res.status === 201) {
dispatch(setRequestStatus(true))
localStorage.setItem('orgId', JSON.stringify(res.data.org._id))
dispatch(getOrgProfile())
}
} catch (error) {
dispatch(errorHandler(error))
Expand Down Expand Up @@ -61,15 +62,17 @@ export const getOrgProfile = () => async (dispatch) => {
export const updateOrgProfile = (updatedInfo) => async (dispatch) => {
try {
let orgId = localStorage.getItem('orgId')
console.log('updatedInfo ', updatedInfo);
const res = await axios.patch(`/org/${orgId}`, updatedInfo)
dispatch(setRequestStatus(false));
if(res.status === 200) {
dispatch(setRequestStatus(true))
console.log('org profile updated!', res.data)
dispatch({
type: UPDATE_ORG_PROFILE,
payload: res.data.organization
})
dispatch(getOrgProfile());
// dispatch({
// type: UPDATE_ORG_PROFILE,
// payload: res.data.organization
// })
}
} catch(error) {
dispatch(errorHandler(error))
Expand Down
19 changes: 18 additions & 1 deletion src/actions/postAction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { errorHandler } from '../utils/errorHandler';
import { setRequestStatus } from '../utils/setRequestStatus';
import { GET_ALL_POSTS } from './types';
import { GET_ALL_POSTS, GET_ALL_PINNED_POSTS } from './types';

// GET ALL POSTS
export const getAllPosts = (pagination = 10, page = 1) => async (dispatch) => {
Expand All @@ -21,3 +21,20 @@ export const getAllPosts = (pagination = 10, page = 1) => async (dispatch) => {
}
}

// GET ALL PINNED POSTS
export const getAllPinnedPosts = (pagination = 10, page = 1) => async (dispatch) => {
try {
const res = await axios.get(`/post/all/pinned?pagination=${pagination}&page=${page}`)
dispatch(setRequestStatus(false))
if(res.status === 200){
dispatch(setRequestStatus(true))
console.log('fetching all pinned posts ', res.data.pinnedPost)
dispatch({
type: GET_ALL_PINNED_POSTS,
payload: res.data.pinnedPost
})
}
} catch(error) {
dispatch(errorHandler(error))
}
}
56 changes: 54 additions & 2 deletions src/actions/projectAction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { errorHandler } from '../utils/errorHandler';
import { setRequestStatus } from '../utils/setRequestStatus';
import { GET_ALL_PROJECTS } from './types';
import { GET_ALL_PROJECTS, GET_SINGLE_PROJECT } from './types';

// CREATE PROJECT
export const createProject = (projectInfo) => async (dispatch) => {
Expand Down Expand Up @@ -34,4 +34,56 @@ export const getAllProjects = (pagination = 10, page = 1) => async (dispatch) =>
} catch(error) {
dispatch(errorHandler(error))
}
}
}

// GET PROJECT BY ID
export const getProjectById = (projectId) => async (dispatch) => {
try {
const res = await axios.get(`/project/${projectId}`)
dispatch(setRequestStatus(false))
if(res.status === 200) {
dispatch(setRequestStatus(true));
dispatch({
type: GET_SINGLE_PROJECT,
payload: res.data.project
})
}
} catch(error) {
dispatch(errorHandler(error))
}
}

// UPDATE PROJECT
export const updateProject = (projectId, updatedInfo) => async (dispatch) => {
try {
const res = await axios.patch(`/project/${projectId}`, updatedInfo)
dispatch(setRequestStatus(false))
if(res.status === 200) {
dispatch(setRequestStatus(true));
console.log('updated project info ', res.data)
dispatch({
type: GET_SINGLE_PROJECT,
payload: res.data.project
})
}
} catch(error) {
dispatch(errorHandler(error))
}
}

// DELETE PROJECT BY ID
export const deleteProjectById = (projectId, history) => async (dispatch) => {
try {
const res = await axios.delete(`/project/${projectId}`)
dispatch(setRequestStatus(false))
if(res.status === 200) {
dispatch(setRequestStatus(true))
console.log('Project deleted', res.data.msg);
// window.location.href = '/projects'
// dispatch(getAllProjects())
history.push('/projects');
}
} catch(error) {
dispatch(errorHandler(error))
}
}
Loading