interface: updated api/logic in reducer and api to reflect new %contact-store

This commit is contained in:
Logan Allen 2021-01-11 13:40:40 -06:00
parent 1eb99bfdcd
commit dca2c9ae58
3 changed files with 20 additions and 77 deletions

View File

@ -5,74 +5,41 @@ import { Contact, ContactEdit } from '~/types/contact-update';
import { GroupPolicy, Resource } from '~/types/group-update';
export default class ContactsApi extends BaseApi<StoreState> {
create(
name: string,
policy: Enc<GroupPolicy>,
title: string,
description: string
) {
return this.viewAction({
create: {
name,
policy,
title,
description,
},
});
add(ship: Patp, contact: any) {
return this.storeAction({ add: { ship, contact } });
}
share(recipient: Patp, path: Patp, ship: Patp, contact: Contact) {
return this.viewAction({
share: {
recipient,
path,
ship,
contact,
},
});
remove(ship: Patp) {
return this.storeAction({ remove: { ship } });
}
remove(path: Path, ship: Patp) {
return this.viewAction({ remove: { path, ship } });
}
edit(path: Path, ship: Patp, editField: ContactEdit) {
edit(ship: Patp, editField: ContactEdit) {
/* editField can be...
{nickname: ''}
{email: ''}
{phone: ''}
{website: ''}
{notes: ''}
{color: 'fff'} // with no 0x prefix
{avatar: null}
{avatar: {url: ''}}
{avatar: ''}
*/
return this.hookAction({
return this.storeAction({
edit: {
path,
ship,
'edit-field': editField,
},
});
}
invite(resource: Resource, ship: Patp, text = '') {
return this.viewAction({
invite: { resource, ship, text },
});
private storeAction(action: any): Promise<any> {
return this.action('contact-store', 'contact-update', action)
}
join(resource: Resource) {
return this.viewAction({
join: resource,
});
private viewAction(threadName: string, action: any) {
return this.spider('contact-view-action', 'json', threadName, action);
}
private hookAction(data) {
return this.action('contact-hook', 'contact-action', data);
}
private viewAction(data) {
return this.action('contact-view', 'json', data);
private hookAction(ship: Patp, action: any): Promise<any> {
return this.action('contact-push-hook', 'contact-update', action);
}
}

View File

@ -10,8 +10,6 @@ export default class ContactReducer<S extends ContactState> {
const data = _.get(json, 'contact-update', false);
if (data) {
this.initial(data, state);
this.create(data, state);
this.delete(data, state);
this.add(data, state);
this.remove(data, state);
this.edit(data, state);
@ -25,27 +23,10 @@ export default class ContactReducer<S extends ContactState> {
}
}
create(json: ContactUpdate, state: S) {
const data = _.get(json, 'create', false);
if (data) {
state.contacts[data.path] = {};
}
}
delete(json: ContactUpdate, state: S) {
const data = _.get(json, 'delete', false);
if (data) {
delete state.contacts[data.path];
}
}
add(json: ContactUpdate, state: S) {
const data = _.get(json, 'add', false);
if (
data &&
(data.path in state.contacts)
) {
state.contacts[data.path][data.ship] = data.contact;
if (data) {
state.contacts[data.ship] = data.contact;
}
}
@ -53,10 +34,9 @@ export default class ContactReducer<S extends ContactState> {
const data = _.get(json, 'remove', false);
if (
data &&
(data.path in state.contacts) &&
(data.ship in state.contacts[data.path])
(data.ship in state.contacts)
) {
delete state.contacts[data.path][data.ship];
delete state.contacts[data.ship];
}
}
@ -64,15 +44,13 @@ export default class ContactReducer<S extends ContactState> {
const data = _.get(json, 'edit', false);
if (
data &&
(data.path in state.contacts) &&
(data.ship in state.contacts[data.path])
(data.ship in state.contacts)
) {
const edit = Object.keys(data['edit-field']);
if (edit.length !== 1) {
return;
}
state.contacts[data.path][data.ship][edit[0]] =
data['edit-field'][edit[0]];
state.contacts[data.ship][edit[0]] = data['edit-field'][edit[0]];
}
}
}

View File

@ -10,7 +10,6 @@ import _ from 'lodash';
type AppSubscription = [Path, string];
const groupSubscriptions: AppSubscription[] = [
['/synced', 'contact-hook']
];
const graphSubscriptions: AppSubscription[] = [
@ -37,8 +36,7 @@ export default class GlobalSubscription extends BaseSubscription<StoreState> {
this.subscribe('/groups', 'group-store');
this.clearQueue();
this.subscribe('/primary', 'contact-view');
this.subscribe('/updates', 'contact-store');
this.subscribe('/all', 's3-store');
this.subscribe('/keys', 'graph-store');
this.subscribe('/updates', 'hark-store');