Browse Source

story: Added listing stories by month or author.

1.0
Gisle Aune 6 years ago
parent
commit
640244faf4
  1. 2
      marko/page/story/components/page/index.marko
  2. 10
      marko/page/story/components/story-menu/index.marko
  3. 23
      routes/story/by-author.js
  4. 31
      routes/story/by-month.js
  5. 2
      server.js

2
marko/page/story/components/page/index.marko

@ -1,4 +1,4 @@
<story-menu categories=input.categories selected=(input.selected || {}) menuTags=input.menuTags user=input.user on-open("open") />
<story-menu categories=input.categories selected=(input.selected || {}) menuTags=input.menuTags menuAuthor=input.menuAuthor menuMonth=input.menuMonth user=input.user on-open("open") />
<main> <main>
<story-list stories=state.stories /> <story-list stories=state.stories />
<add-story-modal enabled=(state.modal === "story.add") stories=input.stories categories=input.categories on-close("close") /> <add-story-modal enabled=(state.modal === "story.add") stories=input.stories categories=input.categories on-close("close") />

10
marko/page/story/components/story-menu/index.marko

@ -6,6 +6,16 @@
<menu-header>Categories</menu-header> <menu-header>Categories</menu-header>
<menu-link for(category in (input.categories||[])) key=(category.name) selected=(input.selected.category == category.name) href=("/story/by-category/"+category.name) icon=category.name.charAt(0)>${category.name}</menu-link> <menu-link for(category in (input.categories||[])) key=(category.name) selected=(input.selected.category == category.name) href=("/story/by-category/"+category.name) icon=category.name.charAt(0)>${category.name}</menu-link>
<menu-gap /> <menu-gap />
<if(input.menuMonth != null)>
<menu-header>Month</menu-header>
<menu-link selected=(input.selected.special === "date") icon="M" href=("/story/by-month/"+input.menuMonth)>${input.menuMonth}</menu-link>
<menu-gap />
</if>
<if(input.menuAuthor != null)>
<menu-header>Author</menu-header>
<menu-link selected=(input.selected.special === "author") icon="A" href=("/story/by-author/"+input.menuAuthor)>${input.menuAuthor}</menu-link>
<menu-gap />
</if>
<menu-header>Tags</menu-header> <menu-header>Tags</menu-header>
<menu-link for(tag in (input.menuTags||[])) key=(tag.kind+":"+tag.name) selected=(input.selected.tag === tag.kind+":"+tag.name) href=("/story/by-tag/"+tag.kind+"/"+tag.name) textClass=("color-tag-"+tag.kind.toLowerCase()) icon=tag.kind.charAt(0)>${tag.name}</menu-link> <menu-link for(tag in (input.menuTags||[])) key=(tag.kind+":"+tag.name) selected=(input.selected.tag === tag.kind+":"+tag.name) href=("/story/by-tag/"+tag.kind+"/"+tag.name) textClass=("color-tag-"+tag.kind.toLowerCase()) icon=tag.kind.charAt(0)>${tag.name}</menu-link>
<menu-link selected=input.selected.tags icon="T" href="/story/tag-list/">Tags</menu-link> <menu-link selected=input.selected.tags icon="T" href="/story/tag-list/">Tags</menu-link>

23
routes/story/by-author.js

@ -0,0 +1,23 @@
const express = require("express")
const router = express.Router()
const common = require("./common")
const {storyApi} = require("../../rpdata/api/Story")
const listTemplate = require("../../marko/page/story/list.marko")
router.get("/:author", common, async(req, res) => {
res.locals.menuAuthor = req.params.author
try {
res.markoAsync(listTemplate, {
stories: storyApi.list({author: req.params.author}),
menuAuthor: req.params.author,
selected: {special: "author"},
})
} catch(err) {
console.error(err)
}
})
module.exports = router

31
routes/story/by-month.js

@ -0,0 +1,31 @@
const express = require("express")
const router = express.Router()
const common = require("./common")
const {storyApi} = require("../../rpdata/api/Story")
const listTemplate = require("../../marko/page/story/list.marko")
router.get("/:yearmonth(\-{0,1}[0-9]{4}\-[0-9]{1,2})", common, async(req, res) => {
try {
const [year, month] = req.params.yearmonth.split("-").map(t => parseInt(t, 10))
if (month > 12) {
return res.status(400).end(`${month} is not a valid month number`)
}
// Month is zero-based, while input is one-based.
const fromDate = new Date(Date.UTC(year, month - 1, 1))
const toDate = new Date(Date.UTC(year, month, 1))
const monthStr = `${fromDate}-${month > 9 ? month : '0' + month}`
res.markoAsync(listTemplate, {
stories: storyApi.list({earliestFictionalDate: fromDate, latestFictionalDate: toDate}),
menuMonth: monthStr,
selected: {special: "month"},
})
} catch(err) {
console.error(err)
}
})
module.exports = router

2
server.js

@ -73,6 +73,8 @@ app.use("/graphql", require("./routes/graphql"))
app.use("/story/", require("./routes/story")) app.use("/story/", require("./routes/story"))
app.use("/story/by-category/", require("./routes/story/by-category")) app.use("/story/by-category/", require("./routes/story/by-category"))
app.use("/story/by-tag/", require("./routes/story/by-tag")) app.use("/story/by-tag/", require("./routes/story/by-tag"))
app.use("/story/by-month/", require("./routes/story/by-month"))
app.use("/story/by-author/", require("./routes/story/by-author"))
app.use("/story/tag-list/", require("./routes/story/tag-list")) app.use("/story/tag-list/", require("./routes/story/tag-list"))
app.use("/story/:id(S[0-9a-z]{15})/", require("./routes/story-content")) app.use("/story/:id(S[0-9a-z]{15})/", require("./routes/story-content"))
app.use("/logs/", require("./routes/logs")) app.use("/logs/", require("./routes/logs"))

Loading…
Cancel
Save