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

added comments #838

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
4 changes: 0 additions & 4 deletions .vscode/settings.json

This file was deleted.

3 changes: 3 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* make h1 as a color red */
h1{
color: red;
}

/* giving it color gray and line-through decoration */
.completed{
color: gray;
text-decoration: line-through;
Expand Down
4 changes: 4 additions & 0 deletions public/js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//delete button
const deleteBtn = document.querySelectorAll('.fa-trash')

const item = document.querySelectorAll('.item span')
const itemCompleted = document.querySelectorAll('.item span.completed')

Expand All @@ -14,6 +16,8 @@ Array.from(itemCompleted).forEach((element)=>{
element.addEventListener('click', markUnComplete)
})


//an aynch function for deleting an item
async function deleteItem(){
const itemText = this.parentNode.childNodes[1].innerText
try{
Expand Down
26 changes: 26 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,51 @@
// To use the packages installed, we import
// them using require and
//requiring express
const express = require('express')

// Initializing a constant to use express
// methods and create middlewares.
const app = express()

//connecting mongodb
const MongoClient = require('mongodb').MongoClient

//listening on port 2121
const PORT = 2121

//connecting .env file
require('dotenv').config()


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

//connecting to mongodb
MongoClient.connect(dbConnectionStr, { useUnifiedTopology: true })
.then(client => {
console.log(`Connected to ${dbName} Database`)
db = client.db(dbName)
})


// viewing engine has to be set to use ejs
app.set('view engine', 'ejs')

// Telling Nodejs that all our static
// files are stored in public folder
app.use(express.static('public'))

// Telling Node.js to use body-parser
app.use(express.urlencoded({ extended: true }))
app.use(express.json())


app.get('/',async (request, response)=>{
const todoItems = await db.collection('todos').find().toArray()
const itemsLeft = await db.collection('todos').countDocuments({completed: false})

// Render the file names index.ejs
response.render('index.ejs', { items: todoItems, left: itemsLeft })
// db.collection('todos').find().toArray()
// .then(data => {
Expand Down Expand Up @@ -78,6 +100,8 @@ app.put('/markUnComplete', (request, response) => {

})


//deleting an item
app.delete('/deleteItem', (request, response) => {
db.collection('todos').deleteOne({thing: request.body.itemFromJS})
.then(result => {
Expand All @@ -88,6 +112,8 @@ app.delete('/deleteItem', (request, response) => {

})


//app listening on port
app.listen(process.env.PORT || PORT, ()=>{
console.log(`Server running on port ${PORT}`)
})
4 changes: 4 additions & 0 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<body>
<h1>Todo List: </h1>
<ul class="todoItems">

<!-- To avoid error, we simply use if-else,
empty object is handled in if block -->
<% for(let i=0; i < items.length; i++) {%>
<li class="item">
<% if(items[i].completed === true) {%>
Expand All @@ -35,6 +38,7 @@
<h2>Left to do: <%= left %></h2>

<h2>Add A Todo:</h2>


<form action="/addTodo" method="POST">
<input type="text" placeholder="Thing To Do" name="todoItem">
Expand Down