FAILING: This commit doesn't work, but I want to go to sleep

This commit is contained in:
Tracker-Friendly 2024-11-12 20:56:23 +00:00
parent 2588da14ee
commit 1a607e30e0
2 changed files with 24 additions and 31 deletions

View File

@ -412,7 +412,7 @@ func main() {
return 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") rows, err := conn.Query("SELECT sender, senderName, message, sent, id FROM messages ORDER BY sent ASC")
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@ -435,13 +435,12 @@ func main() {
return return
} }
messages = append(messages, map[string]interface{}{ messages[id.String()] = map[string]interface{}{
"sender": sender.String(), "sender": sender.String(),
"name": senderName, "name": senderName,
"message": message, "message": message,
"sent": sent, "sent": sent,
"id": id.String(), }
})
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
@ -502,7 +501,6 @@ func main() {
}() }()
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
renderJSON(map[string]interface{}{"id": messageId.String()}, w)
}) })
router.Post("/api/delete", func(w http.ResponseWriter, r *http.Request) { router.Post("/api/delete", func(w http.ResponseWriter, r *http.Request) {

View File

@ -8,7 +8,7 @@ import (
"time" "time"
) )
var messages []map[string]interface{} var messageDivs = make(map[string]js.Value)
func refreshMessages(localStorage js.Value, messageBox js.Value, userId string) { func refreshMessages(localStorage js.Value, messageBox js.Value, userId string) {
jsonBody, err := json.Marshal(map[string]interface{}{ jsonBody, err := json.Marshal(map[string]interface{}{
@ -39,7 +39,7 @@ func refreshMessages(localStorage js.Value, messageBox js.Value, userId string)
return return
} }
var body []map[string]interface{} var body map[string]interface{}
err = json.NewDecoder(response.Body).Decode(&body) err = json.NewDecoder(response.Body).Decode(&body)
if err != nil { if err != nil {
alert("Failed to decode response: " + err.Error()) 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") messageBox.Get("lastChild").Get("style").Set("margin-bottom", "15px")
} }
if len(messages) < len(body) { if len(messageDivs) > len(body) {
for _, message := range body[len(messages):] { for id, _ := range messageDivs {
messageDiv := createMessage(message["message"].(string), message["name"].(string), message["sender"].(string) == userId, message["id"].(string), time.Unix(int64(message["sent"].(float64)), 0)) _, exists := body[id]
if !exists {
messageDivs[id].Call("remove")
delete(messageDivs, id)
}
}
}
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) messageBox.Call("appendChild", messageDiv)
messageDivs[id] = messageDiv
} }
} else {
} }
if !messageBox.Get("lastChild").IsNull() {
messageBox.Get("lastChild").Get("style").Set("margin-bottom", "22.5px") messageBox.Get("lastChild").Get("style").Set("margin-bottom", "22.5px")
}
messageBox.Set("scrollTop", messageBox.Get("scrollTopMax").Float()) messageBox.Set("scrollTop", messageBox.Get("scrollTopMax").Float())
} }
@ -148,7 +160,6 @@ func main() {
return return
} }
username := localStorage.Call("getItem", "username").String()
userId := localStorage.Call("getItem", "userId").String() userId := localStorage.Call("getItem", "userId").String()
login.Get("style").Set("display", "none") login.Get("style").Set("display", "none")
@ -193,22 +204,6 @@ func main() {
} }
return 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 return nil
})) }))