Skip to content
Open
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
2 changes: 1 addition & 1 deletion models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const UserSchema = new mongoose.Schema(
email: {
type: String,
},
image: {
img: {
type: String,
},
groups: [
Expand Down
20 changes: 3 additions & 17 deletions pages/api/groups/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
58 changes: 51 additions & 7 deletions store/users/actions.js
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand All @@ -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));
};
Expand All @@ -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 });
});
};
};
Expand All @@ -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 });
});
};
};
Expand All @@ -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));
};
};
8 changes: 4 additions & 4 deletions store/users/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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)],
Expand Down