Skip to content

Commit

Permalink
Redirect to the collab session if in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelim01 committed Nov 13, 2024
1 parent 48066d2 commit 5560837
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
1 change: 1 addition & 0 deletions frontend/src/_services/history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 26 additions & 23 deletions frontend/src/app/account/history/history.component.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -34,6 +35,7 @@ export class HistoryComponent implements OnInit {
private historyService: HistoryService,
private messageService: MessageService,
private datePipe: DatePipe,
private router: Router,
) {}

ngOnInit() {
Expand All @@ -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);
}
}

Expand All @@ -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,
},
});
}
}
13 changes: 7 additions & 6 deletions frontend/src/app/account/history/history.model.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { DifficultyLevels } from '../../questions/difficulty-levels.enum';

export enum statusValues {
export enum HistoryStatus {
COMPLETED = 'COMPLETED',
FORFEITED = 'FORFEITED',
IN_PROGRESS = 'IN_PROGRESS',
}

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 {
Expand Down Expand Up @@ -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[];
}

0 comments on commit 5560837

Please sign in to comment.