allow draft publish + view drafts as author

This commit is contained in:
Niko Abeler 2023-08-05 10:14:05 +02:00
parent 9e64672f9c
commit 42d960f185
6 changed files with 38 additions and 7 deletions

View File

@ -6,6 +6,10 @@
<br> <br>
<br> <br>
<form method="POST" enctype="multipart/form-data">
{{.}} {{.}}
<input type="submit" value="Submit" />
</form>
{{end}} {{end}}

View File

@ -6,6 +6,13 @@
<br> <br>
<br> <br>
<form method="POST" enctype="multipart/form-data">
{{.}} {{.}}
<div class="grid">
<input type="submit" name="action" value="Publish" />
<input class="secondary" type="submit" name="action" value="Save as Draft" />
</div>
</form>
{{end}} {{end}}

View File

@ -2,6 +2,14 @@
{{define "main"}} {{define "main"}}
{{ if not .Entry.PublishedAt }}
<mark>
This entry is a draft. It is only visible to logged in authors.
</mark>
<br>
<br>
{{ end }}
<div class="h-entry"> <div class="h-entry">
<hgroup> <hgroup>
{{if .Entry.Title}} {{if .Entry.Title}}

View File

@ -75,7 +75,12 @@ func (h *EditorHandler) HandlePostNew(c *fiber.Ctx) error {
// create entry // create entry
now := time.Now() now := time.Now()
entry.SetMetaData(entryMeta) entry.SetMetaData(entryMeta)
published := c.FormValue("action") == "Publish"
if published {
entry.SetPublishedAt(&now) entry.SetPublishedAt(&now)
} else {
entry.SetPublishedAt(nil)
}
entry.SetAuthorId(c.Locals("author").(string)) entry.SetAuthorId(c.Locals("author").(string))
err = h.entrySvc.Create(entry) err = h.entrySvc.Create(entry)
@ -119,6 +124,11 @@ func (h *EditorHandler) HandlePostEdit(c *fiber.Ctx) error {
return err return err
} }
published := c.FormValue("action") == "Publish"
if !published {
entry.SetPublishedAt(nil)
}
// update entry // update entry
entry.SetMetaData(meta) entry.SetMetaData(meta)
err = h.entrySvc.Update(entry) err = h.entrySvc.Update(entry)

View File

@ -39,15 +39,19 @@ func NewEntryHandler(
func (h *EntryHandler) Handle(c *fiber.Ctx) error { func (h *EntryHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
loggedIn := c.Locals("author") != nil
entryId := c.Params("post") entryId := c.Params("post")
entry, err := h.entrySvc.FindById(entryId) entry, err := h.entrySvc.FindById(entryId)
if err != nil { if err != nil {
return err return err
} }
if !loggedIn {
if entry.PublishedAt() == nil || entry.PublishedAt().IsZero() { if entry.PublishedAt() == nil || entry.PublishedAt().IsZero() {
return fiber.NewError(fiber.StatusNotFound, "Entry not found") return fiber.NewError(fiber.StatusNotFound, "Entry not found")
} }
}
author, err := h.authorSvc.FindByName(entry.AuthorId()) author, err := h.authorSvc.FindByName(entry.AuthorId())
if err != nil { if err != nil {
@ -61,7 +65,7 @@ func (h *EntryHandler) Handle(c *fiber.Ctx) error {
entryData{ entryData{
Entry: entry, Entry: entry,
Author: author, Author: author,
LoggedIn: c.Locals("author") != nil, LoggedIn: loggedIn,
}, },
) )
} }

View File

@ -107,12 +107,10 @@ func (s *Form) HtmlForm() (string, error) {
return "", err return "", err
} }
html := "<form method=\"POST\" enctype=\"multipart/form-data\">\n" html := ""
for _, field := range fields { for _, field := range fields {
html += field.Html() html += field.Html()
} }
html += "<input type=\"submit\" value=\"Submit\" />\n"
html += "</form>\n"
return html, nil return html, nil
} }