From e6a13b9a339fee1cf12de454213d6d175cc27d3a Mon Sep 17 00:00:00 2001 From: RTEYL Date: Wed, 14 Apr 2021 20:39:21 -0700 Subject: [PATCH 1/6] fix typo in user schema --- models/User.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/User.js b/models/User.js index 567aae9..6e5c191 100644 --- a/models/User.js +++ b/models/User.js @@ -8,7 +8,7 @@ const UserSchema = new mongoose.Schema( email: { type: String, }, - image: { + img: { type: String, }, groups: [ From 8bc4c242f5eb0c95ce18f66cd989ed97d92a350e Mon Sep 17 00:00:00 2001 From: RTEYL Date: Wed, 14 Apr 2021 20:40:13 -0700 Subject: [PATCH 2/6] moved user logic to the action creator --- pages/api/groups/[id].js | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/pages/api/groups/[id].js b/pages/api/groups/[id].js index 75b7325..a285bac 100644 --- a/pages/api/groups/[id].js +++ b/pages/api/groups/[id].js @@ -46,31 +46,17 @@ export default async function handler(req, res) { case 'PUT': Group.findByIdAndUpdate( id, - { - name: body.name, - owner: [body.ownerId], - image: body.img, - projectDescription: body.projectDescription, - }, + body, { new: true, runValidators: true }, (err, group) => { if (err) res.status(500).json({ err, - message: 'server could not find group by id', + message: 'server could not find/update group by id', }); - return group; + res.status(200).json(group); } ); - group.users.push(body.userId); - group.save({}, (err, group) => { - if (err) - res.status(500).json({ - err, - message: 'server could not update users', - }); - res.status(200).json(group); - }); break; From 60f3405c2fa77d12514498a4d2083074ad0a1df3 Mon Sep 17 00:00:00 2001 From: RTEYL Date: Wed, 14 Apr 2021 20:40:54 -0700 Subject: [PATCH 3/6] add actions to add/remove/transfer groups --- store/users/actions.js | 58 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/store/users/actions.js b/store/users/actions.js index 8a9f889..28722f6 100644 --- a/store/users/actions.js +++ b/store/users/actions.js @@ -1,7 +1,11 @@ +import { deleteGroupById, updateGroup } from '../groups/actions'; + export const userActionsTypes = { - ALL: 'ALL', - UPDATE: 'UPDATE', - DELETE_BY_ID: 'DELETE_BY_ID', + ALL_USERS: 'ALL_USERS', + UPDATE_USER: 'UPDATE', + FIND_USER_BY_ID: 'FIND_USER_BY_ID', + DELETE_USER_BY_ID: 'DELETE_BY_ID', + ADD_GROUP: 'ADD_GROUP', }; export const getUsers = () => { @@ -15,7 +19,7 @@ export const getUsers = () => { }) .then((res) => res.json()) .then((users) => { - dispatch({ type: userActionsTypes.ALL, users }); + dispatch({ type: userActionsTypes.ALL_USERS, users }); }) .catch((err) => console.log('-----------------ERROR: ', err)); }; @@ -33,7 +37,7 @@ export const updateUser = (user) => { }) .then((res) => res.json()) .then((user) => { - dispatch({ type: userActionsTypes.UPDATE, user }); + dispatch({ type: userActionsTypes.UPDATE_USER, user }); }); }; }; @@ -54,7 +58,7 @@ export const findUserById = (id) => { }) .then((res) => res.json()) .then((user) => { - dispatch({ type: userActionsTypes.FIND_BY_ID, user }); + dispatch({ type: userActionsTypes.FIND_USER_BY_ID, user }); }); }; }; @@ -69,7 +73,47 @@ export const deleteUserById = (id) => { }) .then((res) => res.json()) .then((id) => { - dispatch({ type: userActionsTypes.DELETE_BY_ID, id }); + dispatch({ type: userActionsTypes.DELETE_USER_BY_ID, id }); }); }; }; + +export const addUserToGroup = (group, user) => { + user.groups.push(group._id); + group.users.push(user._id); + + return (dispatch) => { + dispatch(updateUser(user)); + dispatch(updateGroup(group)); + }; +}; + +export const removeUserFromGroup = (group, user) => { + user.groups.filter((u) => u._id !== user._id); + group.users.filter((g) => g._id !== group._id); + + return (dispatch) => { + dispatch(updateUser(user)); + dispatch(updateGroup(group)); + }; +}; + +export const deleteUserOwnedGroup = (group, user) => { + user.ownedGroups.filter((u) => u._id !== user._id); + return (dispatch) => { + dispatch(updateUser(user)); + dispatch(deleteGroupById(group._id)); + }; +}; + +export const tansferGroupOwnership = (owner, group, user) => { + owner.ownedGroups.filter((g) => g._id !== group._id); + group.owner = user._id; + user.ownedGroups.push(group._id); + + return (dispatch) => { + dispatch(updateUser(user)); + dispatch(updateUser(owner)); + dispatch(updateGroup(group)); + }; +}; From a7b0d38173b3d734e6595c973f4445c1960a02c2 Mon Sep 17 00:00:00 2001 From: RTEYL Date: Wed, 14 Apr 2021 20:41:29 -0700 Subject: [PATCH 4/6] update action types for potential name conflicts --- store/users/reducer.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/store/users/reducer.js b/store/users/reducer.js index 462b3a9..a3d948a 100644 --- a/store/users/reducer.js +++ b/store/users/reducer.js @@ -6,11 +6,11 @@ const initialState = { export default function reducer(state = initialState, action) { switch (action.type) { - case userActionsTypes.ALL: + case userActionsTypes.ALL_USERS: return { ...state, users: action.users }; break; - case userActionsTypes.UPDATE: + case userActionsTypes.UPDATE_USER: let updateIndex = state.users.findIndex( (user) => user._id === action.user._id ); @@ -24,11 +24,11 @@ export default function reducer(state = initialState, action) { }; break; - case userActionsTypes.FIND_BY_ID: + case userActionsTypes.FIND_USER_BY_ID: return { ...state, users: state.users.concat(action.user) }; break; - case userActionsTypes.DELETE_BY_ID: + case userActionsTypes.DELETE_USER_BY_ID: return { ...state, users: [...state.users.filter((user) => user._id !== action.id)], From 79ea535dfe0eaae8a60df7db42158b342bf7ec5b Mon Sep 17 00:00:00 2001 From: RTEYL Date: Wed, 14 Apr 2021 20:59:58 -0700 Subject: [PATCH 5/6] move user logic on update to action creator --- pages/api/groups/index.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/pages/api/groups/index.js b/pages/api/groups/index.js index 2c8d83e..910be5f 100644 --- a/pages/api/groups/index.js +++ b/pages/api/groups/index.js @@ -36,14 +36,7 @@ export default async function handler(req, res) { }); break; case 'POST': - let newGroup = await new Group({ - name: body.name, - owner: [body.ownerId], - img: body.img, - projectDescription: body.projectDescription, - }); - newGroup.users.push(body.ownerId); - await newGroup.save(); + let newGroup = await new Group(body); Group.aggregate([ { $match: { _id: mongoose.Types.ObjectId(newGroup._id) } }, { From 1614a339877180d13879b89fbbdf5ca284083bbe Mon Sep 17 00:00:00 2001 From: RTEYL Date: Wed, 14 Apr 2021 21:07:03 -0700 Subject: [PATCH 6/6] update action types ref: a7b0d38173b3d734e6595c973f4445c1960a02c2 --- store/groups/actions.js | 88 ++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 37 deletions(-) diff --git a/store/groups/actions.js b/store/groups/actions.js index d8fea1d..3f4b165 100644 --- a/store/groups/actions.js +++ b/store/groups/actions.js @@ -1,8 +1,10 @@ +import { updateUser } from '../users/actions'; + export const groupActionsTypes = { - ALL: 'ALL', - CREATE: 'CREATE', - UPDATE: 'UPDATE', - DELETE_BY_ID: 'DELETE_BY_ID', + ALL_GROUPS: 'ALL_GROUPS', + CREATE_GROUP: 'CREATE_GROUP', + UPDATE_GROUP: 'UPDATE_GROUP', + DELETE_GROUP_BY_ID: 'DELETE_GROUP_BY_ID', }; export const getGroups = () => { @@ -16,13 +18,17 @@ export const getGroups = () => { }) .then((res) => res.json()) .then((groups) => { - dispatch({ type: groupActionsTypes.ALL, groups }); + dispatch({ type: groupActionsTypes.ALL_GROUPS, groups }); }) .catch((err) => console.log('-----------------ERROR: ', err)); }; }; -export const createGroup = (group) => { +export const createGroup = (group, user) => { + group.owner = user._id; + group.users.push(user._id); + user.ownedGroups.push(group._id); + user.groups.push(group._id); return (dispatch) => { fetch('http://localhost:3000/api/groups', { method: 'POST', @@ -34,43 +40,50 @@ export const createGroup = (group) => { }) .then((res) => res.json()) .then((group) => { - dispatch({ type: groupActionsTypes.CREATE, group }); - }); + dispatch({ type: groupActionsTypes.CREATE_GROUP, group }); + dispatch(updateUser(user)); + }) + .catch((err) => console.log('-----------------ERROR: ', err)); }; }; -export const updateGroup = (group) => (dispatch) => { - fetch(`http://localhost:3000/api/groups/${group._id}`, { - method: 'PUT', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(group), - }) - .then((res) => res.json()) - .then((group) => { - dispatch({ type: groupActionsTypes.UPDATE, group }); - }); +export const updateGroup = (group) => { + return (dispatch) => { + fetch(`http://localhost:3000/api/groups/${group._id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(group), + }) + .then((res) => res.json()) + .then((group) => { + dispatch({ type: groupActionsTypes.UPDATE_GROUP, group }); + }) + .catch((err) => console.log('-----------------ERROR: ', err)); + }; }; -export const findGroupById = (id) => (dispatch, getState) => { - const groups = getState().groups.groups; - const group = groups.filter((group) => group._id === id); +export const findGroupById = (id) => { + return (dispatch, getState) => { + let groups = getState().groups.groups; + let group = groups.filter((group) => group._id === id); - if (group) return group; + if (group) return group; - fetch(`http://localhost:3000/api/groups/${id}`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - Accept: 'application/json', - }, - }) - .then((res) => res.json()) - .then((group) => { - dispatch({ type: groupActionsTypes.CREATE, group }); + fetch(`http://localhost:3000/api/groups/${id}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + }, }) - .catch((err) => console.log('-----------------ERROR: ', err)); + .then((res) => res.json()) + .then((group) => { + dispatch({ type: groupActionsTypes.CREATE_GROUP, group }); + }) + .catch((err) => console.log('-----------------ERROR: ', err)); + }; }; export const deleteGroupById = (id) => { @@ -83,7 +96,8 @@ export const deleteGroupById = (id) => { }) .then((res) => res.json()) .then((id) => { - dispatch({ type: groupActionsTypes.DELETE_BY_ID, id }); - }); + dispatch({ type: groupActionsTypes.DELETE_GROUP_BY_ID, id }); + }) + .catch((err) => console.log('-----------------ERROR: ', err)); }; };