Skip to content

Commit

Permalink
feat(todo): view a single todo using /todo/<id> syntax, close #13
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 26, 2016
1 parent 1f8f068 commit 4ee7bda
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ label[for='toggle-all'] {
position: absolute;
}

.todo-list li a {
color: inherit;
text-decoration: none;
}

.todo-list li.completed label {
color: #d9d9d9;
text-decoration: line-through;
Expand Down
7 changes: 6 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ function grabMethodFromBody (req, res) {

app.use(methodOverride(grabMethodFromBody))

const renderIndexPage = require('./index-page')
const {renderIndexPage, renderTodoPage} = require('./index-page')

function sendIndexPage (req, res) {
res.send(renderIndexPage())
}

function sendTodoPage (req, res) {
res.send(renderTodoPage(req.params.id))
}

function activeTodosPage (req, res) {
const filter = (todo) => !todo.done
res.send(renderIndexPage(filter, req.url))
Expand Down Expand Up @@ -102,6 +106,7 @@ function clearCompleted (req, res, next) {
}

app.get('/', broadcast, sendIndexPage)
app.get('/todo/:id', sendTodoPage)
app.get('/app.css', sendAppCss)
app.get('/active', activeTodosPage)
app.get('/completed', completedTodosPage)
Expand Down
14 changes: 12 additions & 2 deletions src/index-page.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
'use strict'

const la = require('lazy-ass')
const is = require('check-more-types')
const render = require('./render/render')
const Todos = require('todomvc-model')
const toHTML = require('vdom-to-html')
Expand All @@ -10,7 +14,7 @@ const header = [
'<meta charset="utf-8">',
'<meta http-equiv="Content-Security-Policy" content="script-src \'none\';">',
'<title>TodoMVC</title>',
'<link rel="stylesheet" href="app.css">',
'<link rel="stylesheet" href="/app.css">',
'</style>',
'</head>',
'<body>',
Expand Down Expand Up @@ -38,4 +42,10 @@ function renderIndexPage (filter, route) {
return header + '\n' + todosMarkup + '\n' + footer
}

module.exports = renderIndexPage
function renderTodoPage (id) {
la(is.unemptyString(id), 'missing todo id', id)
const filter = (todo) => todo.id === id
return renderIndexPage(filter)
}

module.exports = {renderIndexPage, renderTodoPage}
6 changes: 5 additions & 1 deletion src/render/render-todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ function render (Todos, todo) {
value: 'PATCH'
})
]),
h('label', todo.what),
h('label', [
h('a', {
href: `/todo/${todo.id}`
}, todo.what)
]),
h('form', {
method: 'POST',
action: '/',
Expand Down

0 comments on commit 4ee7bda

Please sign in to comment.