add contact viewmodels
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import StorageModel, {StorageModelSubPaths} from "./storageModel";
|
import StorageModel, {StorageModelSubPaths} from "./storageModel";
|
||||||
|
|
||||||
export default class ContactsModel {
|
export default class ContactListModel {
|
||||||
private getContactPath = (contactId: string): string[] => {
|
private getContactPath = (contactId: string): string[] => {
|
||||||
return [StorageModelSubPaths.ContactsModel, contactId];
|
return [StorageModelSubPaths.ContactsModel, contactId];
|
||||||
}
|
}
|
||||||
@@ -16,6 +16,5 @@ export default class ContactsModel {
|
|||||||
this.storageModel.write(path, name)
|
this.storageModel.write(path, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(public storageModel: StorageModel) {
|
constructor(public storageModel: StorageModel) {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import ContactListModel from "../../Model/Global/contactListModel";
|
||||||
|
import * as React from "../../react";
|
||||||
|
|
||||||
|
export default class ContactListViewModel {
|
||||||
|
// state
|
||||||
|
contacts: React.MapState<ContactViewModel> = new React.MapState();
|
||||||
|
|
||||||
|
// main
|
||||||
|
updateContact = (id: string, name: string): void => {
|
||||||
|
if (this.contacts.value.has(id)) {
|
||||||
|
this.contacts.value.get(id)!.name.value = name;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const vm = new ContactViewModel(id, name);
|
||||||
|
this.contacts.set(id, vm);
|
||||||
|
this.contactListModel.storeContact(id, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// load
|
||||||
|
loadData = (): void => {
|
||||||
|
const contacts: [string, string][] = this.contactListModel.loadContacts();
|
||||||
|
for (const contact of contacts) {
|
||||||
|
const vm = new ContactViewModel(...contact);
|
||||||
|
this.contacts.set(vm.id, vm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// init
|
||||||
|
constructor(public contactListModel: ContactListModel) {
|
||||||
|
this.loadData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ContactViewModel {
|
||||||
|
// state
|
||||||
|
name: React.State<string> = new React.State("");
|
||||||
|
|
||||||
|
// init
|
||||||
|
constructor(public id: string, name: string) {
|
||||||
|
this.name.value = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user