Skip to content

Commit 0891b57

Browse files
committed
Differentiate examples
1 parent 72d7942 commit 0891b57

File tree

5 files changed

+69
-32
lines changed

5 files changed

+69
-32
lines changed

packages/app1/app.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,33 @@ const Numbers = observer(() => {
1111
// this module is lazy loaded
1212
const [APIStore, setAPIStore] = React.useState<APIStore | null>(null);
1313
React.useEffect(() => {
14-
APIStoreRuntime.then(module => setAPIStore(module.APIStoreInstance));
14+
APIStoreRuntime.then((module) => setAPIStore(module.APIStoreInstance));
1515
}, []);
1616

1717
return APIStore ? (
1818
<>
1919
<div className="mfe-container">
2020
<div className="users__container">
21-
<h1>Users App 1 (simple lazy fetch): </h1>
21+
<h1>App1 (just display): </h1>
2222
<pre>This component is rendered in the main application</pre>
2323
<table>
24-
{APIStore.users.map(({ name, id, username }) => {
24+
<tr>
25+
<td>Username</td>
26+
<td>Full Name</td>
27+
<td>ID</td>
28+
<td>Flag Count</td>
29+
</tr>
30+
{APIStore.users.map(({ name, id, username, flags }) => {
2531
return (
2632
<tr>
27-
<td>{name}</td>
28-
<td>{id}</td>
2933
<td>{username}</td>
34+
<td>{name}</td>
35+
<td style={{ textAlign: "right" }}>{id}</td>
36+
<td style={{ textAlign: "right" }}>{flags}</td>
3037
</tr>
3138
);
3239
})}
3340
</table>
34-
<button className="users__delete" onClick={APIStore.deleteLastUser}>
35-
Delete Last User from App 1
36-
</button>
3741
</div>
3842
<Suspense fallback={() => "loading remote module..."}>
3943
<App2Users />

packages/app1/styles.less

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
.users {
22
&__container {
3-
border-width: 3px;
4-
border-style: solid;
53
margin: 5px;
64
padding: 12px;
75

8-
&:first-child {
9-
border-color: blue;
10-
}
116
&:last-child {
127
border-color: red;
8+
border-width: 3px;
9+
border-style: solid;
1310
}
1411
}
1512
}

packages/app2/app.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,39 @@ const APIStoreRuntime = import("store/Store");
88
export default observer(function App() {
99
const [APIStore, setAPIStore] = React.useState<APIStore | null>(null);
1010
React.useEffect(() => {
11-
APIStoreRuntime.then(module => setAPIStore(module.APIStoreInstance));
11+
APIStoreRuntime.then((module) => setAPIStore(module.APIStoreInstance));
1212
}, []);
1313

1414
return APIStore ? (
1515
<>
1616
<div className="users__container">
17-
<h1>Users App 2 (simple lazy fetch): </h1>
17+
<h1>App2 (Modify Something): </h1>
1818
<pre>This component is a Federated Module</pre>
1919
<table>
20-
{APIStore.users.map(({ name, id, username }) => {
20+
<tr>
21+
<td>Username</td>
22+
<td>ID</td>
23+
<td>Flags</td>
24+
<td>Action</td>
25+
</tr>
26+
{APIStore.users.map(({ username, flags, id }) => {
2127
return (
2228
<tr>
23-
<td>{name}</td>
24-
<td>{id}</td>
2529
<td>{username}</td>
30+
<td style={{ textAlign: "right" }}>{id}</td>
31+
<td style={{ textAlign: "right" }}>{flags}</td>
32+
<td>
33+
<button onClick={() => APIStore.addFlagToUser(id)}>
34+
Add User Flag
35+
</button>{" "}
36+
<button onClick={() => APIStore.deleteUser(id)}>
37+
Delete User
38+
</button>
39+
</td>
2640
</tr>
2741
);
2842
})}
2943
</table>
30-
<button className="users__delete" onClick={APIStore.deleteLastUser}>
31-
Delete Last User from App 2
32-
</button>
3344
</div>
3445
</>
3546
) : (

packages/store/app.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
2-
import { action, configure, makeObservable, onBecomeObserved } from "mobx";
2+
import {
3+
action,
4+
computed,
5+
configure,
6+
makeObservable,
7+
onBecomeObserved,
8+
} from "mobx";
39

410
// without configuring enforceActions it would be possible to modify any observable from anywhere
511
configure({ enforceActions: "observed" });
@@ -10,28 +16,42 @@ export class APIStore {
1016
constructor(
1117
axiosConf: AxiosRequestConfig = {
1218
baseURL: "https://jsonplaceholder.typicode.com",
13-
auth: undefined
19+
auth: undefined,
1420
}
1521
) {
1622
makeObservable(this, {
17-
users: true
23+
users: true,
1824
});
1925
// setup api that should be in it's own class
2026
this.api = axios.create(axiosConf);
2127

2228
// setup lazy observables
23-
onBecomeObserved(this, "users", this.getUsers);
29+
onBecomeObserved(this, "users", this.handleBecomeObserved);
2430
}
2531

26-
users: User[] = [];
32+
handleBecomeObserved = () => {
33+
if (!this.users.length) {
34+
this.getUsers();
35+
}
36+
};
37+
38+
users: DemoUser[] = [];
2739
// async / await
2840
getUsers = action(async () => {
2941
const { data } = await this.api.get<User[]>("/users");
30-
this.users = data;
31-
// return data;
42+
this.users = (data as DemoUser[]).map((user) => {
43+
user.flags = 0;
44+
return user;
45+
});
3246
});
33-
deleteLastUser = action(() => {
34-
this.users.pop();
47+
deleteUser = action((userId: User["id"]) => {
48+
this.users = this.users.filter((user) => user.id !== userId);
49+
});
50+
addFlagToUser = action((userId: User["id"]) => {
51+
const user = this.users.find((user) => user.id === userId);
52+
if (user) {
53+
user.flags++;
54+
}
3555
});
3656

3757
// not using async/await is a little weirder
@@ -44,4 +64,4 @@ export class APIStore {
4464
}
4565

4666
// all references should point to this singleton (unless you want multiple stores).
47-
export const APIStoreInstance = new APIStore();
67+
export const APIStoreInstance = new APIStore();

packages/store/types/jsonPlaceholder.ts renamed to packages/store/types/Users.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
interface User {
2-
id: 1;
2+
id: number;
33
name: string;
44
username: string;
55
email: string;
@@ -14,3 +14,8 @@ interface User {
1414
};
1515
};
1616
}
17+
18+
19+
interface DemoUser extends User {
20+
flags: number
21+
};

0 commit comments

Comments
 (0)