Renamed DmList to ContactList and updated logic to V3
This commit is contained in:
31
src/app/chat/contact-list/contact-list.html
Normal file
31
src/app/chat/contact-list/contact-list.html
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<button disabled tuiButton appearance="secondary" iconStart="@tui.mail-plus">
|
||||||
|
{{ "chat.chatnav.dmList.newChat"|translate }}
|
||||||
|
</button>
|
||||||
|
@if (cache.contactsState() == LoadState.CACHE_UPDATING) {
|
||||||
|
<div id="loader">
|
||||||
|
<tui-loader/>
|
||||||
|
{{ "updating"|translate }}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
@for (contact of cache.contacts(); track contact.contactId) {
|
||||||
|
<button [class.enlarge]="breakpoint() == 'mobile'" tuiButton
|
||||||
|
[appearance]="router.url == '/chat/dm/' + contact.contactId ? 'primary' : 'flat'"
|
||||||
|
[routerLink]="'/chat/dm/' + contact.contactId">
|
||||||
|
<avatar height="35px" width="35px" [radius]="10" [src]="contact.pfp" [name]="contact.username"></avatar>
|
||||||
|
<div class="info">
|
||||||
|
@if (contact.displayName == "") {
|
||||||
|
<span>{{ '@' + contact.username }}</span>
|
||||||
|
} @else {
|
||||||
|
<span>{{ contact.displayName }}</span>
|
||||||
|
}
|
||||||
|
@if (contact.latestMessage) {
|
||||||
|
<span class="latest_message">
|
||||||
|
@if (contact.latestMessage.isAuthor) {
|
||||||
|
{{ "chat.chatnav.dmList.messageBox.latestMessage.you"|translate }}
|
||||||
|
}
|
||||||
|
{{ contact.latestMessage.message }}
|
||||||
|
</span>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
}
|
||||||
@@ -1,17 +1,17 @@
|
|||||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
import { DmList } from './dm-list';
|
import { ContactList } from './contact-list';
|
||||||
|
|
||||||
describe('PictureList', () => {
|
describe('PictureList', () => {
|
||||||
let component: DmList;
|
let component: ContactList;
|
||||||
let fixture: ComponentFixture<DmList>;
|
let fixture: ComponentFixture<ContactList>;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await TestBed.configureTestingModule({
|
await TestBed.configureTestingModule({
|
||||||
imports: [DmList],
|
imports: [ContactList],
|
||||||
}).compileComponents();
|
}).compileComponents();
|
||||||
|
|
||||||
fixture = TestBed.createComponent(DmList);
|
fixture = TestBed.createComponent(ContactList);
|
||||||
component = fixture.componentInstance;
|
component = fixture.componentInstance;
|
||||||
await fixture.whenStable();
|
await fixture.whenStable();
|
||||||
});
|
});
|
||||||
61
src/app/chat/contact-list/contact-list.ts
Normal file
61
src/app/chat/contact-list/contact-list.ts
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import {Component, inject, input, OnInit} from '@angular/core';
|
||||||
|
import {ChatService} from '@chatenium/chatenium-sdk/services/chatService';
|
||||||
|
import {IndexedDB} from '../../storage/indexed-db';
|
||||||
|
import { TUI_BREAKPOINT, TuiButton, TuiLoader, TuiNotificationService } from '@taiga-ui/core';
|
||||||
|
import {Oimg} from '../elements/oimg/oimg';
|
||||||
|
import {Router, RouterLink} from '@angular/router';
|
||||||
|
import {TranslatePipe} from '@ngx-translate/core';
|
||||||
|
import {LoadStatus, ServiceManager} from '../../service-manager';
|
||||||
|
import { Contact, createContactWrapper } from 'chatenium-sdk-preview';
|
||||||
|
import { CHATENIUM_SDK } from '../../chatenium.token';
|
||||||
|
import { Cache, LoadState } from '../../cache';
|
||||||
|
import { Avatar } from '../elements/avatar/avatar';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-contact-list',
|
||||||
|
imports: [TuiButton, Oimg, RouterLink, TranslatePipe, TuiLoader, Avatar],
|
||||||
|
templateUrl: './contact-list.html',
|
||||||
|
styleUrl: './contact-list.scss',
|
||||||
|
})
|
||||||
|
export class ContactList implements OnInit {
|
||||||
|
SDK = inject(CHATENIUM_SDK);
|
||||||
|
idb = inject(IndexedDB);
|
||||||
|
router = inject(Router);
|
||||||
|
cache = inject(Cache);
|
||||||
|
notification = inject(TuiNotificationService);
|
||||||
|
serviceManager = inject(ServiceManager);
|
||||||
|
breakpoint = inject(TUI_BREAKPOINT);
|
||||||
|
|
||||||
|
async ngOnInit() {
|
||||||
|
if (this.cache.contactsState() != LoadState.LOADED) {
|
||||||
|
try {
|
||||||
|
const contacts = (await this.idb.getAll('contacts')) as Contact[];
|
||||||
|
this.cache.contacts.set(contacts);
|
||||||
|
this.cache.contactsState.set(LoadState.CACHE_UPDATING);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(`Contacts cache load failed: ${e}. Skipping cache load...`);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const resp = await this.SDK.contacts.getContacts();
|
||||||
|
const data = resp.data as Contact[];
|
||||||
|
if (data == null) {
|
||||||
|
console.log("No contacts were fetched - user possibly has 0 contacts")
|
||||||
|
this.cache.contactsState.set(LoadState.LOADED);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
this.cache.contacts.set(data);
|
||||||
|
this.cache.contactsState.set(LoadState.LOADED);
|
||||||
|
console.log(`Successfully fetched ${data.length} contacts`);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
this.serviceManager.chatsStatus.set(LoadStatus.error);
|
||||||
|
this.notification
|
||||||
|
.open(`Loading contacts failed: ${e}`, { appearance: 'negative' })
|
||||||
|
.subscribe();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected readonly LoadStatus = LoadStatus;
|
||||||
|
protected readonly LoadState = LoadState;
|
||||||
|
}
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
<button disabled tuiButton appearance="secondary" iconStart="@tui.mail-plus">
|
|
||||||
{{ "chat.chatnav.dmList.newChat"|translate }}
|
|
||||||
</button>
|
|
||||||
@if (serviceManager.chatsStatus() == LoadStatus.updating) {
|
|
||||||
<div id="loader">
|
|
||||||
<tui-loader/>
|
|
||||||
{{ "updating"|translate }}
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
@for (chat of serviceManager.chats(); track chat.chatid) {
|
|
||||||
<button [class.enlarge]="breakpoint() == 'mobile'" tuiButton
|
|
||||||
[appearance]="router.url == '/chat/dm/' + chat.chatid ? 'primary' : 'flat'"
|
|
||||||
[routerLink]="'/chat/dm/' + chat.chatid">
|
|
||||||
<oimg [src]="chat.pfp" height="35px" width="35px" [radius]="10"></oimg>
|
|
||||||
<div class="info">
|
|
||||||
@if (chat.displayName == "") {
|
|
||||||
<span>{{ '@' + chat.username }}</span>
|
|
||||||
} @else {
|
|
||||||
<span>{{ chat.displayName }}</span>
|
|
||||||
}
|
|
||||||
@if (chat.latestMessage) {
|
|
||||||
<span class="latest_message">
|
|
||||||
@if (chat.latestMessage.isAuthor) {
|
|
||||||
{{ "chat.chatnav.dmList.messageBox.latestMessage.you"|translate }}
|
|
||||||
}
|
|
||||||
{{ chat.latestMessage.message }}
|
|
||||||
</span>
|
|
||||||
}
|
|
||||||
</div>
|
|
||||||
</button>
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
import {Component, inject, input, OnInit} from '@angular/core';
|
|
||||||
import {ChatService} from '@chatenium/chatenium-sdk/services/chatService';
|
|
||||||
import {IndexedDB} from '../../storage/indexed-db';
|
|
||||||
import {TUI_BREAKPOINT, TuiButton, TuiLoader} from '@taiga-ui/core';
|
|
||||||
import {Oimg} from '../elements/oimg/oimg';
|
|
||||||
import {Router, RouterLink} from '@angular/router';
|
|
||||||
import {TranslatePipe} from '@ngx-translate/core';
|
|
||||||
import {LoadStatus, ServiceManager} from '../../service-manager';
|
|
||||||
|
|
||||||
@Component({
|
|
||||||
selector: 'app-dm-list',
|
|
||||||
imports: [
|
|
||||||
TuiButton,
|
|
||||||
Oimg,
|
|
||||||
RouterLink,
|
|
||||||
TranslatePipe,
|
|
||||||
TuiLoader
|
|
||||||
],
|
|
||||||
templateUrl: './dm-list.html',
|
|
||||||
styleUrl: './dm-list.scss',
|
|
||||||
})
|
|
||||||
export class DmList implements OnInit {
|
|
||||||
userid = input<string>("")
|
|
||||||
token = input<string>("")
|
|
||||||
|
|
||||||
indexedDb = inject(IndexedDB)
|
|
||||||
router = inject(Router)
|
|
||||||
serviceManager = inject(ServiceManager)
|
|
||||||
breakpoint = inject(TUI_BREAKPOINT)
|
|
||||||
|
|
||||||
async ngOnInit() {
|
|
||||||
this.serviceManager.chatService = new ChatService(this.userid(), this.token(), this.indexedDb.getApi(), () => {})
|
|
||||||
if (this.serviceManager.chatsStatus() != LoadStatus.loaded) {
|
|
||||||
try {
|
|
||||||
this.serviceManager.chats.set(await this.serviceManager.chatService.getQuick())
|
|
||||||
this.serviceManager.chatsStatus.set(LoadStatus.updating)
|
|
||||||
} catch (e) {
|
|
||||||
console.warn(`Cache load failed: ${e}. Skipping cache load...`)
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
this.serviceManager.chats.set(await this.serviceManager.chatService.get())
|
|
||||||
this.serviceManager.chatsStatus.set(LoadStatus.loaded)
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
this.serviceManager.chatsStatus.set(LoadStatus.error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected readonly LoadStatus = LoadStatus;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user