Skip to content

Commit de1925c

Browse files
committed
docs(todo): no preload, show users in login page
1 parent f2bdf0e commit de1925c

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

examples/TodoMVC/src/components/SignIn.tsx

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,37 @@
33
* @author junbao <[email protected]>
44
*/
55

6-
import { useDispatch } from 'amos/react';
6+
import { useDispatch, useSelector } from 'amos/react';
77
import { memo, useState } from 'react';
88
import { signIn } from '../store/user.actions';
9+
import { userMapBox } from '../store/user.boxes';
910

1011
export const SignIn = memo(() => {
1112
const [input, setInput] = useState('');
1213
const dispatch = useDispatch();
14+
const userMap = useSelector(userMapBox);
1315
return (
1416
<div className="sign-in">
1517
<input placeholder="Your name" value={input} onChange={(e) => setInput(e.target.value)} />
1618
&nbsp;
1719
<button disabled={!input.trim()} onClick={() => dispatch(signIn(input))}>
1820
Sign In
1921
</button>
22+
<br />
23+
{userMap.size() > 0 && (
24+
<div>
25+
<br />
26+
<span>Users:&nbsp;</span>
27+
{Array.from(userMap.values(), (u) => (
28+
<>
29+
<button key={u.id} onClick={() => dispatch(signIn(u.name))}>
30+
{u.name}
31+
</button>
32+
&nbsp;
33+
</>
34+
))}
35+
</div>
36+
)}
2037
</div>
2138
);
2239
});

examples/TodoMVC/src/main.tsx

-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,11 @@ import { StrictMode } from 'react';
44
import { createRoot } from 'react-dom/client';
55
import { App } from './App';
66
import './index.css';
7-
import { hashCode } from './store/todo.boxes';
8-
import { currentUserIdBox, userMapBox } from './store/user.boxes';
9-
10-
const userId = hashCode('Amos');
117

128
const store = createStore(
139
{
1410
name: 'Amos - TodoMVC',
1511
devtools: true,
16-
preloadedState: {
17-
[currentUserIdBox.key]: userId,
18-
[userMapBox.key]: {
19-
[userId]: { id: userId, name: 'Amos' },
20-
},
21-
},
2212
},
2313
withPersist({
2414
storage: new IDBStorage('todo', 'todo'),

examples/TodoMVC/src/store/user.boxes.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { box, Record, recordMapBox, signal } from 'amos';
7+
import { hashCode } from './todo.boxes';
78

89
export interface UserModel {
910
id: number;
@@ -24,7 +25,9 @@ export const signOutSignal = signal<SignOutEvent>('user.signOut');
2425

2526
export const signInSignal = signal<UserModel>('user.signIn');
2627

27-
export const userMapBox = recordMapBox('users', UserRecord, 'id');
28+
export const userMapBox = recordMapBox('users', UserRecord, 'id').setInitialState((s) => {
29+
return s.mergeItem(hashCode('Amos'), { name: 'Amos' });
30+
});
2831

2932
userMapBox.subscribe(signOutSignal, (state, data) => {
3033
if (!data.keepData) {
@@ -33,4 +36,4 @@ userMapBox.subscribe(signOutSignal, (state, data) => {
3336
return state;
3437
});
3538

36-
export const currentUserIdBox = box('users.currentUserId', 0);
39+
export const currentUserIdBox = box('users.currentUserId', hashCode('Amos'));

scripts/check_deps.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const check_deps = autorun(
2121
const pj = await fs.readJSON(`packages/${pkg}/package.json`);
2222
const files = await glob(`packages/${pkg}/src/**/*.{ts,tsx}`);
2323
for (const f of files) {
24-
const isDev = f.includes('.spec.');
24+
const isDev = f.includes('.spec.') || f.includes('test');
2525
const d = await fs.readFile(f, 'utf-8');
2626
const ms = d.matchAll(/ from ['"]([^'"]+)['"]/g);
2727
for (const m of ms) {
@@ -76,7 +76,7 @@ export const check_deps = autorun(
7676
console.error(`%s should not dev dependents on amos.`, pkg);
7777
}
7878
if (pkg === 'amos-utils') {
79-
const bad = deps.concat(devDeps).filter((d) => d.startsWith('amos'));
79+
const bad = deps.filter((d) => d.startsWith('amos'));
8080
if (bad.length) {
8181
console.error('amos-utils no amos deps: ', bad.join(', '));
8282
}

0 commit comments

Comments
 (0)