Skip to content

Commit

Permalink
fixed reset
Browse files Browse the repository at this point in the history
  • Loading branch information
nsafai committed Mar 4, 2019
1 parent a496726 commit 047cc76
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
50 changes: 25 additions & 25 deletions sockets/checklist-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,85 +11,85 @@
// socket.emit sends data to the client that sent the original data to the server.

module.exports = (io, socket) => {
const Checklist = require('../models/checklist');
const Todo = require('../models/todo');
const Checklist = require('../models/checklist')
const Todo = require('../models/todo')

/***********************
* GET LIST
***********************/
socket.on('get-list', (id) => {
console.log('someone requesting lid with id', id);
console.log('someone requesting lid with id', id)
Checklist.findById(id, (err, selectedList) => {
if (err) { console.error(err) }
if (selectedList) {
selectedList.updatedAt = Date.now();
selectedList.save();
selectedList.updatedAt = Date.now()
selectedList.save()
Todo.find({
_id: {
$in: selectedList.todoItems,
},
}, (error, selectedListTodos) => {
if (error) return console.log(error);
if (error) return console.log(error)
const listData = {
currentList: selectedList,
currentListTodos: selectedListTodos,
}
socket.emit('get-list', listData)
});
})
} else {
console.log('no list found')
}
});
})
})

/***********************
* NEW LIST
***********************/
socket.on('new-list', (currentUserId) => {
console.log('someone trying to create a new list');
console.log('someone trying to create a new list')
const list = new Checklist({
title: 'New List',
ownerUserId: currentUserId,
});
console.log(`list: ${list}`);
})
console.log(`list: ${list}`)
list.save((err, savedList) => {
if (err) return console.error(err);
socket.emit('new-list', savedList);
});
if (err) return console.error(err)
socket.emit('new-list', savedList)
})
})

/***********************
* DELETE LIST
***********************/
socket.on('delete-list', (listId) => {
console.log(`got a delete request with: ${listId}`);
console.log(`got a delete request with: ${listId}`)
Checklist.findByIdAndRemove(listId, (err) => {
if (err) return console.log(err);
socket.emit('delete-list', listId);
});
if (err) return console.log(err)
socket.emit('delete-list', listId)
})
})

/***********************
* SAVE LIST NAME
***********************/
socket.on('save-list-name', (listData) => {
const { currentListId, newListName } = listData;
const { currentListId, newListName } = listData
Checklist.findByIdAndUpdate(
currentListId, {
$set: { title: newListName },
}, (err) => {
if (err) return console.error(err);
if (err) return console.error(err)
},
);
)
})

/***********************
* SEARCH LISTS
***********************/
socket.on('search', (searchData) => {
console.log('search data is:', searchData);
const { searchTerm, currentUserId } = searchData;
const query = new RegExp(searchTerm, 'i');
console.log('search data is:', searchData)
const { searchTerm, currentUserId } = searchData
const query = new RegExp(searchTerm, 'i')
console.log('user', currentUserId, ' searching for:', searchTerm)

Checklist.find({
Expand All @@ -100,7 +100,7 @@ module.exports = (io, socket) => {
socket.emit('search', {
searchTerm,
lists, // results are lists
});
})
})
})
}
12 changes: 6 additions & 6 deletions sockets/todo-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ module.exports = (io, socket) => {
/***********************
* RESET ALL TODOS
***********************/

// RESET ALL todos
socket.on('reset-all-todos', (checklistId) => {
console.log('got a request to reset all todos')
console.log('got a request to reset all todos on list', checklistId)
Checklist.findById(checklistId, (err, checklist) => {
if (err) return console.log(err)
const todosArray = checklist.todoItems
todosArray.forEach((todo) => {
Todo.findByIdAndUpdate(todo.id, {
console.log('todosArray', todosArray)
todosArray.forEach((todoId) => {
Todo.findByIdAndUpdate(todoId, {
$set: { completed: false },
}, (error) => {
if (error) return console.error(err)
if (error) return console.error(error)
})
})
}).then(() => {
socket.emit('reset-all-todos')
})
})
Expand Down

0 comments on commit 047cc76

Please sign in to comment.