Skip to content

Commit 0efc451

Browse files
author
codingChewie
committed
Linking Around the Application
1 parent 880b9d5 commit 0efc451

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

routes/cards.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@ const eyes = require('eyespect')
44
const { data } = require('../data/flashcardData.json')
55
const { cards } = data
66

7+
router.get('/', (req, res) => {
8+
const numberOfCards = cards.length
9+
const flashcardId = Math.floor(Math.random() * numberOfCards)
10+
11+
res.redirect(`/cards/${flashcardId}`)
12+
})
13+
714
router.get('/:id', (req, res) => {
815
eyes.inspect(req.query, 'req.query')
916

1017
const { side } = req.query
1118
const { id } = req.params
19+
20+
if (!side) res.redirect(`/cards/${id}?side=question`)
21+
22+
const name = req.cookies.username
1223
const text = cards[id][side]
1324
const { hint } = cards[id]
1425

15-
const templateData = { id, text }
26+
const templateData = { id, text, name }
1627

1728
if (side === 'question') {
1829
templateData.hint = hint

views/card.pug

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ block content
66
if hint
77
p
88
i Hint: #{hint}
9-
a(href=`/cards/${id}?side=${sideToShow}`)= `${sideToShow.charAt(0).toUpperCase()}${sideToShow.slice(1)}`
9+
a(href=`/cards/${id}?side=${sideToShow}`)= `${sideToShow.charAt(0).toUpperCase()}${sideToShow.slice(1)}`
10+
br
11+
a(href='/cards') Next card

views/index.pug

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@ extends layout.pug
22

33
block content
44
section#content
5-
h2 Welcome, #{name}!
6-
form(action='/goodbye', method='POST')
7-
button(type='submit') Goodbye
8-
5+
p
6+
a(href='/cards') Begin!

views/layout.pug

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ html(lang="en")
44
title Flash Cards
55
body
66
include includes/header.pug
7+
if name
8+
h2 Welcome, #{name}!
9+
form(action='/goodbye', method='POST')
10+
button(type='submit') Goodbye
711
block content
812
include includes/footer.pug

0 commit comments

Comments
 (0)