From 1a607e30e0620fcc865fa6caf8e418cb6e09303c Mon Sep 17 00:00:00 2001 From: arzumify Date: Tue, 12 Nov 2024 20:56:23 +0000 Subject: [PATCH] FAILING: This commit doesn't work, but I want to go to sleep --- server/main.go | 8 +++----- web/wasm/app.go | 47 +++++++++++++++++++++-------------------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/server/main.go b/server/main.go index 305bccd..aa372cb 100644 --- a/server/main.go +++ b/server/main.go @@ -412,7 +412,7 @@ func main() { return } - var messages []map[string]interface{} + messages := make(map[string]interface{}) rows, err := conn.Query("SELECT sender, senderName, message, sent, id FROM messages ORDER BY sent ASC") if err != nil { w.WriteHeader(http.StatusInternalServerError) @@ -435,13 +435,12 @@ func main() { return } - messages = append(messages, map[string]interface{}{ + messages[id.String()] = map[string]interface{}{ "sender": sender.String(), "name": senderName, "message": message, "sent": sent, - "id": id.String(), - }) + } } w.WriteHeader(http.StatusOK) @@ -502,7 +501,6 @@ func main() { }() w.WriteHeader(http.StatusOK) - renderJSON(map[string]interface{}{"id": messageId.String()}, w) }) router.Post("/api/delete", func(w http.ResponseWriter, r *http.Request) { diff --git a/web/wasm/app.go b/web/wasm/app.go index d7d4c70..d5207fc 100644 --- a/web/wasm/app.go +++ b/web/wasm/app.go @@ -8,7 +8,7 @@ import ( "time" ) -var messages []map[string]interface{} +var messageDivs = make(map[string]js.Value) func refreshMessages(localStorage js.Value, messageBox js.Value, userId string) { jsonBody, err := json.Marshal(map[string]interface{}{ @@ -39,7 +39,7 @@ func refreshMessages(localStorage js.Value, messageBox js.Value, userId string) return } - var body []map[string]interface{} + var body map[string]interface{} err = json.NewDecoder(response.Body).Decode(&body) if err != nil { alert("Failed to decode response: " + err.Error()) @@ -50,16 +50,28 @@ func refreshMessages(localStorage js.Value, messageBox js.Value, userId string) messageBox.Get("lastChild").Get("style").Set("margin-bottom", "15px") } - if len(messages) < len(body) { - for _, message := range body[len(messages):] { - messageDiv := createMessage(message["message"].(string), message["name"].(string), message["sender"].(string) == userId, message["id"].(string), time.Unix(int64(message["sent"].(float64)), 0)) - messageBox.Call("appendChild", messageDiv) + if len(messageDivs) > len(body) { + for id, _ := range messageDivs { + _, exists := body[id] + if !exists { + messageDivs[id].Call("remove") + delete(messageDivs, id) + } } - } else { - } - messageBox.Get("lastChild").Get("style").Set("margin-bottom", "22.5px") + for id, message := range body { + _, exists := messageDivs[id] + if !exists { + messageDiv := createMessage(message.(map[string]interface{})["message"].(string), message.(map[string]interface{})["name"].(string), message.(map[string]interface{})["sender"].(string) == userId, message.(map[string]interface{})["id"].(string), time.Unix(int64(message.(map[string]interface{})["sent"].(float64)), 0)) + messageBox.Call("appendChild", messageDiv) + messageDivs[id] = messageDiv + } + } + + if !messageBox.Get("lastChild").IsNull() { + messageBox.Get("lastChild").Get("style").Set("margin-bottom", "22.5px") + } messageBox.Set("scrollTop", messageBox.Get("scrollTopMax").Float()) } @@ -148,7 +160,6 @@ func main() { return } - username := localStorage.Call("getItem", "username").String() userId := localStorage.Call("getItem", "userId").String() login.Get("style").Set("display", "none") @@ -193,22 +204,6 @@ func main() { } return } - - var body map[string]interface{} - err = json.NewDecoder(response.Body).Decode(&body) - if err != nil { - alert("Failed to decode response: " + err.Error()) - return - } - - if !messageBox.Get("lastChild").IsNull() { - messageBox.Get("lastChild").Get("style").Set("margin-bottom", "15px") - } - - message := createMessage(sendField.Get("value").String(), username, true, body["id"].(string), time.Now()) - message.Get("style").Set("margin-bottom", "22.5px") - messageBox.Call("appendChild", message) - messageBox.Set("scrollTop", messageBox.Get("scrollTopMax").Float()) }() return nil }))