first commit
This commit is contained in:
parent
6984c550fb
commit
595d0e3013
47 changed files with 13471 additions and 0 deletions
22
frontend/src/app/app-routing.module.ts
Normal file
22
frontend/src/app/app-routing.module.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { NgModule } from '@angular/core';
|
||||
import { RouterModule, Routes } from '@angular/router';
|
||||
import {AppComponent} from "./app.component";
|
||||
import {LoginComponent} from "./login/login.component";
|
||||
import {PrivateComponent} from "./private/private.component";
|
||||
|
||||
const routes: Routes = [
|
||||
{
|
||||
path: 'login',
|
||||
component: LoginComponent,
|
||||
},
|
||||
{
|
||||
path: 'private',
|
||||
component: PrivateComponent,
|
||||
}
|
||||
];
|
||||
|
||||
@NgModule({
|
||||
imports: [RouterModule.forRoot(routes)],
|
||||
exports: [RouterModule]
|
||||
})
|
||||
export class AppRoutingModule { }
|
||||
1
frontend/src/app/app.component.html
Normal file
1
frontend/src/app/app.component.html
Normal file
|
|
@ -0,0 +1 @@
|
|||
<router-outlet></router-outlet>
|
||||
0
frontend/src/app/app.component.scss
Normal file
0
frontend/src/app/app.component.scss
Normal file
35
frontend/src/app/app.component.spec.ts
Normal file
35
frontend/src/app/app.component.spec.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
import { AppComponent } from './app.component';
|
||||
|
||||
describe('AppComponent', () => {
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [
|
||||
RouterTestingModule
|
||||
],
|
||||
declarations: [
|
||||
AppComponent
|
||||
],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
it('should create the app', () => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.componentInstance;
|
||||
expect(app).toBeTruthy();
|
||||
});
|
||||
|
||||
it(`should have as title 'frontend'`, () => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.componentInstance;
|
||||
expect(app.title).toEqual('frontend');
|
||||
});
|
||||
|
||||
it('should render title', () => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
fixture.detectChanges();
|
||||
const compiled = fixture.nativeElement;
|
||||
expect(compiled.querySelector('.content span').textContent).toContain('frontend app is running!');
|
||||
});
|
||||
});
|
||||
10
frontend/src/app/app.component.ts
Normal file
10
frontend/src/app/app.component.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Component } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.component.html',
|
||||
styleUrls: ['./app.component.scss']
|
||||
})
|
||||
export class AppComponent {
|
||||
title = 'frontend';
|
||||
}
|
||||
35
frontend/src/app/app.module.ts
Normal file
35
frontend/src/app/app.module.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { NgModule } from '@angular/core';
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
import {HttpClientModule} from "@angular/common/http";
|
||||
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
|
||||
|
||||
|
||||
import { AppRoutingModule } from './app-routing.module';
|
||||
import { AppComponent } from './app.component';
|
||||
import { LoginComponent } from './login/login.component';
|
||||
import { GeneralComponent } from './general/general.component';
|
||||
import { PrivateComponent } from './private/private.component';
|
||||
import { NavbarComponent } from './navbar/navbar.component';
|
||||
import {CommonModule} from "@angular/common";
|
||||
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
AppComponent,
|
||||
LoginComponent,
|
||||
GeneralComponent,
|
||||
PrivateComponent,
|
||||
NavbarComponent
|
||||
],
|
||||
imports: [
|
||||
BrowserModule,
|
||||
HttpClientModule,
|
||||
FormsModule,
|
||||
CommonModule,
|
||||
ReactiveFormsModule,
|
||||
AppRoutingModule
|
||||
],
|
||||
providers: [],
|
||||
bootstrap: [AppComponent]
|
||||
})
|
||||
export class AppModule { }
|
||||
1
frontend/src/app/general/general.component.html
Normal file
1
frontend/src/app/general/general.component.html
Normal file
|
|
@ -0,0 +1 @@
|
|||
<p>general works!</p>
|
||||
0
frontend/src/app/general/general.component.scss
Normal file
0
frontend/src/app/general/general.component.scss
Normal file
25
frontend/src/app/general/general.component.spec.ts
Normal file
25
frontend/src/app/general/general.component.spec.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { GeneralComponent } from './general.component';
|
||||
|
||||
describe('GeneralComponent', () => {
|
||||
let component: GeneralComponent;
|
||||
let fixture: ComponentFixture<GeneralComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
declarations: [ GeneralComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(GeneralComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
15
frontend/src/app/general/general.component.ts
Normal file
15
frontend/src/app/general/general.component.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import { Component, OnInit } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-general',
|
||||
templateUrl: './general.component.html',
|
||||
styleUrls: ['./general.component.scss']
|
||||
})
|
||||
export class GeneralComponent implements OnInit {
|
||||
|
||||
constructor() { }
|
||||
|
||||
ngOnInit(): void {
|
||||
}
|
||||
|
||||
}
|
||||
23
frontend/src/app/login/login.component.html
Normal file
23
frontend/src/app/login/login.component.html
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<div class="container">
|
||||
<div class="modal-header">
|
||||
<h4 class="modal-title">Login</h4>
|
||||
</div>
|
||||
|
||||
<div class="modal-body">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="form-group">
|
||||
<input type="text"
|
||||
placeholder="Enter your phone number"
|
||||
class="form-control"
|
||||
[(ngModel)]="phone">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-sm btn-primary" (click) = "login()">Login</button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
0
frontend/src/app/login/login.component.scss
Normal file
0
frontend/src/app/login/login.component.scss
Normal file
25
frontend/src/app/login/login.component.spec.ts
Normal file
25
frontend/src/app/login/login.component.spec.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { LoginComponent } from './login.component';
|
||||
|
||||
describe('LoginComponent', () => {
|
||||
let component: LoginComponent;
|
||||
let fixture: ComponentFixture<LoginComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
declarations: [ LoginComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(LoginComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
50
frontend/src/app/login/login.component.ts
Normal file
50
frontend/src/app/login/login.component.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import { Component, OnInit } from '@angular/core';
|
||||
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
|
||||
import {Router} from "@angular/router";
|
||||
import {AuthService} from "../services/auth/auth.service";
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
templateUrl: './login.component.html',
|
||||
styleUrls: ['./login.component.scss']
|
||||
})
|
||||
export class LoginComponent implements OnInit {
|
||||
|
||||
|
||||
public phone = '';
|
||||
|
||||
|
||||
constructor(
|
||||
private auth: AuthService,
|
||||
private router: Router
|
||||
) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
}
|
||||
|
||||
|
||||
login() : void {
|
||||
console.log('1 -Phone :', this.phone);
|
||||
if (this.auth.sendAuthentication(this.phone) === true){
|
||||
this.router.navigateByUrl('/private');
|
||||
} else {
|
||||
console.log("error");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
1
frontend/src/app/navbar/navbar.component.html
Normal file
1
frontend/src/app/navbar/navbar.component.html
Normal file
|
|
@ -0,0 +1 @@
|
|||
<p>navbar works!</p>
|
||||
0
frontend/src/app/navbar/navbar.component.scss
Normal file
0
frontend/src/app/navbar/navbar.component.scss
Normal file
25
frontend/src/app/navbar/navbar.component.spec.ts
Normal file
25
frontend/src/app/navbar/navbar.component.spec.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { NavbarComponent } from './navbar.component';
|
||||
|
||||
describe('NavbarComponent', () => {
|
||||
let component: NavbarComponent;
|
||||
let fixture: ComponentFixture<NavbarComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
declarations: [ NavbarComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(NavbarComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
15
frontend/src/app/navbar/navbar.component.ts
Normal file
15
frontend/src/app/navbar/navbar.component.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import { Component, OnInit } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-navbar',
|
||||
templateUrl: './navbar.component.html',
|
||||
styleUrls: ['./navbar.component.scss']
|
||||
})
|
||||
export class NavbarComponent implements OnInit {
|
||||
|
||||
constructor() { }
|
||||
|
||||
ngOnInit(): void {
|
||||
}
|
||||
|
||||
}
|
||||
57
frontend/src/app/private/private.component.html
Normal file
57
frontend/src/app/private/private.component.html
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<div class="container-fluid" *ngIf="showScreen">
|
||||
<div class="row">
|
||||
|
||||
<div class="col-md-4">
|
||||
|
||||
<div class="user-list-card">
|
||||
<div class="user-card"
|
||||
[ngClass]="{'active' : user?.phone === selectedUser?.phone}"
|
||||
*ngFor="let user of userList"
|
||||
(click)="selectUserHandler(user.phone)"
|
||||
>
|
||||
<!---->
|
||||
|
||||
<img src="./assets/image/user.png" height="25" width="25"/>
|
||||
<p class="username">{{user?.name}}</p>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="col-md-8">
|
||||
|
||||
<div class="chat-container">
|
||||
<ng-container *ngIf="selectedUser">
|
||||
<div class="chat-header">
|
||||
<p class="username">{{selectedUser?.name}}</p>
|
||||
</div>
|
||||
|
||||
<div class="chat-body">
|
||||
<div *ngFor="let item of messageArray"
|
||||
[ngClass]="{'same-user' : item?.user === currentUser?.name}"
|
||||
>
|
||||
<!---->
|
||||
|
||||
<p class="message-container">{{item?.message}}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="chat-footer">
|
||||
<div class="row">
|
||||
<div class="col-md-10">
|
||||
<div class="form-group mb-0">
|
||||
<input type="text" placeholder="Type a message" class="form-control"
|
||||
[(ngModel)]="messageText" (keyup)="$event.keyCode === 13 && sendMessage()"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-2 text-center align-self-center">
|
||||
<button class="btn btn-primary btn-bm px-3 " (click)="sendMessage()">Send</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</ng-container>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
0
frontend/src/app/private/private.component.scss
Normal file
0
frontend/src/app/private/private.component.scss
Normal file
25
frontend/src/app/private/private.component.spec.ts
Normal file
25
frontend/src/app/private/private.component.spec.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { PrivateComponent } from './private.component';
|
||||
|
||||
describe('PrivateComponent', () => {
|
||||
let component: PrivateComponent;
|
||||
let fixture: ComponentFixture<PrivateComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
declarations: [ PrivateComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(PrivateComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
143
frontend/src/app/private/private.component.ts
Normal file
143
frontend/src/app/private/private.component.ts
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
import {NgModule, Component, OnInit} from '@angular/core';
|
||||
import {ChatService} from "../services/chat/chat.service";
|
||||
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'app-private',
|
||||
templateUrl: './private.component.html',
|
||||
styleUrls: ['./private.component.scss']
|
||||
})
|
||||
export class PrivateComponent implements OnInit {
|
||||
public userList = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Yuki Vachot',
|
||||
phone: '0608020103',
|
||||
roomId: {
|
||||
2: 'room-1',
|
||||
3: 'room-2',
|
||||
4: 'room-3'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: 'Wilfried Vallee',
|
||||
phone: '0604080701',
|
||||
roomId: {
|
||||
1: 'room-1',
|
||||
3: 'room-4',
|
||||
4: 'room-5'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: 'Khai Phan',
|
||||
phone: '0603050960',
|
||||
roomId: {
|
||||
1: 'room-2',
|
||||
2: 'room-4',
|
||||
4: 'room-6'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
// @ts-ignore
|
||||
public roomId: string;
|
||||
// @ts-ignore
|
||||
public messageText: string;
|
||||
public messageArray: {user: string, message: string}[] = [];
|
||||
private storageArray = [];
|
||||
|
||||
// @ts-ignore
|
||||
public showScreen: boolean;
|
||||
|
||||
// @ts-ignore
|
||||
public phone: string;
|
||||
// @ts-ignore
|
||||
public currentUser;
|
||||
// @ts-ignore
|
||||
public selectedUser;
|
||||
|
||||
|
||||
|
||||
constructor(
|
||||
private chatService: ChatService,
|
||||
|
||||
) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.chatService.getMessage()
|
||||
.subscribe((data: {user: string, message: string}) => {
|
||||
this.messageArray.push(data);
|
||||
if (this.roomId) {
|
||||
setTimeout( () => {
|
||||
this.storageArray = this.chatService.getStorage();
|
||||
//@ts-ignore
|
||||
const storeIndex = this.storageArray.findIndex((storage) => storage.roomId === this.roomId);
|
||||
//@ts-ignore
|
||||
this.messageArray = this.storageArray[storeIndex].chats;
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
selectUserHandler(phone: string): void {
|
||||
this.selectedUser = this.userList.find(user => user.phone === phone);
|
||||
this.roomId = this.selectedUser.roomId[this.currentUser.id];
|
||||
this.messageArray = [];
|
||||
|
||||
this.storageArray = this.chatService.getStorage();
|
||||
// @ts-ignore
|
||||
const storeIndex = this.storageArray.findIndex((storage) => storage.roomId === this.roomId);
|
||||
|
||||
if (storeIndex > -1) {
|
||||
// @ts-ignore
|
||||
this.messageArray = this.storageArray[storeIndex].chats;
|
||||
}
|
||||
|
||||
this.join(this.currentUser.name, this.roomId);
|
||||
}
|
||||
|
||||
join(username: string, roomId: string): void {
|
||||
this.chatService.joinRoom({user: username, room: roomId});
|
||||
}
|
||||
|
||||
sendMessage(): void {
|
||||
this.chatService.sendMessage({
|
||||
user: this.currentUser.name,
|
||||
room: this.roomId,
|
||||
message: this.messageText
|
||||
});
|
||||
|
||||
this.storageArray = this.chatService.getStorage();
|
||||
// @ts-ignore
|
||||
const storeIndex = this.storageArray.findIndex((storage) => storage.roomId === this.roomId);
|
||||
|
||||
if (storeIndex > -1) {
|
||||
// @ts-ignore
|
||||
this.storageArray[storeIndex].chats.push({
|
||||
user: this.currentUser.name,
|
||||
message: this.messageText
|
||||
})
|
||||
} else {
|
||||
const updateStorage = {
|
||||
roomId: this.roomId,
|
||||
chats: [{
|
||||
user: this.currentUser.name,
|
||||
message: this.messageText
|
||||
}]
|
||||
};
|
||||
// @ts-ignore
|
||||
this.storageArray.push(updateStorage);
|
||||
}
|
||||
this.chatService.setStorage(this.storageArray);
|
||||
this.messageText = '';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
16
frontend/src/app/services/auth/auth.service.spec.ts
Normal file
16
frontend/src/app/services/auth/auth.service.spec.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
describe('AuthService', () => {
|
||||
let service: AuthService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(AuthService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
79
frontend/src/app/services/auth/auth.service.ts
Normal file
79
frontend/src/app/services/auth/auth.service.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import {Observable} from "rxjs";
|
||||
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
|
||||
export class AuthService {
|
||||
// @ts-ignore
|
||||
// @ts-ignore
|
||||
public roomId: string;
|
||||
// @ts-ignore
|
||||
public messageText: string;
|
||||
public messageArray: {user: string, message: string}[] = [];
|
||||
private storageArray = [];
|
||||
|
||||
// @ts-ignore
|
||||
public showScreen: boolean;
|
||||
|
||||
// @ts-ignore
|
||||
public phone: string;
|
||||
// @ts-ignore
|
||||
public currentUser;
|
||||
// @ts-ignore
|
||||
public selectedUser;
|
||||
|
||||
public userList = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Yuki Vachot',
|
||||
phone: '0608020103',
|
||||
roomId: {
|
||||
2: 'room-1',
|
||||
3: 'room-2',
|
||||
4: 'room-3'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: 'Wilfried Vallee',
|
||||
phone: '0604080701',
|
||||
roomId: {
|
||||
1: 'room-1',
|
||||
3: 'room-4',
|
||||
4: 'room-5'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: 'Khai Phan',
|
||||
phone: '0603050960',
|
||||
roomId: {
|
||||
1: 'room-2',
|
||||
2: 'room-4',
|
||||
4: 'room-6'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
constructor() {}
|
||||
sendAuthentication(phone: string): boolean {
|
||||
this.phone = phone;
|
||||
this.currentUser = this.userList.find(user => user.phone === this.phone.toString());
|
||||
this.userList = this.userList.filter((user) => user.phone !== this.phone.toString());
|
||||
//console.log("2 -", this.currentUser.phone);
|
||||
console.log("3 -", this.phone);
|
||||
if(this.currentUser) {
|
||||
this.showScreen = true;
|
||||
console.log("user : TRUE", this.phone.toString());
|
||||
} else {
|
||||
console.log("error user", this.phone.toString());
|
||||
}
|
||||
return this.showScreen ;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
16
frontend/src/app/services/chat/chat.service.spec.ts
Normal file
16
frontend/src/app/services/chat/chat.service.spec.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ChatService } from './chat.service';
|
||||
|
||||
describe('ChatService', () => {
|
||||
let service: ChatService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(ChatService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
47
frontend/src/app/services/chat/chat.service.ts
Normal file
47
frontend/src/app/services/chat/chat.service.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { io, Socket } from "socket.io-client";
|
||||
import {Observable} from "rxjs";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ChatService {
|
||||
|
||||
private socket: Socket;
|
||||
private url = 'http://localhost:3000';
|
||||
|
||||
constructor() {
|
||||
this.socket = io(this.url);
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
joinRoom(data): void {
|
||||
this.socket.emit('join', data);
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
sendMessage(data): void{
|
||||
this.socket.emit('message', data);
|
||||
}
|
||||
|
||||
getMessage(): Observable<any> {
|
||||
return new Observable<{user: string, message: string}>(observer => {
|
||||
this.socket.on('new message', (data) => {
|
||||
observer.next(data);
|
||||
});
|
||||
return () => {
|
||||
this.socket.disconnect();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getStorage() {
|
||||
const storage = localStorage.getItem('chats');
|
||||
return storage ? JSON.parse(storage) : [];
|
||||
}
|
||||
|
||||
setStorage(data: any) {
|
||||
localStorage.setItem('chats', JSON.stringify(data));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in a new issue