141 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			141 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
package views
 | 
						|
 | 
						|
import "fmt"
 | 
						|
import "git.32bit.cafe/32bitcafe/guestbook/internal/models"
 | 
						|
import "git.32bit.cafe/32bitcafe/guestbook/internal/forms"
 | 
						|
 | 
						|
func gbUrl(gb models.Guestbook) string {
 | 
						|
    return fmt.Sprintf("/guestbooks/%s", shortIdToSlug(gb.ShortId))
 | 
						|
}
 | 
						|
 | 
						|
templ gbCreateForm(csrf_token string) {
 | 
						|
    <input type="hidden" name="csrf_token" value={ csrf_token }>
 | 
						|
    <label for="siteurl">Site URL: </label>
 | 
						|
    <input type="text" name="siteurl" id="siteurl" required />
 | 
						|
    <button type="submit">Submit</button>
 | 
						|
}
 | 
						|
 | 
						|
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.SiteUrl }</h1>
 | 
						|
                if len(comments) == 0 {
 | 
						|
                    <p>No comments yet!</p>
 | 
						|
                }
 | 
						|
                for  _, c := range comments {
 | 
						|
                    <div>
 | 
						|
                        <strong>{ c.AuthorName }</strong>
 | 
						|
                        { c.Created.Format("01-02-2006 03:04PM") }
 | 
						|
                        if c.Deleted.IsZero() {
 | 
						|
                            {{ commentUrl := fmt.Sprintf("%s/dashboard/guestbook/comments/%s", wUrl(website), shortIdToSlug(c.ShortId)) }}
 | 
						|
                            {{ hxHeaders := fmt.Sprintf("{\"X-CSRF-Token\": \"%s\"}", data.CSRFToken) }}
 | 
						|
                            <a href="#" hx-delete={ commentUrl } hx-target="closest div" hx-headers={ hxHeaders }>Delete</a>
 | 
						|
                        }
 | 
						|
                        
 | 
						|
                        <p>
 | 
						|
                            { c.CommentText }
 | 
						|
                        </p>
 | 
						|
                    </div>
 | 
						|
                }
 | 
						|
            </div>
 | 
						|
        </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: </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: </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 GuestbookCommentList(comments []models.GuestbookComment) {
 | 
						|
    if len(comments) == 0 {
 | 
						|
        <p>No comments yet!</p>
 | 
						|
    }
 | 
						|
    for _, c := range comments {
 | 
						|
        <div>
 | 
						|
            <strong>{ c.AuthorName }</strong>
 | 
						|
            { c.Created.Format("01-02-2006 03:04PM") }
 | 
						|
            <p>
 | 
						|
                { c.CommentText }
 | 
						|
            </p>
 | 
						|
        </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>
 | 
						|
            </head>
 | 
						|
            <body>
 | 
						|
                <main>
 | 
						|
                    <div>
 | 
						|
                        <h1>Guestbook for { website.SiteUrl }</h1>
 | 
						|
                        <form action={ templ.URL(postUrl) } method="post">
 | 
						|
                            <input type="hidden" name="csrf_token" value={data.CSRFToken}>
 | 
						|
                            @commentForm(form)
 | 
						|
                        </form>
 | 
						|
                    </div>
 | 
						|
                    <div id="comments">
 | 
						|
                        @GuestbookCommentList(comments)
 | 
						|
                    </div>
 | 
						|
                </main>
 | 
						|
            </body>
 | 
						|
        </html>
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
templ CreateGuestbookComment(title string, data CommonData, website models.Website, guestbook models.Guestbook, form forms.CommentCreateForm) {
 | 
						|
    {{ postUrl := fmt.Sprintf("/websites/%s/guestbook/comments/create", shortIdToSlug(website.ShortId)) }}
 | 
						|
    if data.IsHtmx {
 | 
						|
        <form hx-post={ postUrl } hx-target="closest div">
 | 
						|
            @commentForm(form)
 | 
						|
        </form>
 | 
						|
    } else {
 | 
						|
        @base(title, data) {
 | 
						|
            <form action={ templ.URL(postUrl) } method="post">
 | 
						|
                @commentForm(form)
 | 
						|
            </form>
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |