[U] deleteMessage

This commit is contained in:
Jan 2020-11-02 00:22:18 +01:00
parent 24cbd3b96c
commit e60084e919
4 changed files with 32 additions and 11 deletions

View file

@ -1,7 +1,7 @@
import { BrowserModule } from "@angular/platform-browser"; import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core"; import { NgModule } from "@angular/core";
import { NgbModalModule } from "@ng-bootstrap/ng-bootstrap"; import { NgbModalModule, NgbTooltipModule } from "@ng-bootstrap/ng-bootstrap";
import { AppRoutingModule } from './app-routing.module'; import { AppRoutingModule } from './app-routing.module';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
@ -34,13 +34,14 @@ import { InputManagerComponent } from './input-manager/input-manager.component';
ChannelComponent, ChannelComponent,
SidebarComponent, SidebarComponent,
DashboardComponent, DashboardComponent,
InputManagerComponent InputManagerComponent,
], ],
imports: [ imports: [
BrowserModule, BrowserModule,
NgbModalModule, NgbModalModule,
AppRoutingModule, AppRoutingModule,
FormsModule, FormsModule,
NgbTooltipModule,
], ],
providers: [ providers: [
MessageService, MessageService,

View file

@ -17,7 +17,7 @@
<div class="row messageRow"> <div class="row messageRow">
<div class="col-12 p-0"> <div class="col-12 p-0">
<ul class="list-group list-group-flush"> <ul class="list-group list-group-flush">
<li *ngFor="let m of messages" class="list-group-item"><small class="text-muted mr-1">{{ m.created_at | date : 'HH:MM' }}</small><strong class="mr-1">{{ m.user ? m.user.username : '...' }}</strong>{{m.message}}</li> <li *ngFor="let m of messages" class="list-group-item"><small class="text-muted mr-1" placement="right" [ngbTooltip]="m.created_at | date : 'dd.MM.yy - HH:mm'">{{ m.created_at | date : 'HH:mm' }}</small><strong class="mr-1">{{ m.user ? m.user.username : '...' }}</strong>{{m.message}}<small *ngIf="(user && user.id) === m.user_id" class="ml-1 float-right text-danger deleteMessage" title="Delete this message." (click)="deleteMessage(m)"><strong>X</strong></small></li>
</ul> </ul>
</div> </div>
</div> </div>

View file

@ -26,6 +26,10 @@
&::-webkit-scrollbar-button { &::-webkit-scrollbar-button {
display: none; display: none;
} }
.deleteMessage {
cursor: pointer;
}
} }
.inputRow { .inputRow {
width: 100%; width: 100%;

View file

@ -1,7 +1,5 @@
import { Component, OnInit } from "@angular/core"; import { Component, OnInit } from "@angular/core";
import { DomSanitizer } from '@angular/platform-browser';
import { ActivatedRoute, Router } from '@angular/router'; import { ActivatedRoute, Router } from '@angular/router';
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
import { SupabaseAuthUser } from '@supabase/supabase-js'; import { SupabaseAuthUser } from '@supabase/supabase-js';
import { Subject } from 'rxjs'; import { Subject } from 'rxjs';
import { take, takeUntil, map } from 'rxjs/operators'; import { take, takeUntil, map } from 'rxjs/operators';
@ -25,19 +23,21 @@ export class ChannelComponent implements OnInit {
$destroy: Subject<boolean> = new Subject(); $destroy: Subject<boolean> = new Subject();
dataUpdated: boolean = false; dataUpdated: boolean = false;
messageInput: string = ''; messageInput: string = '';
user: SupabaseAuthUser;
constructor( constructor(
private channelService: ChannelService, private channelService: ChannelService,
private modalService: NgbModal,
private supaService: SupaService, private supaService: SupaService,
private route: ActivatedRoute, private route: ActivatedRoute,
private sanitizer: DomSanitizer,
private userService: UserService, private userService: UserService,
private router: Router, private router: Router,
private messageService: MessageService, private messageService: MessageService,
) { } ) { }
ngOnInit() { ngOnInit() {
this.supaService.client.auth.user()
.then(user => this.user = user)
.catch(error => console.error(error));
this.route.params.subscribe( this.route.params.subscribe(
params => { params => {
this.reset(); this.reset();
@ -90,12 +90,11 @@ export class ChannelComponent implements OnInit {
); );
} }
async sendMessage(text: string) { sendMessage(text: string) {
if (!this.channel || !text) { if (!this.channel || !text || !this.user) {
return; return;
} }
const user: SupabaseAuthUser = await this.supaService.client.auth.user(); const message = new Message(this.channel.id, this.user.id, text);
const message = new Message(this.channel.id, user.id, text);
this.messageService.addOne(message).pipe(take(1)).subscribe( this.messageService.addOne(message).pipe(take(1)).subscribe(
data => { data => {
console.log('Success', data); console.log('Success', data);
@ -106,6 +105,23 @@ export class ChannelComponent implements OnInit {
); );
} }
deleteMessage(message: Message) {
if (!message) {
return;
}
const confirm = window.confirm(`Do you want to delete the message "${message.message}"?`)
if (confirm) {
this.messageService.deleteOne(message).pipe(take(1)).subscribe(
data => {
console.log('Success', data);
},
error => {
console.error('Failed', error);
}
);
}
}
updateChannel(name:string, desc:string) { updateChannel(name:string, desc:string) {
this.channel.name = name; this.channel.name = name;
this.channel.description = desc; this.channel.description = desc;