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
}
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) {

View File

@ -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
}))