The primary model users will interact with is now Websites, which will have a single guestbook associated with it (though this is not enforced by the database).
		
			
				
	
	
		
			170 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			170 lines
		
	
	
		
			5.3 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 gbList(guestbooks []models.Guestbook) {
 | 
						|
    if len(guestbooks) == 0 {
 | 
						|
        <p>No Guestbooks yet</p>
 | 
						|
    } else {
 | 
						|
        <ul id="guestbooks" hx-get="/guestbooks" hx-trigger="newGuestbook from:body" hx-swap="outerHTML">
 | 
						|
            for _, gb := range guestbooks {
 | 
						|
                <li><a href={ templ.URL(gbUrl(gb) + "/dashboard") }>
 | 
						|
                </a></li>
 | 
						|
 | 
						|
            }
 | 
						|
        </ul>
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
templ GuestbookList(title string, data CommonData, guestbooks []models.Guestbook) {
 | 
						|
    if data.IsHtmx {
 | 
						|
        @gbList(guestbooks)
 | 
						|
    } else {
 | 
						|
        @base(title, data) {
 | 
						|
            <h1>My Guestbooks</h1>
 | 
						|
            <div>
 | 
						|
                <button hx-get="/guestbooks/create" hx-target="closest div">New Guestbook</button>
 | 
						|
            </div>
 | 
						|
            @gbList(guestbooks)
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
templ GuestbookCreate(title string, data CommonData) {
 | 
						|
    if data.IsHtmx {
 | 
						|
        <form hx-post="/guestbooks/create" hx-target="closest div">
 | 
						|
            @gbCreateForm(data.CSRFToken)
 | 
						|
        </form>
 | 
						|
    } else {
 | 
						|
        @base(title, data) {
 | 
						|
            <form action="/guestbooks/create" method="post">
 | 
						|
                @gbCreateForm(data.CSRFToken)
 | 
						|
            </form>
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
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") }
 | 
						|
                        <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 {
 | 
						|
        <div id="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>
 | 
						|
        </div>
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
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>
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |