Add mock API and reorg code for clarity
This commit is contained in:
parent
de87ec17d1
commit
1d00f3b984
29 changed files with 356 additions and 168 deletions
48
src/state/models/me.ts
Normal file
48
src/state/models/me.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
import {Instance, SnapshotOut, types, flow, getRoot} from 'mobx-state-tree'
|
||||
import {RootStore} from './root-store'
|
||||
import {withEnvironment} from '../env'
|
||||
|
||||
export const MeModel = types
|
||||
.model('Me')
|
||||
.props({
|
||||
did: types.maybe(types.string),
|
||||
name: types.maybe(types.string),
|
||||
displayName: types.maybe(types.string),
|
||||
description: types.maybe(types.string),
|
||||
})
|
||||
.extend(withEnvironment)
|
||||
.actions(self => ({
|
||||
load: flow(function* () {
|
||||
const sess = (getRoot(self) as RootStore).session
|
||||
if (sess.isAuthed) {
|
||||
// TODO temporary
|
||||
const userDb = self.env.adx.mockDb.mainUser
|
||||
self.did = userDb.did
|
||||
self.name = userDb.name
|
||||
const profile = yield self.env.adx
|
||||
.repo(self.did, true)
|
||||
.collection('blueskyweb.xyz:Profiles')
|
||||
.get('Profile', 'profile')
|
||||
.catch(_ => undefined)
|
||||
if (profile?.valid) {
|
||||
self.displayName = profile.value.displayName
|
||||
self.description = profile.value.description
|
||||
} else {
|
||||
self.displayName = ''
|
||||
self.description = ''
|
||||
}
|
||||
} else {
|
||||
self.did = undefined
|
||||
self.name = undefined
|
||||
self.displayName = undefined
|
||||
self.description = undefined
|
||||
}
|
||||
}),
|
||||
}))
|
||||
|
||||
export interface Me extends Instance<typeof MeModel> {}
|
||||
export interface MeSnapshot extends SnapshotOut<typeof MeModel> {}
|
||||
|
||||
export function createDefaultMe() {
|
||||
return {}
|
||||
}
|
|
@ -5,9 +5,11 @@
|
|||
import {Instance, SnapshotOut, types} from 'mobx-state-tree'
|
||||
import {createContext, useContext} from 'react'
|
||||
import {SessionModel, createDefaultSession} from './session'
|
||||
import {MeModel, createDefaultMe} from './me'
|
||||
|
||||
export const RootStoreModel = types.model('RootStore').props({
|
||||
session: SessionModel,
|
||||
me: MeModel,
|
||||
})
|
||||
|
||||
export interface RootStore extends Instance<typeof RootStoreModel> {}
|
||||
|
@ -16,6 +18,7 @@ export interface RootStoreSnapshot extends SnapshotOut<typeof RootStoreModel> {}
|
|||
export function createDefaultRootStore() {
|
||||
return {
|
||||
session: createDefaultSession(),
|
||||
me: createDefaultMe(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import {Instance, SnapshotOut, types, flow} from 'mobx-state-tree'
|
||||
// import {UserConfig} from '../../api'
|
||||
import * as auth from '../auth'
|
||||
import * as auth from '../lib/auth'
|
||||
import {withEnvironment} from '../env'
|
||||
|
||||
export const SessionModel = types
|
||||
|
@ -24,10 +24,10 @@ export const SessionModel = types
|
|||
self.uiIsProcessing = true
|
||||
self.uiError = undefined
|
||||
try {
|
||||
if (!self.environment.authStore) {
|
||||
if (!self.env.authStore) {
|
||||
throw new Error('Auth store not initialized')
|
||||
}
|
||||
const res = yield auth.requestAppUcan(self.environment.authStore)
|
||||
const res = yield auth.requestAppUcan(self.env.authStore)
|
||||
self.isAuthed = res
|
||||
self.uiIsProcessing = false
|
||||
return res
|
||||
|
@ -42,10 +42,10 @@ export const SessionModel = types
|
|||
self.uiIsProcessing = true
|
||||
self.uiError = undefined
|
||||
try {
|
||||
if (!self.environment.authStore) {
|
||||
if (!self.env.authStore) {
|
||||
throw new Error('Auth store not initialized')
|
||||
}
|
||||
const res = yield auth.logout(self.environment.authStore)
|
||||
const res = yield auth.logout(self.env.authStore)
|
||||
self.isAuthed = false
|
||||
self.uiIsProcessing = false
|
||||
return res
|
||||
|
@ -65,7 +65,7 @@ export const SessionModel = types
|
|||
// secretKeyStr: self.secretKeyStr,
|
||||
// rootAuthToken: self.rootAuthToken,
|
||||
// })
|
||||
// self.environment.api.setUserCfg(cfg)
|
||||
// self.env.api.setUserCfg(cfg)
|
||||
self.isAuthed = true
|
||||
self.uiIsProcessing = false
|
||||
return true
|
||||
|
@ -86,7 +86,7 @@ export const SessionModel = types
|
|||
// self.secretKeyStr = state.secretKeyStr
|
||||
// self.rootAuthToken = state.rootAuthToken
|
||||
self.isAuthed = true
|
||||
// self.environment.api.setUserCfg(cfg)
|
||||
// self.env.api.setUserCfg(cfg)
|
||||
} catch (e: any) {
|
||||
console.error('Failed to create test account', e)
|
||||
self.uiError = e.toString()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue