/******************************************************* * File: msg.js * Author: Anthony Debucquoy * Scope: Extension messagerie * Description: Messages frontend api consumer *******************************************************/ import { restGet, restPost, restPatch } from './restConsumer.js' import { ref } from 'vue' /** * - id * - name * - members */ export const discussionsList = ref(); export const currentDiscussion = ref([]); let timerSet = false export async function createDiscussion(name){ let disc = await restPost("/discussion", {name: name}); discussionsList.value.push(disc); } export async function invite(id, regNo){ restPatch("/discussion/"+ id+ "/add", {regNo: parseInt(regNo)}).then(() => fetchDiscussion(id)) } export async function removeMember(id, regNo){ restPatch("/discussion/"+ id+ "/remove", {regNo: parseInt(regNo)}).then(() => fetchDiscussion(id)) } export async function sendMessage(id, content, responseId){ let data = { content: content, response: responseId, } restPost("/discussion/" + id, data).then(() => fetchDiscussion(id)); } 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); if(!timerSet){ timerSet = true; setTimeout(() => {timerSet = false;fetchDiscussion(currentDiscussion.value.id)} , 5000); } } await fetchDiscussions();