first commit

This commit is contained in:
wilfried 2021-05-27 10:53:34 +02:00
parent 6984c550fb
commit 595d0e3013
47 changed files with 13471 additions and 0 deletions

View 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>

View 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();
});
});

View 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 = '';
}
}