From 5560837794c937f69ccc7bf043a7ff700abf6acc Mon Sep 17 00:00:00 2001 From: Samuel Lim Date: Wed, 13 Nov 2024 14:27:47 +0800 Subject: [PATCH] Redirect to the collab session if in progress --- frontend/src/_services/history.service.ts | 1 + .../app/account/history/history.component.ts | 49 ++++++++++--------- .../src/app/account/history/history.model.ts | 13 ++--- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/frontend/src/_services/history.service.ts b/frontend/src/_services/history.service.ts index 13bb68b79d..3ee0240ca3 100644 --- a/frontend/src/_services/history.service.ts +++ b/frontend/src/_services/history.service.ts @@ -20,6 +20,7 @@ export class HistoryService extends ApiService { map(response => response.data.map(item => ({ id: item._id, + roomId: item.roomId, collaborator: item.collaborator.username, question: item.question, topics: item.question.topics, diff --git a/frontend/src/app/account/history/history.component.ts b/frontend/src/app/account/history/history.component.ts index 2874256483..728a0b8782 100644 --- a/frontend/src/app/account/history/history.component.ts +++ b/frontend/src/app/account/history/history.component.ts @@ -1,7 +1,7 @@ import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; import { TableModule } from 'primeng/table'; import { CommonModule, DatePipe } from '@angular/common'; -import { MatchingHistory } from './history.model'; +import { HistoryStatus, MatchingHistory } from './history.model'; import { HistoryService } from '../../../_services/history.service'; import { MessageService } from 'primeng/api'; import { InputTextModule } from 'primeng/inputtext'; @@ -13,6 +13,7 @@ import { EditorState } from '@codemirror/state'; import { EditorView, basicSetup } from 'codemirror'; import { languageMap } from '../../collaboration/editor/languages'; import { ToastModule } from 'primeng/toast'; +import { Router } from '@angular/router'; @Component({ standalone: true, @@ -34,6 +35,7 @@ export class HistoryComponent implements OnInit { private historyService: HistoryService, private messageService: MessageService, private datePipe: DatePipe, + private router: Router, ) {} ngOnInit() { @@ -60,16 +62,11 @@ export class HistoryComponent implements OnInit { onRowSelect(history: MatchingHistory) { this.panelHistory = history; - if (history.code && history.language) { + if (history.status != HistoryStatus.IN_PROGRESS) { this.isPanelVisible = true; - this.initializeEditor(history.code, history.language); + this.initializeEditor(history.code as string, history.language as string); } else { - this.messageService.add({ - severity: 'warn', - summary: 'Code Not Found', - detail: 'Your collaboration session might not have ended', - life: 3000, - }); + this.redirectToCollab(history.roomId); } } @@ -79,21 +76,27 @@ export class HistoryComponent implements OnInit { } initializeEditor(code: string, language: string) { - if (this.editor) { - if (this.editorView) { - this.editorView.destroy(); - } + if (this.editorView) { + this.editorView.destroy(); + } - const languageExtension = languageMap[language] || languageMap['java']; - const state = EditorState.create({ - doc: code, - extensions: [basicSetup, languageExtension, oneDark, EditorView.editable.of(false)], - }); + const languageExtension = languageMap[language] || languageMap['java']; + const state = EditorState.create({ + doc: code, + extensions: [basicSetup, languageExtension, oneDark, EditorView.editable.of(false)], + }); - this.editorView = new EditorView({ - state, - parent: this.editor.nativeElement, - }); - } + this.editorView = new EditorView({ + state, + parent: this.editor.nativeElement, + }); + } + + redirectToCollab(collabId: string) { + this.router.navigate(['/collab'], { + queryParams: { + roomId: collabId, + }, + }); } } diff --git a/frontend/src/app/account/history/history.model.ts b/frontend/src/app/account/history/history.model.ts index 8e04a6848c..e0972b1411 100644 --- a/frontend/src/app/account/history/history.model.ts +++ b/frontend/src/app/account/history/history.model.ts @@ -1,6 +1,6 @@ import { DifficultyLevels } from '../../questions/difficulty-levels.enum'; -export enum statusValues { +export enum HistoryStatus { COMPLETED = 'COMPLETED', FORFEITED = 'FORFEITED', IN_PROGRESS = 'IN_PROGRESS', @@ -8,14 +8,15 @@ export enum statusValues { export interface MatchingHistory { id: string; + roomId: string; collaborator: string; // collaborator username question: Question; // question difficulty: DifficultyLevels; // question difficulty topics: string[]; // question topics - status: statusValues; // status of the session + status: HistoryStatus; // status of the session time: string | null; // time of the session - language: string | undefined; // language used during the session - code: string | undefined; // code written during the session + language?: string; // language used during the session + code?: string; // code written during the session } export interface User { @@ -43,14 +44,14 @@ export interface sessionHistory { user: User; collaborator: User; question: Question; - status: statusValues; + status: HistoryStatus; createdAt: string; updatedAt: string; snapshot?: Snapshot; } export interface historyResponse { - status: statusValues; + status: HistoryStatus; message: string; data: sessionHistory[]; }