From a1534405334c3a6d78be58279394e0580eb55d63 Mon Sep 17 00:00:00 2001 From: Michal Marhan Date: Fri, 15 Mar 2024 21:46:07 +0100 Subject: [PATCH] allow notifications to be added as another event With this, composing triggers like below will be possible trigger := htmx.NewTrigger() trigger.AddEvent("fl:record:updated") trigger.AddSuccess("Transcription updated") hx.TriggerWithObject(trigger) --- trigger.go | 55 +++++++++++++++++++++++++++++++++---------------- trigger_test.go | 5 ++--- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/trigger.go b/trigger.go index 28b9913..fba607a 100644 --- a/trigger.go +++ b/trigger.go @@ -48,6 +48,42 @@ func (t *Trigger) AddEventObject(event string, details map[string]any) *Trigger return t.add(eventContent{event: event, data: details}) } +func (t *Trigger) AddSuccess(message string, vars ...map[string]any) { + t.addNotifyObject(notificationSuccess, message, vars...) +} + +func (t *Trigger) AddInfo(message string, vars ...map[string]any) { + t.addNotifyObject(notificationInfo, message, vars...) +} + +func (t *Trigger) AddWarning(message string, vars ...map[string]any) { + t.addNotifyObject(notificationWarning, message, vars...) +} + +func (t *Trigger) AddError(message string, vars ...map[string]any) { + t.addNotifyObject(notificationError, message, vars...) +} + +func (t *Trigger) addNotifyObject(nt notificationType, message string, vars ...map[string]any) *Trigger { + details := map[string]any{ + notificationKeyLevel: nt, + notificationKeyMessage: message, + } + + if len(vars) > 0 { + for _, m := range vars { + for k, v := range m { + if k == notificationKeyLevel || k == notificationKeyMessage { + k = "_" + k + } + details[k] = v + } + } + } + + return t.AddEventObject(DefaultNotificationKey, details) +} + // String returns the string representation of the Trigger set func (t *Trigger) String() string { if t.onlySimple { @@ -90,24 +126,7 @@ func (n *notificationType) String() string { } func (h *Handler) notifyObject(nt notificationType, message string, vars ...map[string]any) { - details := map[string]any{ - notificationKeyLevel: nt, - notificationKeyMessage: message, - } - - if len(vars) > 0 { - for _, m := range vars { - for k, v := range m { - if k == notificationKeyLevel || k == notificationKeyMessage { - k = "_" + k - } - details[k] = v - } - } - } - - t := NewTrigger().AddEventObject(DefaultNotificationKey, details) - + t := NewTrigger().addNotifyObject(nt, message, vars...) h.TriggerWithObject(t) } diff --git a/trigger_test.go b/trigger_test.go index d84c519..5e970bd 100644 --- a/trigger_test.go +++ b/trigger_test.go @@ -52,9 +52,8 @@ func TestNewTriggerMixedNested(t *testing.T) { trigger.AddEvent("foo"). AddEventDetailed("bar", "baz"). AddEventDetailed("qux", "quux"). - AddEventObject("corge", map[string]any{"grault": "garply", "waldo": "fred", "plugh": "xyzzy", "thud": map[string]any{"foo": "bar", "baz": "qux"}}) - - expected := `{"bar":"baz","corge":{"grault":"garply","plugh":"xyzzy","thud":{"baz":"qux","foo":"bar"},"waldo":"fred"},"foo":"","qux":"quux"}` + AddEventObject("corge", map[string]any{"grault": "garply", "waldo": "fred", "plugh": "xyzzy", "thud": map[string]any{"foo": "bar", "baz": "qux"}}).AddSuccess("successfully tested", map[string]any{"foo": "bar", "baz": "qux"}) + expected := `{"bar":"baz","corge":{"grault":"garply","plugh":"xyzzy","thud":{"baz":"qux","foo":"bar"},"waldo":"fred"},"foo":"","qux":"quux","showMessage":{"baz":"qux","foo":"bar","level":"success","message":"successfully tested"}}` if trigger.String() != expected { t.Errorf("expected trigger to be %v, got %v", expected, trigger.String())