216 lines
5.8 KiB
Plaintext
216 lines
5.8 KiB
Plaintext
package views
|
|
|
|
import "fmt"
|
|
import "git.32bit.cafe/32bitcafe/guestbook/internal/models"
|
|
import "git.32bit.cafe/32bitcafe/guestbook/internal/forms"
|
|
import "time"
|
|
|
|
templ GuestbookDashboardCommentsView(title string, data CommonData, website models.Website, guestbook models.Guestbook, comments []models.GuestbookComment) {
|
|
@base(title, data) {
|
|
<div id="dashboard">
|
|
@wSidebar(website)
|
|
<div>
|
|
<h1>Comments on { website.Name }</h1>
|
|
<hr/>
|
|
if len(comments) == 0 {
|
|
<p>No comments yet!</p>
|
|
}
|
|
for _, c := range comments {
|
|
@GuestbookDashboardCommentView(data, website, c)
|
|
}
|
|
</div>
|
|
</div>
|
|
}
|
|
}
|
|
|
|
templ GuestbookDashboardCommentView(data CommonData, w models.Website, c models.GuestbookComment) {
|
|
{{ commentUrl := fmt.Sprintf("%s/dashboard/guestbook/comments/%s", wUrl(w), shortIdToSlug(c.ShortId)) }}
|
|
{{ hxHeaders := fmt.Sprintf("{\"X-CSRF-Token\": \"%s\"}", data.CSRFToken) }}
|
|
<div class="comment">
|
|
<div>
|
|
if c.Deleted.IsZero() {
|
|
<button class="danger" hx-delete={ commentUrl } hx-target="closest div.comment" hx-headers={ hxHeaders }>Delete</button>
|
|
<button class="outline" hx-put={ commentUrl } hx-target="closest div.comment" hx-headers={ hxHeaders }>
|
|
if !c.IsPublished {
|
|
Publish
|
|
} else {
|
|
Hide
|
|
}
|
|
</button>
|
|
}
|
|
</div>
|
|
<div>
|
|
<strong>{ c.AuthorName }</strong>
|
|
if len(c.AuthorEmail) > 0 {
|
|
{{ email := "mailto:" + c.AuthorEmail }}
|
|
| <a href={ templ.URL(email) } target="_blank">{ c.AuthorEmail }</a>
|
|
}
|
|
if len(c.AuthorSite) > 0 {
|
|
| <a href={ templ.URL(externalUrl(c.AuthorSite)) } target="_blank">{ c.AuthorSite }</a>
|
|
}
|
|
<p>
|
|
{ c.Created.In(data.CurrentUser.Settings.LocalTimezone).Format("01-02-2006 03:04PM") }
|
|
</p>
|
|
</div>
|
|
<p>
|
|
{ c.CommentText }
|
|
</p>
|
|
<hr/>
|
|
</div>
|
|
}
|
|
|
|
templ commentForm(form forms.CommentCreateForm) {
|
|
<div>
|
|
<label for="authorname">Name</label>
|
|
{{ error, exists := form.FieldErrors["authorName"] }}
|
|
if exists {
|
|
<label class="error">{ error }</label>
|
|
}
|
|
<input type="text" name="authorname" id="authorname"/>
|
|
</div>
|
|
<div>
|
|
<label for="authoremail">Email (Optional) </label>
|
|
{{ error, exists = form.FieldErrors["authorEmail"] }}
|
|
if exists {
|
|
<label class="error">{ error }</label>
|
|
}
|
|
<input type="text" name="authoremail" id="authoremail"/>
|
|
</div>
|
|
<div>
|
|
<label for="authorsite">Site Url (Optional) </label>
|
|
{{ error, exists = form.FieldErrors["authorSite"] }}
|
|
if exists {
|
|
<label class="error">{ error }</label>
|
|
}
|
|
<input type="text" name="authorsite" id="authorsite"/>
|
|
</div>
|
|
<div>
|
|
<label for="content">Comment</label>
|
|
{{ error, exists = form.FieldErrors["content"] }}
|
|
if exists {
|
|
<label class="error">{ error }</label>
|
|
}
|
|
<textarea name="content" id="content"></textarea>
|
|
</div>
|
|
<div>
|
|
<input type="submit" value="Submit"/>
|
|
</div>
|
|
}
|
|
|
|
templ GuestbookView(title string, data CommonData, website models.Website, guestbook models.Guestbook, comments []models.GuestbookComment, form forms.CommentCreateForm) {
|
|
{{ postUrl := fmt.Sprintf("/websites/%s/guestbook/comments/create", shortIdToSlug(website.ShortId)) }}
|
|
if data.IsHtmx {
|
|
@commentForm(form)
|
|
} else {
|
|
<html>
|
|
<head>
|
|
<title>{ title }</title>
|
|
<link href="/static/css/classless.min.css" rel="stylesheet"/>
|
|
<script src="/static/js/main.js" defer></script>
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<div>
|
|
<h1>{ website.Name } Guestbook</h1>
|
|
{ data.Flash }
|
|
<form action={ templ.URL(postUrl) } method="post">
|
|
<input type="hidden" name="csrf_token" value={ data.CSRFToken }/>
|
|
@commentForm(form)
|
|
</form>
|
|
</div>
|
|
<div id="comments">
|
|
if len(comments) == 0 {
|
|
<p>No comments yet!</p>
|
|
}
|
|
for _, c := range comments {
|
|
<div>
|
|
<h3>
|
|
if c.AuthorSite != "" {
|
|
<a href={ templ.URL(externalUrl(c.AuthorSite)) } target="_blank">{ c.AuthorName }</a>
|
|
} else {
|
|
{ c.AuthorName }
|
|
}
|
|
</h3>
|
|
<time datetime={ c.Created.Format(time.RFC3339) }>{ c.Created.Format("01-02-2006 03:04PM") }</time>
|
|
<p>
|
|
{ c.CommentText }
|
|
</p>
|
|
</div>
|
|
}
|
|
</div>
|
|
</main>
|
|
</body>
|
|
</html>
|
|
}
|
|
}
|
|
|
|
templ settingRadio(selected bool, name, id, value string) {
|
|
<input type="radio" name={ name } id={ id } value={ value } selected?={ selected }/>
|
|
}
|
|
|
|
templ EmbeddableGuestbookCommentForm(data CommonData, w models.Website, f forms.CommentCreateForm) {
|
|
{{ postUrl := fmt.Sprintf("/websites/%s/guestbook/comments/create?headless=true", shortIdToSlug(w.ShortId)) }}
|
|
<html>
|
|
<head>
|
|
<link href="/static/css/classless.min.css" rel="stylesheet"/>
|
|
</head>
|
|
<body>
|
|
{ data.Flash }
|
|
<form action={ templ.URL(postUrl) } method="post">
|
|
<input type="hidden" name="csrf_token" value={ data.CSRFToken }/>
|
|
@commentForm(f)
|
|
</form>
|
|
</body>
|
|
</html>
|
|
}
|
|
|
|
templ AllGuestbooksView(data CommonData, websites []models.Website) {
|
|
@base("All Guestbooks", data) {
|
|
<div>
|
|
<p>
|
|
This page exists only for testing the service.
|
|
</p>
|
|
<ul>
|
|
for _, w := range websites {
|
|
<li>
|
|
{{ gbUrl := fmt.Sprintf("/websites/%s/guestbook", shortIdToSlug(w.ShortId)) }}
|
|
<a href={ templ.URL(gbUrl) } target="_blank">{ w.Name }</a>
|
|
</li>
|
|
}
|
|
</ul>
|
|
</div>
|
|
}
|
|
}
|
|
|
|
templ GuestbookCommentCreateRemoteErrorView(url, err string) {
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="refresh" content={ fmt.Sprintf("3; url='%s'", templ.URL(externalUrl(url))) }/>
|
|
</head>
|
|
<body>
|
|
<p>
|
|
An error occurred while posting comment. { err }. Redirecting.
|
|
</p>
|
|
<p>
|
|
<a href={ templ.URL(url) }>Redirect</a>
|
|
</p>
|
|
</body>
|
|
</html>
|
|
}
|
|
|
|
templ GuestbookCommentCreateRemoteSuccessView(url string) {
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="refresh" content={ fmt.Sprintf("3; url='%s'", templ.URL(externalUrl(url))) }/>
|
|
</head>
|
|
<body>
|
|
<p>
|
|
Comment successfully posted. Redirecting.
|
|
</p>
|
|
<p>
|
|
<a href={ templ.URL(url) }>Redirect</a>
|
|
</p>
|
|
</body>
|
|
</html>
|
|
}
|