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
9 changes: 1 addition & 8 deletions pages/api/groups/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) } },
{
Expand Down
88 changes: 51 additions & 37 deletions store/groups/actions.js
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand All @@ -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',
Expand All @@ -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) => {
Expand All @@ -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));
};
};
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