wip: update event

This commit is contained in:
Tigor Hutasuhut 2024-06-05 16:18:22 +07:00
parent 0c623d1bf1
commit d902a2b8c1
2 changed files with 72 additions and 16 deletions

View file

@ -4,6 +4,9 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"io" "io"
"github.com/tigorlazuardi/redmage/pkg/errs"
"github.com/tigorlazuardi/redmage/views/components/progress"
) )
type ImageDownloadEvent string type ImageDownloadEvent string
@ -31,12 +34,19 @@ type ImageDownload struct {
// Render the template. // Render the template.
func (im ImageDownload) Render(ctx context.Context, w io.Writer) error { func (im ImageDownload) Render(ctx context.Context, w io.Writer) error {
panic("not implemented") // TODO: Implement switch im.EventKind {
case ImageDownloadStart:
return progress.ImageDownloadStartNotification(progress.ImageDownloadStartNotificationData{}).Render(ctx, w)
case ImageDownloadEnd:
return progress.ImageDownloadEndNotification(progress.ImageDownloadEndNotificationData{}).Render(ctx, w)
default:
return errs.Fail("events.ImageDownload: unknown event kind", "event", im)
}
} }
// Event returns the event name // Event returns the event name
func (im ImageDownload) Event() string { func (im ImageDownload) Event() string {
return "image.download" return "image.download.notification"
} }
// SerializeTo writes the event data to the writer. // SerializeTo writes the event data to the writer.

View file

@ -1,23 +1,69 @@
package progress package progress
type ImageDownloadStartData struct { import "time"
ID string import "fmt"
Subreddit string
PostURL string type ImageDownloadStartNotificationData struct {
PostName string ID string
PostTitle string Subreddit string
PostURL string
PostName string
PostTitle string
AutoRemoveDuration time.Duration
} }
templ ImageDownloadStart(data ImageDownloadStartData) { templ ImageDownloadStartNotification(data ImageDownloadStartNotificationData) {
<div
id={ data.ID }
if data.AutoRemoveDuration > 0 {
x-data={ fmt.Sprintf("{ init() { setTimeout(() => $el.remove(), %d) }}", data.AutoRemoveDuration.Milliseconds()) }
}
onclick="this.remove()"
class="alert alert-info hover:bg-success-content transition-all"
>
<span>
<a
target="_blank"
href={ templ.SafeURL(fmt.Sprintf("https://www.reddit.com/r/%s", data.Subreddit)) }
>{ data.Subreddit }</a>:
Start Downloading
<a href={ templ.SafeURL(data.PostURL) }>{ truncateTitle(data.PostTitle) }</a>
</span>
</div>
} }
type ImageDownloadEndData struct { func truncateTitle(s string) string {
ID string if len(s) > 20 {
Subreddit string return s[:20] + "..."
PostURL string }
PostName string return s
PostTitle string
} }
templ ImageDownloadEnd(data ImageDownloadEndData) { type ImageDownloadEndNotificationData struct {
ID string
Subreddit string
PostURL string
PostName string
PostTitle string
AutoRemoveDuration time.Duration
}
templ ImageDownloadEndNotification(data ImageDownloadEndNotificationData) {
<div
id={ data.ID }
if data.AutoRemoveDuration > 0 {
x-data={ fmt.Sprintf("{ init() { setTimeout(() => $el.remove(), %d) }}", data.AutoRemoveDuration.Milliseconds()) }
}
onclick="this.remove()"
class="alert alert-info hover:bg-success-content transition-all"
>
<span>
<a
target="_blank"
href={ templ.SafeURL(fmt.Sprintf("https://www.reddit.com/r/%s", data.Subreddit)) }
>{ data.Subreddit }</a>:
Finished Downloading
<a href={ templ.SafeURL(data.PostURL) }>{ truncateTitle(data.PostTitle) }</a>
</span>
</div>
} }