Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commented out original conde and rewrote every line #64

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 94 additions & 21 deletions public/js/main.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,87 @@
const deleteBtn = document.querySelectorAll('.del')
const todoItem = document.querySelectorAll('.todoItem span')
const todoComplete = document.querySelectorAll('.todoItem span.completed')
// const deleteBtn = document.querySelectorAll('.del')
// const todoItem = document.querySelectorAll('.todoItem span')
// const todoComplete = document.querySelectorAll('.todoItem span.completed')

Array.from(deleteBtn).forEach((el)=>{
el.addEventListener('click', deleteTodo)
})
// Array.from(deleteBtn).forEach((el)=>{
// el.addEventListener('click', deleteTodo)
// })

Array.from(todoItem).forEach((el)=>{
el.addEventListener('click', markComplete)
})
// Array.from(todoItem).forEach((el)=>{
// el.addEventListener('click', markComplete)
// })

// Array.from(todoComplete).forEach((el)=>{
// el.addEventListener('click', undo)
// })

// async function deleteTodo(){
// const todoText = this.parentNode.childNodes[1].innerText
// try{
// const response = await fetch('deleteTodo', {
// method: 'delete',
// headers: {'Content-type': 'application/json'},
// body: JSON.stringify({
// 'rainbowUnicorn': todoText
// })
// })
// const data = await response.json()
// console.log(data)
// location.reload()
// }catch(err){
// console.log(err)
// }
// }

// async function markComplete(){
// const todoText = this.parentNode.childNodes[1].innerText
// try{
// const response = await fetch('markComplete', {
// method: 'put',
// headers: {'Content-type': 'application/json'},
// body: JSON.stringify({
// 'rainbowUnicorn': todoText
// })
// })
// const data = await response.json()
// console.log(data)
// location.reload()
// }catch(err){
// console.log(err)
// }
// }

// async function undo(){
// const todoText = this.parentNode.childNodes[1].innerText
// try{
// const response = await fetch('undo', {
// method: 'put',
// headers: {'Content-type': 'application/json'},
// body: JSON.stringify({
// 'rainbowUnicorn': todoText
// })
// })
// const data = await response.json()
// console.log(data)
// location.reload()
// }catch(err){
// console.log(err)
// }
// }

const delBtn = document.querySelectorAll('.del')

Array.from(todoComplete).forEach((el)=>{
el.addEventListener('click', undo)
Array.from(delBtn).forEach(item => {
item.addEventListener('click', deleteTodo)
})

async function deleteTodo(){
const todoText = this.parentNode.childNodes[1].innerText
try{
const response = await fetch('deleteTodo', {
const todoText = await this.parentNode.childNodes[1].innerText
try {
const response = await fetch('deleteTodo' , {
method: 'delete',
headers: {'Content-type': 'application/json'},
body: JSON.stringify({
'rainbowUnicorn': todoText
'delItem': todoText
})
})
const data = await response.json()
Expand All @@ -32,14 +92,20 @@ async function deleteTodo(){
}
}

const compBtn = document.querySelectorAll('.item')

Array.from(compBtn).forEach(item => {
item.addEventListener('click', markComplete)
})

async function markComplete(){
const todoText = this.parentNode.childNodes[1].innerText
const todoText = await this.innerText
try{
const response = await fetch('markComplete', {
method: 'put',
headers: {'Content-type': 'application/json'},
body: JSON.stringify({
'rainbowUnicorn': todoText
'updateItem': todoText
})
})
const data = await response.json()
Expand All @@ -50,14 +116,21 @@ async function markComplete(){
}
}

async function undo(){
const todoText = this.parentNode.childNodes[1].innerText
const undoBtn = document.querySelectorAll('.completed')

Array.from(undoBtn).forEach(item => {
item.addEventListener('click', undoItem)
})
console.log(undoBtn)

async function undoItem() {
const undoText = await this.innerText
try{
const response = await fetch('undo', {
const response = await fetch('markIncomplete' , {
method: 'put',
headers: {'Content-type': 'application/json'},
body: JSON.stringify({
'rainbowUnicorn': todoText
'updateItem': undoText
})
})
const data = await response.json()
Expand Down
158 changes: 118 additions & 40 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,152 @@
// const express = require('express')
// const app = express()
// const MongoClient = require('mongodb').MongoClient
// const PORT = 2121
// require('dotenv').config()

// let db,
// dbConnectionStr = process.env.DB_STRING,
// dbName = 'todo'

// MongoClient.connect(dbConnectionStr, {useUnifiedTopology: true})
// .then(client => {
// console.log(`Hey, connected to ${dbName} database`)
// db = client.db(dbName)
// })
// .catch(err =>{
// console.log(err)
// })

// app.set('view engine', 'ejs')
// app.use(express.static('public'))
// app.use(express.urlencoded({ extended: true }))
// app.use(express.json())

// app.get('/', async (req,res)=>{
// const todoItems = await db.collection('todos').find().toArray()
// const itemsLeft = await db.collection('todos').countDocuments({completed: false})
// res.render('index.ejs', {zebra: todoItems, left: itemsLeft})
// })

// app.post('/createTodo', (req, res)=>{
// db.collection('todos').insertOne({todo: req.body.todoItem, completed: false})
// .then(result =>{
// console.log('Todo has been added!')
// res.redirect('/')
// })
// })

// app.put('/markComplete', (req, res)=>{
// db.collection('todos').updateOne({todo: req.body.rainbowUnicorn},{
// $set: {
// completed: true
// }
// })
// .then(result =>{
// console.log('Marked Complete')
// res.json('Marked Complete')
// })
// })

// app.put('/undo', (req, res)=>{
// db.collection('todos').updateOne({todo: req.body.rainbowUnicorn},{
// $set: {
// completed: false
// }
// })
// .then(result =>{
// console.log('Marked Complete')
// res.json('Marked Complete')
// })
// })

// app.delete('/deleteTodo', (req, res)=>{
// db.collection('todos').deleteOne({todo:req.body.rainbowUnicorn})
// .then(result =>{
// console.log('Deleted Todo')
// res.json('Deleted It')
// })
// .catch( err => console.log(err))
// })

// app.listen(process.env.PORT || PORT, ()=>{
// console.log('Server is running, you better catch it!')
// })

const express = require('express')
const app = express()
const MongoClient = require('mongodb').MongoClient
const PORT = 2121
const PORT = process.env.PORT || 6969
require('dotenv').config()

let db,
dbConnectionStr = process.env.DB_STRING,
dbConnectionStr = process.env.DB_STRING
dbName = 'todo'

MongoClient.connect(dbConnectionStr, {useUnifiedTopology: true})
.then(client => {
console.log(`Hey, connected to ${dbName} database`)
console.log(`Connected to ${dbName} database`)
db = client.db(dbName)
})
.catch(err =>{
console.log(err)
})
.catch(err => console.log(err))

app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())

app.get('/', async (req,res)=>{
const todoItems = await db.collection('todos').find().toArray()
const itemsLeft = await db.collection('todos').countDocuments({completed: false})
res.render('index.ejs', {zebra: todoItems, left: itemsLeft})
app.get('/', (req, res) => {
db.collection('todos').find().toArray()
.then(data => {
res.render('index.ejs' , {dbarr : data})
})
.catch(err => console.log(err))
})

app.post('/createTodo', (req, res)=>{
db.collection('todos').insertOne({todo: req.body.todoItem, completed: false})
.then(result =>{
console.log('Todo has been added!')
res.redirect('/')
})
app.post('/createTodo' , (req, res) => {
db.collection('todos').insertOne({todo : req.body.todoItem, completed : false})
.then(result => {
console.log('Item added')
res.redirect('/')
})
.catch(err => console.log(err))
})

app.put('/markComplete', (req, res)=>{
db.collection('todos').updateOne({todo: req.body.rainbowUnicorn},{
app.put('/markComplete', (req, res) => [
db.collection('todos').updateOne({todo : req.body.updateItem} , {
$set: {
completed: true
}
})
.then(result =>{
console.log('Marked Complete')
res.json('Marked Complete')
})
})
.then(result => {
console.log('Marked Complete')
res.json('Marked Complete')
})
.catch(err => console.log(err))
])

app.put('/undo', (req, res)=>{
db.collection('todos').updateOne({todo: req.body.rainbowUnicorn},{
app.put('/markIncomplete', (req,res) => {
db.collection('todos').updateOne({todo: req.body.updateItem} , {
$set: {
completed: false
}
})
.then(result =>{
console.log('Marked Complete')
res.json('Marked Complete')
})
.then(result => {
console.log('Marked Incomplete')
res.json('Undo Complete')
})
.catch(err => console.log(err))
})

app.delete('/deleteTodo', (req, res)=>{
db.collection('todos').deleteOne({todo:req.body.rainbowUnicorn})
.then(result =>{
console.log('Deleted Todo')
res.json('Deleted It')
})
.catch( err => console.log(err))
app.delete('/deleteTodo' , (req, res) => {
db.collection('todos').deleteOne({todo : req.body.delItem})
.then(result => {
console.log('Item deleted')
res.json('Item deleted')
})
.catch(err => console.log(err))
})
app.listen(process.env.PORT || PORT, ()=>{
console.log('Server is running, you better catch it!')
})

app.listen(PORT, () => {
console.log(`Connected to PORT ${PORT}`)
})
Loading