interface: move invite subs to airlock

This commit is contained in:
Liam Fitzgerald 2021-06-09 13:54:24 +10:00
parent 3066077ca9
commit 8bf990becb
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
3 changed files with 41 additions and 25 deletions

View File

@ -3,6 +3,8 @@ import useHarkState from '~/logic/state/hark';
import useMetadataState from '~/logic/state/metadata';
import useContactState from '../state/contact';
import useGroupState from '../state/group';
import useInviteState from '../state/invite';
import useLaunchState from '../state/launch';
import useSettingsState from '../state/settings';
const api = new Urbit('', '');
@ -21,7 +23,9 @@ export const bootstrapApi = async () => {
useMetadataState,
useGroupState,
useContactState,
useSettingsState
useSettingsState,
useLaunchState,
useInviteState
].forEach((state) => {
state.getState().initialize(api);
});

View File

@ -1,24 +1,9 @@
import { InviteUpdate } from '@urbit/api/invite';
import _ from 'lodash';
import { Cage } from '~/types/cage';
import { reduceState } from '../state/base';
import useInviteState, { InviteState } from '../state/invite';
import { BaseState } from '../state/base';
import { InviteState as State } from '../state/invite';
export default class InviteReducer {
reduce(json: Cage) {
const data = json['invite-update'];
if (data) {
reduceState<InviteState, InviteUpdate>(useInviteState, data, [
initial,
create,
deleteInvite,
invite,
accepted,
decline
]);
}
}
}
type InviteState = State & BaseState<State>;
const initial = (json: InviteUpdate, state: InviteState): InviteState => {
const data = _.get(json, 'initial', false);
@ -67,3 +52,12 @@ const decline = (json: InviteUpdate, state: InviteState): InviteState => {
}
return state;
};
export const reduce = [
initial,
create,
deleteInvite,
invite,
accepted,
decline
];

View File

@ -1,13 +1,31 @@
import { Invites } from '@urbit/api';
import { BaseState, createState } from './base';
import { reduce } from '../reducers/invite-update';
import _ from 'lodash';
import {
createState,
createSubscription,
reduceStateN
} from './base';
export interface InviteState extends BaseState<InviteState> {
export interface InviteState {
invites: Invites;
}
// @ts-ignore investigate zustand types
const useInviteState = createState<InviteState>('Invite', {
invites: {}
});
const useInviteState = createState<InviteState>(
'Invite',
{
invites: {}
},
['invites'],
[
(set, get) =>
createSubscription('invite-store', '/all', (e) => {
const d = _.get(e, 'invite-update', false);
if (d) {
reduceStateN(get(), d, reduce);
}
})
]
);
export default useInviteState;