generated from nichoth/template-ts-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.ts
118 lines (99 loc) · 2.88 KB
/
state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Signal, effect, signal } from '@preact/signals'
import { Account, LocalNode, Profile, AccountMeta, ProfileMeta } from 'cojson'
import { ProfileShape } from 'cojson/src/coValues/account.js'
import { Resolved, ResolvedAccount } from 'jazz-autosub'
import Route from 'route-event'
import {
profile as getProfile,
createLocalNode,
createLocalAuth,
AuthStatus,
ReadyStatus
} from '../src/index.js'
import { Task, ListOfTasks, TodoProject, TodoAccountRoot } from './types.js'
/**
* Create a localNode
* Subscribe to Jazz objects
* Get your profile
* Authenticate
*/
export function State ():{
localNode:Signal<LocalNode|null>;
authStatus:Signal<AuthStatus>;
profile:Signal<ResolvedAccount<Account<
Profile<ProfileShape, ProfileMeta>,
TodoAccountRoot,
AccountMeta
>>|null>;
logoutCount:Signal<number>;
route:Signal<string>;
_setRoute:(path:string)=>void;
} { // eslint-disable-line indent
const {
authProvider,
authStatus,
logoutCount
} = createLocalAuth({ appName: 'Todo App' })
/**
* connect to jazz
*/
const { node } = createLocalNode({
auth: authProvider,
authStatus,
logoutCount
})
// @ts-ignore
window.node = node
const { profile } = getProfile<Profile, TodoAccountRoot, AccountMeta>(node)
const onRoute = Route()
const state = {
_setRoute: onRoute.setRoute.bind(onRoute),
localNode: node,
profile,
logoutCount,
authStatus,
route: signal(location.pathname + location.search)
}
// @ts-ignore
window.state = state
onRoute((path:string) => {
const newPath = path.replace('/jazz-signals/', '/') // for github pages
state.route.value = newPath
})
return state
}
export function createNewProfile (state:ReturnType<typeof State>, username:string) {
(state.authStatus.value as ReadyStatus).signUp(username)
}
export function createList (state:ReturnType<typeof State>, { name }:{
name:string
}) {
if (!name) throw new Error('missing list name')
const { profile } = state
const dispose = effect(() => {
if (!profile.value) return
const projectGroup = profile.value.createGroup()
const project = projectGroup.createMap<TodoProject>({
title: name,
tasks: projectGroup.createList<ListOfTasks>().id
})
console.log('project...', project, profile.value.root)
profile.value.root?.projects?.append(project.id)
state._setRoute(`/id/${project.id}`)
})
return dispose
}
export function createTask (
project:Resolved<TodoProject>,
opts:{
name:string,
}
) {
if (!project.tasks) return
const { name } = opts
const task = project.meta.group.createMap<Task>({
done: false,
text: name,
})
project.tasks.append(task.id)
}