Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lulunac27a committed Aug 26, 2024
1 parent 215d234 commit 8e29e76
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
36 changes: 18 additions & 18 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
require 'sinatra'
require 'erb'
require 'date'
require 'sinatra' # Sinatra web server
require 'erb' # Ruby templates
require 'date' # date parser

class TaskList < Sinatra::Base
task_list = []
id_counter = 1
class TaskList < Sinatra::Base # task list class
task_list = [] # set initial task list to empty array
id_counter = 1 # set initial id counter to 1

def overdue?(due_date)
Date.parse(due_date) < Date.today
def overdue?(due_date) # check if task is overdue
Date.parse(due_date) < Date.today # parse the date string
end

get '/' do
@tasks = task_list
erb :index
get '/' do # get task list
@tasks = task_list # set tasks to task list array
erb :index # redirect to index page
end

post '/add' do
task_list << { id: id_counter, name: params[:task], due_date: params[:due_date] }
id_counter += 1
redirect '/'
post '/add' do # add task to task list
task_list << { id: id_counter, name: params[:task], due_date: params[:due_date] } # add task to task list
id_counter += 1 # increase id counter by 1 when task is added
redirect '/' # redirect to index page
end

post '/delete/:id' do
task_list.reject! { |task| task[:id] == params[:id].to_i }
redirect '/'
task_list.reject! { |task| task[:id] == params[:id].to_i } # find task to delete by task id
redirect '/' # redirect to index page
end
end

TaskList.run!
TaskList.run! # run the task list application
11 changes: 6 additions & 5 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
</head>
<body>
<h1>Task List</h1>
<form action="/add" method="post">
<form action="/add" method="post"><%# form to add task to task list %>
Name: <input type="text" name="task" required><br>
<%# all fields are required to add a task %>
Due date: <input type="date" name="due_date" required><br>
<input type="submit">
</form>
<ul>
Number of tasks: <%= @tasks.length %><br>
<% @tasks.each do |task| %>
Number of tasks: <%= @tasks.length %><%# get number of tasks in task list %><br>
<% @tasks.each do |task| %><%# repeat for each task%>
<li>
<%= task[:name] %><% if overdue?(task[:due_date]) %>
<%= task[:name] %><% if overdue?(task[:due_date]) %><%# check if task is overdue %>
<strong>(Overdue)</strong>
<% end %><br>
Due date: <%= task[:due_date] %><br>
<form action="/delete/<%= task[:id] %>" method="post">
<form action="/delete/<%= task[:id] %>" method="post"><%# form to delete task from task list from task id %>
<input type="submit" value="Delete Task">
</form>
</li>
Expand Down

0 comments on commit 8e29e76

Please sign in to comment.