Add getting chat history logic

This commit is contained in:
Hepatica
2024-01-27 03:17:59 +01:00
parent 6d29ac44b9
commit 0151af15bd
2 changed files with 66 additions and 30 deletions

View File

@@ -49,6 +49,26 @@ export async function GetCurrentMoney(authToken, searchToken) {
}
}
export async function GetChatHistory() {
try {
const response = await fetch(`${BackendApiUrl}/ChatHistory/GetChatHistory`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: "",
redirect: "follow"
});
if (!response.ok) {
console.log("Fetch error:", response.status);
}
return await response.json();
} catch (error) {
console.log("Fetch error:", error);
}
}

View File

@@ -10,13 +10,10 @@
<div class="chat__content--users">
<ul>
<transition-group name="fade">
<li
v-for="msg in array"
:key="msg"
>
<li v-for="msg in array" :key="msg">
<div class="card__user">
<div class="user__icon">
<img :src="msg.icon">
<img :src="msg.icon" />
</div>
<div class="content">
<h1>{{ msg.username }}</h1>
@@ -32,54 +29,73 @@
</template>
<script>
import '@/assets/css/ComponentsStyles/chat.css'
import "@/assets/css/ComponentsStyles/chat.css";
import { GetChatHistory } from "@/assets/js/rest/RestMethods.js";
import WritechatComponent from "@/components/WritechatComponent.vue";
import { SendMessageToChat } from "@/assets/js/chat/ChatLogic.js";
import {eventBus} from "@/main";
import { eventBus } from "@/main";
export default {
components: { WritechatComponent },
inject: [ 'eventBus' ],
inject: ["eventBus"],
data() {
return {
array: [],
id: 0,
}
};
},
methods: {
ClaimDatamsg(msg) {
SendMessageToChat(msg[0]);
// if(this.array.length > 7) {
// this.array.shift()
// }
}
},
},
mounted() {
eventBus.on('dataChat', (dataFromServer) => {
eventBus.on("dataChat", (dataFromServer) => {
try {
// Attempt to parse the JSON string
const dataObject = JSON.parse(dataFromServer);
let imageUrl = "https://avatar.spworlds.ru/face/55/" + dataObject.SpUserName;
// Attempt to parse the JSON string
const dataObject = JSON.parse(dataFromServer);
let imageUrl =
"https://avatar.spworlds.ru/face/55/" + dataObject.SpUserName;
const MsgUser = {
id: this.id + 1,
msg: dataObject.Message,
username: dataObject.SpUserName,
icon: imageUrl
};
const MsgUser = {
id: this.id + 1,
msg: dataObject.Message,
username: dataObject.SpUserName,
icon: imageUrl,
};
this.array.push(MsgUser);
} catch (error) {
console.error('Error parsing JSON data:', error);
}
})
}
}
this.array.push(MsgUser);
} catch (error) {
console.error("Error parsing JSON data:", error);
}
});
},
created() {
GetChatHistory().then((response) => {
response.forEach((element) => {
let imageUrl =
"https://avatar.spworlds.ru/face/55/" + element.userName;
const MsgUser = {
id: this.id + 1,
msg: element.message,
username: element.userName,
icon: imageUrl,
};
this.array.push(MsgUser);
});
this.balance = response.currentMoney;
});
},
};
</script>
<style scoped>
</style>