Clyde/frontend/src/rest/msg.js

61 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-03-22 13:54:52 +01:00
/*******************************************************
* File: msg.js
* Author: Anthony Debucquoy
* Scope: Extension messagerie
* Description: Messages frontend api consumer
*******************************************************/
2024-03-27 19:52:48 +01:00
import { restGet, restPost, restPatch } from './restConsumer.js'
import { ref } from 'vue'
/**
* - id
* - name
* - members
*/
2024-03-27 23:54:59 +01:00
export const discussionsList = ref();
export const currentDiscussion = ref([]);
2024-04-02 10:18:50 +02:00
let timerSet = false
2024-03-22 13:54:52 +01:00
export async function createDiscussion(name){
2024-03-27 19:52:48 +01:00
let disc = await restPost("/discussion", {name: name});
discussionsList.value.push(disc);
}
export async function invite(id, regNo){
2024-03-27 23:54:59 +01:00
restPatch("/discussion/"+ id+ "/add", {regNo: parseInt(regNo)}).then(() => fetchDiscussion(id))
}
2024-03-27 19:52:48 +01:00
2024-03-29 14:30:46 +01:00
export async function removeMember(id, regNo){
restPatch("/discussion/"+ id+ "/remove", {regNo: parseInt(regNo)}).then(() => fetchDiscussion(id))
}
2024-03-27 19:52:48 +01:00
export async function sendMessage(id, content, responseId){
let data = {
content: content,
response: responseId,
}
2024-03-29 14:31:08 +01:00
restPost("/discussion/" + id, data).then(() => fetchDiscussion(id));
2024-03-27 19:52:48 +01:00
}
export async function updateDiscussionName(id, name){
restPatch("/discussion/" + id, {name: name}).then(() => fetchDiscussions());
}
async function fetchDiscussions(){
discussionsList.value = await restGet("/discussions");
}
export async function fetchDiscussion(id){
currentDiscussion.value = await restGet("/discussion/" + id);
2024-04-02 10:18:50 +02:00
if(!timerSet){
timerSet = true;
setTimeout(() => {timerSet = false;fetchDiscussion(currentDiscussion.value.id)} , 5000);
}
2024-03-27 19:52:48 +01:00
}
await fetchDiscussions();