create-obsession 0.0.0-stage → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +160 -2
- package/bin/README.md +6 -0
- package/bin/obsession.exe +0 -0
- package/bin/obsession.out +0 -0
- package/frontend/README.md +41 -0
- package/frontend/project-type.json +8 -0
- package/frontend/template/.agent/task-scope.example.json +7 -0
- package/frontend/template/.editorconfig +11 -0
- package/frontend/template/.env +2 -0
- package/frontend/template/.env.example +2 -0
- package/frontend/template/.github/dependabot.yml +11 -0
- package/frontend/template/.github/workflows/quality.yml +28 -0
- package/frontend/template/.gitignore.tpl +28 -0
- package/frontend/template/.vscode/extensions.json +3 -0
- package/frontend/template/.vscode/settings.json +13 -0
- package/frontend/template/.vscode/tasks.json +39 -0
- package/frontend/template/AGENTS.md +56 -0
- package/frontend/template/CLAUDE.md +18 -0
- package/frontend/template/README.md.tpl +57 -0
- package/frontend/template/docs/CHAPTER_8_9_10_ENFORCEMENT.md +82 -0
- package/frontend/template/docs/FEATURE_BLUEPRINT.md +164 -0
- package/frontend/template/docs/FRONTEND_ARCHITECTURE.md +132 -0
- package/frontend/template/docs/SECURITY_BASELINE.md +32 -0
- package/frontend/template/docs/STATE_OWNERSHIP.md +33 -0
- package/frontend/template/docs/TASK_PROTOCOL.md +50 -0
- package/frontend/template/docs/TASK_SCOPE.md +35 -0
- package/frontend/template/docs/TECHNOLOGY_AND_CONFIG.md +96 -0
- package/frontend/template/docs/TESTING_AND_GATES.md +109 -0
- package/frontend/template/docs/TESTING_STRATEGY.md +34 -0
- package/frontend/template/docs/UI_STATES.md +26 -0
- package/frontend/template/docs/reference/front-end-structure-course.html +1606 -0
- package/frontend/template/docs/reference/frontend-project-setup-manual.docx +0 -0
- package/frontend/template/e2e/smoke.spec.ts +12 -0
- package/frontend/template/index.html.tpl +13 -0
- package/frontend/template/package.json.tpl +45 -0
- package/frontend/template/playwright.config.ts +18 -0
- package/frontend/template/scripts/check-architecture.mjs +291 -0
- package/frontend/template/scripts/check-architecture.test.mjs +93 -0
- package/frontend/template/scripts/task-scope.mjs +70 -0
- package/frontend/template/scripts/task-scope.test.mjs +46 -0
- package/frontend/template/src/app/NotFoundPage.tsx +5 -0
- package/frontend/template/src/app/RootLayout.tsx +15 -0
- package/frontend/template/src/app/RouteErrorBoundary.tsx +12 -0
- package/frontend/template/src/app/main.tsx +11 -0
- package/frontend/template/src/app/providers.tsx +12 -0
- package/frontend/template/src/app/router.tsx +14 -0
- package/frontend/template/src/app/store.ts +12 -0
- package/frontend/template/src/features/counter/index.ts +2 -0
- package/frontend/template/src/features/counter/model/counterSlice.test.ts +12 -0
- package/frontend/template/src/features/counter/model/counterSlice.ts +16 -0
- package/frontend/template/src/features/counter/model/selectors.ts +4 -0
- package/frontend/template/src/features/counter/ui/CounterWidget.tsx +16 -0
- package/frontend/template/src/features/home/index.ts +1 -0
- package/frontend/template/src/features/home/routes.tsx +8 -0
- package/frontend/template/src/features/home/ui/HomePage.test.tsx +11 -0
- package/frontend/template/src/features/home/ui/HomePage.tsx +13 -0
- package/frontend/template/src/shared/api/baseApi.ts +9 -0
- package/frontend/template/src/shared/api/index.ts +1 -0
- package/frontend/template/src/shared/config/env.ts +3 -0
- package/frontend/template/src/shared/config/index.ts +1 -0
- package/frontend/template/src/shared/ui/Button.tsx +6 -0
- package/frontend/template/src/shared/ui/StatusPanel.test.tsx +25 -0
- package/frontend/template/src/shared/ui/StatusPanel.tsx +36 -0
- package/frontend/template/src/shared/ui/index.ts +4 -0
- package/frontend/template/src/shared/ui/styles.css +27 -0
- package/frontend/template/src/vite-env.d.ts +1 -0
- package/frontend/template/tests/setup.ts +1 -0
- package/frontend/template/tsconfig.json +21 -0
- package/frontend/template/vite.config.ts +73 -0
- package/npm/cli.js +53 -0
- package/package.json +30 -4
|
@@ -0,0 +1,1606 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
6
|
+
<title>Front-end structure: the complete course</title>
|
|
7
|
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
8
|
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
9
|
+
<link href="https://fonts.googleapis.com/css2?family=Oswald:wght@400;600;700&family=Crimson+Pro:ital,wght@0,400;0,600;1,400&family=JetBrains+Mono:wght@400;600&display=swap" rel="stylesheet">
|
|
10
|
+
<style>
|
|
11
|
+
:root{
|
|
12
|
+
--desk:#D1A63E; --desk-shadow:rgba(80,55,0,.45);
|
|
13
|
+
--paper:#EDEDE9; --paper-edge:#D6D5CF; --stack:#9E9D98;
|
|
14
|
+
--ink:#1E1D1A; --ink-soft:#4B4944;
|
|
15
|
+
--mustard:#E4B13F; --mustard-deep:#C98F17; --on-mustard:#1E1D1A;
|
|
16
|
+
--rule:#BEBCB4; --code-bg:#E0DFD8;
|
|
17
|
+
--ok:#2E6A38; --bad:#A3311E;
|
|
18
|
+
--spine:rgba(0,0,0,.13);
|
|
19
|
+
color-scheme:light;
|
|
20
|
+
box-sizing:border-box;
|
|
21
|
+
padding-top:env(safe-area-inset-top,0px);
|
|
22
|
+
padding-bottom:env(safe-area-inset-bottom,0px);
|
|
23
|
+
}
|
|
24
|
+
@media (prefers-color-scheme:dark){
|
|
25
|
+
:root:not([data-theme="light"]){
|
|
26
|
+
--desk:#3A2F15; --desk-shadow:rgba(0,0,0,.6);
|
|
27
|
+
--paper:#25241F; --paper-edge:#34332D; --stack:#4A4840;
|
|
28
|
+
--ink:#ECEAE2; --ink-soft:#B6B3A8;
|
|
29
|
+
--mustard:#D9A535; --mustard-deep:#A9760E; --on-mustard:#1E1D1A;
|
|
30
|
+
--rule:#4A4840; --code-bg:#1B1A16;
|
|
31
|
+
--ok:#86C48D; --bad:#EE8B76;
|
|
32
|
+
--spine:rgba(0,0,0,.35);
|
|
33
|
+
color-scheme:dark;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
:root[data-theme="dark"]{
|
|
37
|
+
--desk:#3A2F15; --desk-shadow:rgba(0,0,0,.6);
|
|
38
|
+
--paper:#25241F; --paper-edge:#34332D; --stack:#4A4840;
|
|
39
|
+
--ink:#ECEAE2; --ink-soft:#B6B3A8;
|
|
40
|
+
--mustard:#D9A535; --mustard-deep:#A9760E; --on-mustard:#1E1D1A;
|
|
41
|
+
--rule:#4A4840; --code-bg:#1B1A16;
|
|
42
|
+
--ok:#86C48D; --bad:#EE8B76;
|
|
43
|
+
--spine:rgba(0,0,0,.35);
|
|
44
|
+
color-scheme:dark;
|
|
45
|
+
}
|
|
46
|
+
*,*::before,*::after{box-sizing:inherit}
|
|
47
|
+
html{height:100%;scroll-padding-top:env(safe-area-inset-top,0px)}
|
|
48
|
+
body{margin:0;min-height:100%;background:var(--desk);color:var(--ink);
|
|
49
|
+
font-family:"Crimson Pro",Georgia,"Times New Roman",serif;font-size:17px;line-height:1.55}
|
|
50
|
+
.wrap{max-width:1120px;margin:0 auto;padding:28px 20px 48px}
|
|
51
|
+
|
|
52
|
+
/* progress */
|
|
53
|
+
.progress{display:grid;grid-template-columns:1fr auto;gap:6px 16px;align-items:end;margin-bottom:22px;color:var(--on-mustard)}
|
|
54
|
+
.progress h1{margin:0;font-family:Oswald,"Arial Narrow",sans-serif;font-weight:700;font-size:22px;letter-spacing:.01em}
|
|
55
|
+
.progress .meta{font-family:Oswald,"Arial Narrow",sans-serif;font-size:14px}
|
|
56
|
+
.bar{grid-column:1/-1;display:grid;grid-template-columns:repeat(10,1fr);gap:4px;border:2.5px solid var(--on-mustard);padding:4px;background:rgba(255,255,255,.18)}
|
|
57
|
+
.seg{height:14px;background:transparent;border:1.5px solid rgba(30,29,26,.35)}
|
|
58
|
+
.seg.done{background:var(--on-mustard);border-color:var(--on-mustard)}
|
|
59
|
+
.seg.current{border-color:var(--on-mustard);background:repeating-linear-gradient(135deg,var(--on-mustard) 0 3px,transparent 3px 7px)}
|
|
60
|
+
@media (prefers-color-scheme:dark){:root:not([data-theme="light"]) .progress{color:var(--ink)}
|
|
61
|
+
:root:not([data-theme="light"]) .bar{border-color:var(--ink);background:rgba(0,0,0,.2)}
|
|
62
|
+
:root:not([data-theme="light"]) .seg{border-color:rgba(236,234,226,.3)}
|
|
63
|
+
:root:not([data-theme="light"]) .seg.current{border-color:var(--mustard);background:repeating-linear-gradient(135deg,var(--mustard) 0 3px,transparent 3px 7px)}}
|
|
64
|
+
:root[data-theme="dark"] .progress{color:var(--ink)}
|
|
65
|
+
:root[data-theme="dark"] .bar{border-color:var(--ink);background:rgba(0,0,0,.2)}
|
|
66
|
+
:root[data-theme="dark"] .seg.current{border-color:var(--mustard);background:repeating-linear-gradient(135deg,var(--mustard) 0 3px,transparent 3px 7px)}
|
|
67
|
+
|
|
68
|
+
/* book */
|
|
69
|
+
.book{display:grid;grid-template-columns:1fr 1fr;box-shadow:0 34px 60px -24px var(--desk-shadow);position:relative}
|
|
70
|
+
.page{background:var(--paper);padding:52px 50px 64px;position:relative;min-height:660px}
|
|
71
|
+
.page.left{background:linear-gradient(to left,var(--spine),transparent 46px),var(--paper);
|
|
72
|
+
box-shadow:-5px 0 0 var(--paper-edge),-9px 0 0 var(--stack)}
|
|
73
|
+
.page.right{background:linear-gradient(to right,var(--spine),transparent 30px),var(--paper);
|
|
74
|
+
box-shadow:5px 0 0 var(--paper-edge)}
|
|
75
|
+
.folio{position:absolute;bottom:26px;font-family:Oswald,sans-serif;font-size:12px;color:var(--ink-soft)}
|
|
76
|
+
.left .folio{right:50px}.right .folio{right:50px}
|
|
77
|
+
.spread[hidden]{display:none}
|
|
78
|
+
|
|
79
|
+
h2.chap{font-family:Oswald,"Arial Narrow",sans-serif;font-weight:700;font-size:clamp(42px,5.4vw,60px);line-height:1;margin:34px 0 6px;letter-spacing:.005em}
|
|
80
|
+
h2.chap + .sub{font-family:Oswald,sans-serif;font-weight:600;font-size:clamp(24px,3vw,32px);margin:0 0 40px}
|
|
81
|
+
h3{font-family:Oswald,"Arial Narrow",sans-serif;font-weight:600;font-size:21px;margin:0 0 12px;line-height:1.2}
|
|
82
|
+
h4{font-family:Oswald,sans-serif;font-weight:600;font-size:16px;margin:26px 0 6px}
|
|
83
|
+
p{margin:0 0 12px;max-width:62ch;hyphens:auto}
|
|
84
|
+
.small{font-size:15px;color:var(--ink-soft)}
|
|
85
|
+
|
|
86
|
+
/* insight box */
|
|
87
|
+
.insight{display:grid;grid-template-columns:auto 1fr;border:3px solid var(--ink);margin:24px 0 8px;box-shadow:3px 4px 0 var(--ink)}
|
|
88
|
+
.insight b{background:var(--mustard-deep);color:var(--on-mustard);font-family:Oswald,sans-serif;font-weight:700;font-size:17px;padding:16px 14px;border-right:3px solid var(--ink);display:flex;align-items:center;white-space:nowrap}
|
|
89
|
+
.insight span{background:var(--mustard);color:var(--on-mustard);font-family:Oswald,sans-serif;font-size:15.5px;line-height:1.35;padding:12px 18px;display:flex;align-items:center;justify-content:center;text-align:center}
|
|
90
|
+
|
|
91
|
+
/* roadmap */
|
|
92
|
+
.map{list-style:none;padding:0;margin:8px 0 18px;counter-reset:s}
|
|
93
|
+
.map li{display:grid;grid-template-columns:30px 1fr auto;gap:10px;align-items:baseline;padding:7px 0;border-bottom:1px solid var(--rule);counter-increment:s;color:var(--ink-soft)}
|
|
94
|
+
.map li::before{content:counter(s);font-family:Oswald,sans-serif;font-weight:600;font-size:15px}
|
|
95
|
+
.map li em{font-style:normal;font-family:Oswald,sans-serif;font-size:12.5px}
|
|
96
|
+
.map li.now{color:var(--ink);font-weight:600}
|
|
97
|
+
.map li.now em{background:var(--mustard);color:var(--on-mustard);padding:1px 7px;border:1.5px solid var(--ink)}
|
|
98
|
+
|
|
99
|
+
/* diagram */
|
|
100
|
+
.diagram{width:100%;max-width:380px;display:block;margin:4px auto 10px}
|
|
101
|
+
.diagram .s{fill:none;stroke:var(--ink);stroke-width:2.1;stroke-linejoin:round;stroke-linecap:round}
|
|
102
|
+
.diagram .door{fill:var(--mustard);stroke:var(--ink);stroke-width:1.6}
|
|
103
|
+
.diagram text{fill:var(--ink);font-family:Oswald,"Arial Narrow",sans-serif}
|
|
104
|
+
.diagram .t{font-weight:700;font-size:17px;text-anchor:middle}
|
|
105
|
+
.diagram .tl{font-weight:700;font-size:14px}
|
|
106
|
+
.diagram .ts{font-size:11px;text-anchor:middle;fill:var(--ink-soft)}
|
|
107
|
+
.diagram .td{font-size:8.5px;text-anchor:middle;fill:var(--on-mustard)}
|
|
108
|
+
.diagram .bad{stroke:var(--bad);fill:none;stroke-width:2.1;stroke-dasharray:5 4}
|
|
109
|
+
.diagram .badx{stroke:var(--bad);stroke-width:2.6;stroke-linecap:round}
|
|
110
|
+
.diagram .badt{fill:var(--bad);font-size:11px}
|
|
111
|
+
|
|
112
|
+
dl.zones{margin:6px 0 0}
|
|
113
|
+
dl.zones dt{font-family:Oswald,sans-serif;font-weight:700;font-size:15px;margin-top:10px}
|
|
114
|
+
dl.zones dd{margin:0;font-size:16px}
|
|
115
|
+
|
|
116
|
+
/* code */
|
|
117
|
+
pre{background:var(--code-bg);border-left:4px solid var(--ink);padding:14px 16px;margin:12px 0 14px;overflow-x:auto;
|
|
118
|
+
font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:12.5px;line-height:1.6;color:var(--ink)}
|
|
119
|
+
pre .c{color:var(--ink-soft)}
|
|
120
|
+
pre .ok{color:var(--ok);font-weight:600}
|
|
121
|
+
pre .bad{color:var(--bad);font-weight:600}
|
|
122
|
+
pre .hl{background:var(--mustard);color:var(--on-mustard);padding:0 3px}
|
|
123
|
+
code{font-family:"JetBrains Mono",ui-monospace,Menlo,monospace;font-size:.85em;background:var(--code-bg);padding:1px 4px}
|
|
124
|
+
|
|
125
|
+
.rules{counter-reset:r;margin:6px 0 4px;padding:0;list-style:none}
|
|
126
|
+
.rules li{border:2.5px solid var(--ink);padding:10px 14px 10px 50px;position:relative;margin-bottom:10px;counter-increment:r}
|
|
127
|
+
.rules li::before{content:counter(r,upper-alpha);position:absolute;left:0;top:0;bottom:0;width:36px;background:var(--mustard);color:var(--on-mustard);border-right:2.5px solid var(--ink);display:flex;align-items:center;justify-content:center;font-family:Oswald,sans-serif;font-weight:700;font-size:18px}
|
|
128
|
+
.rules b{font-family:Oswald,sans-serif;font-weight:600}
|
|
129
|
+
|
|
130
|
+
ol.steps{margin:4px 0 0;padding-left:22px}
|
|
131
|
+
ol.steps li{margin-bottom:4px}
|
|
132
|
+
|
|
133
|
+
/* résumé */
|
|
134
|
+
.resume{border-top:3px solid var(--ink);border-bottom:3px solid var(--ink);padding:12px 0;margin:0 0 20px}
|
|
135
|
+
.resume p{margin:0 0 8px;font-size:16px}
|
|
136
|
+
.resume p b{font-family:Oswald,sans-serif;font-weight:600}
|
|
137
|
+
.check{background:var(--mustard);color:var(--on-mustard);border:3px solid var(--ink);box-shadow:3px 4px 0 var(--ink);padding:16px 18px}
|
|
138
|
+
.check h3{color:var(--on-mustard)}
|
|
139
|
+
.check ol{margin:0;padding-left:20px}
|
|
140
|
+
.check li{margin-bottom:10px;font-size:16px}
|
|
141
|
+
.check code{background:rgba(255,255,255,.45);color:var(--on-mustard)}
|
|
142
|
+
.check .reply{font-family:Oswald,sans-serif;font-size:14px;margin:10px 0 0}
|
|
143
|
+
|
|
144
|
+
/* nav */
|
|
145
|
+
.nav{display:flex;align-items:center;justify-content:space-between;gap:12px;margin-top:26px}
|
|
146
|
+
.nav button{font-family:Oswald,sans-serif;font-weight:600;font-size:16px;padding:9px 20px;background:var(--paper);color:var(--ink);border:2.5px solid var(--ink);box-shadow:3px 3px 0 var(--ink);cursor:pointer}
|
|
147
|
+
.nav button:disabled{opacity:.4;cursor:default;box-shadow:none}
|
|
148
|
+
.nav button:not(:disabled):active{transform:translate(2px,2px);box-shadow:1px 1px 0 var(--ink)}
|
|
149
|
+
.nav button:focus-visible{outline:3px solid var(--ink);outline-offset:3px}
|
|
150
|
+
.nav .where{font-family:Oswald,sans-serif;font-size:15px;color:var(--on-mustard)}
|
|
151
|
+
@media (prefers-color-scheme:dark){:root:not([data-theme="light"]) .nav .where{color:var(--ink)}}
|
|
152
|
+
:root[data-theme="dark"] .nav .where{color:var(--ink)}
|
|
153
|
+
|
|
154
|
+
@media (max-width:820px){
|
|
155
|
+
.book{grid-template-columns:1fr;box-shadow:0 20px 40px -18px var(--desk-shadow)}
|
|
156
|
+
.page{padding:36px 24px 56px;min-height:0}
|
|
157
|
+
.page.left,.page.right{background:var(--paper);box-shadow:none}
|
|
158
|
+
.page.left{border-bottom:1px dashed var(--rule)}
|
|
159
|
+
.left .folio,.right .folio{right:24px}
|
|
160
|
+
h2.chap{margin-top:10px}
|
|
161
|
+
}
|
|
162
|
+
@media (prefers-reduced-motion:no-preference){
|
|
163
|
+
.spread:not([hidden]) .page{animation:turn .28s ease-out}
|
|
164
|
+
@keyframes turn{from{opacity:.4}to{opacity:1}}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
.feat{width:100%;max-width:380px;display:block;margin:4px auto 10px}
|
|
168
|
+
.legend{margin:6px 0 0;padding:0;list-style:none}
|
|
169
|
+
.legend li{padding:6px 0;border-bottom:1px solid var(--rule);font-size:16px}
|
|
170
|
+
.legend li code{font-weight:600}
|
|
171
|
+
.pubpriv{display:grid;grid-template-columns:1fr 1fr;gap:10px;margin:10px 0}
|
|
172
|
+
.pubpriv div{border:2.5px solid var(--ink);padding:10px 12px;font-size:15.5px}
|
|
173
|
+
.pubpriv div h4{margin:0 0 4px}
|
|
174
|
+
.pubpriv .pub{background:var(--mustard);color:var(--on-mustard)}
|
|
175
|
+
.pubpriv .pub code{background:rgba(255,255,255,.45);color:var(--on-mustard)}
|
|
176
|
+
.course{margin:0;padding:0;list-style:none;counter-reset:c}
|
|
177
|
+
.course li{counter-increment:c;display:grid;grid-template-columns:28px 1fr;gap:8px;padding:5px 0;border-bottom:1px solid var(--rule);font-size:15px;line-height:1.4}
|
|
178
|
+
.course li::before{content:counter(c);font-family:Oswald,sans-serif;font-weight:700}
|
|
179
|
+
.course b{font-family:Oswald,sans-serif;font-weight:600}
|
|
180
|
+
|
|
181
|
+
.index{list-style:none;padding:0;margin:6px 0 0}
|
|
182
|
+
.index li{border-bottom:1px solid var(--rule)}
|
|
183
|
+
.index button{all:unset;box-sizing:border-box;cursor:pointer;width:100%;display:grid;grid-template-columns:34px 1fr auto;gap:10px;align-items:baseline;padding:8px 2px;font-size:16.5px;color:var(--ink)}
|
|
184
|
+
.index button:hover .t2,.index button:focus-visible .t2{text-decoration:underline;text-decoration-color:var(--mustard-deep);text-decoration-thickness:3px;text-underline-offset:3px}
|
|
185
|
+
.index button:focus-visible{outline:3px solid var(--ink);outline-offset:2px}
|
|
186
|
+
.index .n{font-family:Oswald,sans-serif;font-weight:700}
|
|
187
|
+
.index .pg{font-family:Oswald,sans-serif;font-size:13px;color:var(--ink-soft)}
|
|
188
|
+
.index li.end .n{background:var(--mustard);color:var(--on-mustard);text-align:center;border:1.5px solid var(--ink)}
|
|
189
|
+
.page[hidden]{display:none}
|
|
190
|
+
.chaptag{position:absolute;top:22px;left:50px;font-family:Oswald,sans-serif;font-size:11.5px;letter-spacing:.06em;text-transform:uppercase;color:var(--ink-soft)}
|
|
191
|
+
.page.right .chaptag{left:auto;right:50px}
|
|
192
|
+
.sheet h4{margin:14px 0 4px}
|
|
193
|
+
.sheet pre{font-size:11.5px;margin:6px 0 8px}
|
|
194
|
+
.ruleset .rules li{font-size:15.5px}
|
|
195
|
+
.nav .mid{display:flex;align-items:center;gap:12px}
|
|
196
|
+
@media (max-width:820px){.chaptag{left:24px}.page.right .chaptag{right:24px}}
|
|
197
|
+
</style>
|
|
198
|
+
</head>
|
|
199
|
+
<body>
|
|
200
|
+
<div class="wrap">
|
|
201
|
+
<header class="progress" aria-label="Reading progress">
|
|
202
|
+
<h1>Front-end structure: React, Router, Redux on Vite+</h1>
|
|
203
|
+
<div class="meta" id="meta">Introduction</div>
|
|
204
|
+
<div class="bar" role="progressbar" aria-valuemin="0" aria-valuemax="10" aria-valuenow="0" id="bar">
|
|
205
|
+
<div class="seg"></div><div class="seg"></div><div class="seg"></div><div class="seg"></div><div class="seg"></div><div class="seg"></div><div class="seg"></div><div class="seg"></div><div class="seg"></div><div class="seg"></div>
|
|
206
|
+
</div>
|
|
207
|
+
</header>
|
|
208
|
+
<div class="book" id="book">
|
|
209
|
+
<article class="page" data-ch="0" hidden>
|
|
210
|
+
<h2 class="chap" style="margin-top:20px">Front-end</h2>
|
|
211
|
+
<p class="sub">structure, one room at a time</p>
|
|
212
|
+
<p>A course in 10 steps on how to organize a front-end app built with <b>React</b>, <b>React Router</b> and <b>Redux Toolkit</b> on <b>Vite+</b>.</p>
|
|
213
|
+
<p>The goal: code that is split into small, separate modules. Every piece has one place. Any feature can be found, debugged and deleted without breaking the rest. This matters most when AI writes part of the code.</p>
|
|
214
|
+
<div class="insight"><b>METHOD</b><span>Divide and conquer. Small bits. One step at a time, each closed by a checkpoint.</span></div>
|
|
215
|
+
<h4>How to read</h4>
|
|
216
|
+
<p class="small">Each chapter opens with the problem, gives one insight, shows a diagram and the code, then ends with a résumé and a checkpoint. Use the arrow keys or the buttons below. The bar at the top shows the current chapter.</p>
|
|
217
|
+
<p class="small">The last two pages hold all the rules and a cheat sheet for daily work.</p>
|
|
218
|
+
<span class="folio">1</span></article>
|
|
219
|
+
<article class="page" data-ch="0" hidden>
|
|
220
|
+
<h3>Index</h3>
|
|
221
|
+
<ol class="index"><li><button type="button" data-go="2"><span class="n">1</span><span class="t2">Three zones</span><span class="pg">p. 3</span></button></li><li><button type="button" data-go="8"><span class="n">2</span><span class="t2">Inside a feature</span><span class="pg">p. 9</span></button></li><li><button type="button" data-go="14"><span class="n">3</span><span class="t2">The app shell</span><span class="pg">p. 15</span></button></li><li><button type="button" data-go="20"><span class="n">4</span><span class="t2">Routes per feature</span><span class="pg">p. 21</span></button></li><li><button type="button" data-go="26"><span class="n">5</span><span class="t2">State per feature</span><span class="pg">p. 27</span></button></li><li><button type="button" data-go="32"><span class="n">6</span><span class="t2">Server data per feature</span><span class="pg">p. 33</span></button></li><li><button type="button" data-go="38"><span class="n">7</span><span class="t2">The shared layer</span><span class="pg">p. 39</span></button></li><li><button type="button" data-go="44"><span class="n">8</span><span class="t2">Enforcing boundaries</span><span class="pg">p. 45</span></button></li><li><button type="button" data-go="50"><span class="n">9</span><span class="t2">One prompt, one module</span><span class="pg">p. 51</span></button></li><li><button type="button" data-go="56"><span class="n">10</span><span class="t2">Tests per module</span><span class="pg">p. 57</span></button></li><li class="end"><button type="button" data-go="64"><span class="n">★</span><span class="t2">Rules and cheat sheet</span><span class="pg">p. 65</span></button></li></ol>
|
|
222
|
+
<p class="small" style="margin-top:14px">Rules collected along the way: A imports go down, B use the door, C no circles. All of them are on page 65.</p>
|
|
223
|
+
<span class="folio">2</span></article>
|
|
224
|
+
<article class="page" data-ch="1" hidden><div style="display:flex;flex-direction:column;justify-content:center;height:100%;min-height:520px">
|
|
225
|
+
<div style="font-family:Oswald,sans-serif;font-weight:700;font-size:clamp(120px,16vw,190px);line-height:.9;color:var(--mustard-deep)">01</div>
|
|
226
|
+
<h2 class="chap" style="margin-top:18px">Three zones</h2>
|
|
227
|
+
<p style="font-size:19px;margin-top:6px">Where every file lives, and which way imports point.</p>
|
|
228
|
+
<div class="bar" style="margin-top:28px;max-width:320px;border-color:var(--ink)"><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div></div>
|
|
229
|
+
<p class="small" style="margin-top:8px">Chapter 1 of 10</p>
|
|
230
|
+
</div><span class="folio">3</span></article>
|
|
231
|
+
<article class="page" data-ch="1" hidden><span class="chaptag">Chapter 1: Three zones</span>
|
|
232
|
+
<h2 class="chap">Chapter 1:</h2>
|
|
233
|
+
<p class="sub">Three zones</p>
|
|
234
|
+
<p>Spaghetti code has one cause: any file is allowed to import any other file. Once everything depends on everything, nothing can be removed without breaking something unrelated.</p>
|
|
235
|
+
<p>AI code generation accelerates this. Each prompt drops files wherever they seem to fit, and the web of imports grows with every request.</p>
|
|
236
|
+
<div class="insight"><b>INSIGHT #1</b><span>The app is a thin shell that plugs in independent features, and every feature stands on a shared base.</span></div>
|
|
237
|
+
<h4>Scope of this step</h4>
|
|
238
|
+
<p>Where every file lives, and which way imports are allowed to point. Nothing else. What goes inside a feature folder is step 2.</p>
|
|
239
|
+
<span class="folio">4</span></article>
|
|
240
|
+
<article class="page" data-ch="1" hidden><span class="chaptag">Chapter 1: Three zones</span>
|
|
241
|
+
<h3>The three zones</h3>
|
|
242
|
+
<svg class="diagram" viewBox="0 0 360 362" role="img" aria-label="App at the top points down to features, features point down to shared. An upward arrow from shared to features is crossed out.">
|
|
243
|
+
<path class="s" data-r="100,8,160,58,3"/>
|
|
244
|
+
<text class="t" x="180" y="36">APP</text>
|
|
245
|
+
<text class="ts" x="180" y="54">wiring only</text>
|
|
246
|
+
|
|
247
|
+
<path class="s" d="M180 70 L180 104"/><path class="s" d="M172 96 L180 106 L188 96"/>
|
|
248
|
+
|
|
249
|
+
<path class="s" data-r="14,110,332,140,7"/>
|
|
250
|
+
<text class="tl" x="28" y="134">FEATURES</text>
|
|
251
|
+
<path class="s" data-r="30,150,92,80,11"/>
|
|
252
|
+
<path class="s" data-r="134,150,92,80,13"/>
|
|
253
|
+
<path class="s" data-r="238,150,92,80,17"/>
|
|
254
|
+
<rect class="door" x="56" y="143" width="40" height="14"/><text class="td" x="76" y="153">index.ts</text>
|
|
255
|
+
<rect class="door" x="160" y="143" width="40" height="14"/><text class="td" x="180" y="153">index.ts</text>
|
|
256
|
+
<rect class="door" x="264" y="143" width="40" height="14"/><text class="td" x="284" y="153">index.ts</text>
|
|
257
|
+
<text class="t" x="76" y="198">AUTH</text>
|
|
258
|
+
<text class="t" x="180" y="198">CART</text>
|
|
259
|
+
<text class="t" x="284" y="198">PRODUCTS</text>
|
|
260
|
+
|
|
261
|
+
<path class="s" d="M180 254 L180 290"/><path class="s" d="M172 282 L180 292 L188 282"/>
|
|
262
|
+
|
|
263
|
+
<path class="s" data-r="40,296,280,58,23"/>
|
|
264
|
+
<text class="t" x="180" y="324">SHARED</text>
|
|
265
|
+
<text class="ts" x="180" y="342">ui, lib, config</text>
|
|
266
|
+
|
|
267
|
+
<path class="bad" d="M300 294 L300 256"/><path class="bad" d="M293 264 L300 254 L307 264" style="stroke-dasharray:none"/>
|
|
268
|
+
<path class="badx" d="M292 268 L308 284 M308 268 L292 284"/>
|
|
269
|
+
<text class="badt" x="312" y="280">never</text>
|
|
270
|
+
</svg>
|
|
271
|
+
<dl class="zones">
|
|
272
|
+
<dt>app/</dt>
|
|
273
|
+
<dd>Creates the store, the router and the providers. Holds no business logic. Knows every feature.</dd>
|
|
274
|
+
<dt>features/</dt>
|
|
275
|
+
<dd>One folder per user-facing capability. Owns its components, state and API calls. Exposes one door: <code>index.ts</code>.</dd>
|
|
276
|
+
<dt>shared/</dt>
|
|
277
|
+
<dd>Code that knows nothing about the business. A <code>Button</code> has no idea what a cart is.</dd>
|
|
278
|
+
</dl>
|
|
279
|
+
<span class="folio">5</span></article>
|
|
280
|
+
<article class="page" data-ch="1" hidden><span class="chaptag">Chapter 1: Three zones</span>
|
|
281
|
+
<h3>The folder map</h3>
|
|
282
|
+
<pre>src/
|
|
283
|
+
├─ app/
|
|
284
|
+
│ ├─ main.tsx <span class="c">entry, mounts React</span>
|
|
285
|
+
│ ├─ providers.tsx <span class="c">Redux + Router providers</span>
|
|
286
|
+
│ ├─ router.tsx <span class="c">route table</span>
|
|
287
|
+
│ └─ store.ts <span class="c">combines feature slices</span>
|
|
288
|
+
├─ features/
|
|
289
|
+
│ ├─ auth/
|
|
290
|
+
│ │ ├─ <span class="hl">index.ts</span> <span class="c">public API, the only door</span>
|
|
291
|
+
│ │ └─ … <span class="c">private internals (step 2)</span>
|
|
292
|
+
│ ├─ cart/
|
|
293
|
+
│ └─ products/
|
|
294
|
+
└─ shared/
|
|
295
|
+
├─ ui/ <span class="c">Button, Modal, Input</span>
|
|
296
|
+
├─ lib/ <span class="c">formatDate, http client</span>
|
|
297
|
+
└─ config/ <span class="c">env, constants</span></pre>
|
|
298
|
+
<h4>Placement test for any file</h4>
|
|
299
|
+
<ol class="steps">
|
|
300
|
+
<li>Does it mention a business concept (user, cart, order)? It goes in <code>features/<concept>/</code>.</li>
|
|
301
|
+
<li>No business concept, and reusable anywhere? It goes in <code>shared/</code>.</li>
|
|
302
|
+
<li>It only connects pieces together? It goes in <code>app/</code>.</li>
|
|
303
|
+
</ol>
|
|
304
|
+
<p class="small" style="margin-top:12px">Ambiguous case: a component used by two features but naming one concept (<code>UserAvatar</code>) still belongs to that concept’s feature, not to <code>shared/</code>.</p>
|
|
305
|
+
<span class="folio">6</span></article>
|
|
306
|
+
<article class="page" data-ch="1" hidden><span class="chaptag">Chapter 1: Three zones</span>
|
|
307
|
+
<h3>The one-direction rule</h3>
|
|
308
|
+
<ul class="rules">
|
|
309
|
+
<li><b>Imports point down only.</b> app → features → shared. Never upward.</li>
|
|
310
|
+
<li><b>Another feature is entered through its door.</b> Import from <code>@/features/auth</code>, never from a file inside it.</li>
|
|
311
|
+
</ul>
|
|
312
|
+
<pre><span class="c">// features/cart/ui/CartPage.tsx</span>
|
|
313
|
+
import { Button } from '@/shared/ui'
|
|
314
|
+
<span class="ok">✔ down: feature → shared</span>
|
|
315
|
+
import { selectUser } from '@/features/auth'
|
|
316
|
+
<span class="ok">✔ through the door</span>
|
|
317
|
+
import { authSlice } from '@/features/auth/model/slice'
|
|
318
|
+
<span class="bad">✘ rule B: reaching into internals</span>
|
|
319
|
+
|
|
320
|
+
<span class="c">// shared/ui/Header.tsx</span>
|
|
321
|
+
import { selectUser } from '@/features/auth'
|
|
322
|
+
<span class="bad">✘ rule A: shared points up</span></pre>
|
|
323
|
+
<p class="small"><code>@/</code> is a path alias for <code>src/</code>, configured in step 8.</p>
|
|
324
|
+
<h4>The deletion test</h4>
|
|
325
|
+
<ol class="steps">
|
|
326
|
+
<li>Delete one feature folder.</li>
|
|
327
|
+
<li>Run the type checker.</li>
|
|
328
|
+
<li>Errors only in <code>app/</code> (a route line, a store line): boundaries hold.</li>
|
|
329
|
+
<li>Errors anywhere else: a boundary leaked.</li>
|
|
330
|
+
</ol>
|
|
331
|
+
<p class="small" style="margin-top:10px">This test is the debugging tool for AI output: generated code that passes it can be removed in one move.</p>
|
|
332
|
+
<span class="folio">7</span></article>
|
|
333
|
+
<article class="page" data-ch="1" hidden><span class="chaptag">Chapter 1: Three zones</span>
|
|
334
|
+
<h2 class="chap" style="font-size:clamp(36px,4.4vw,48px);margin-top:0">Résumé</h2>
|
|
335
|
+
<div class="resume">
|
|
336
|
+
<p><b>Three zones.</b> app wires, features own business logic, shared knows no business.</p>
|
|
337
|
+
<p><b>Rule A.</b> Imports point down: app → features → shared.</p>
|
|
338
|
+
<p><b>Rule B.</b> Features meet each other only through <code>index.ts</code>.</p>
|
|
339
|
+
<p><b>Deletion test.</b> Removing a feature breaks only <code>app/</code>.</p>
|
|
340
|
+
</div>
|
|
341
|
+
<div class="check">
|
|
342
|
+
<h3>Checkpoint 1 (easy version)</h3>
|
|
343
|
+
<p style="font-size:15px;margin-bottom:10px">3 small questions. Short answers. Write them in the chat.</p>
|
|
344
|
+
<ol>
|
|
345
|
+
<li>Which folder: <code>app</code>, <code>features</code> or <code>shared</code>?<br>a) A price label that works in any app.<br>b) An “Add to cart” button.<br>c) The list of all pages (routes).</li>
|
|
346
|
+
<li>The cart wants the user from auth. Which import is good: <b>A</b> or <b>B</b>?<br>A) <code>from '@/features/auth'</code><br>B) <code>from '@/features/auth/model/selectors'</code></li>
|
|
347
|
+
<li>The products folder is deleted. Two files break. Which break is a mistake: <b>A</b> or <b>B</b>?<br>A) <code>app/router.tsx</code><br>B) <code>shared/ui/ProductCard.tsx</code></li>
|
|
348
|
+
</ol>
|
|
349
|
+
<p class="reply">Reply like this: 1) a=… b=… c=… 2) … 3) …</p>
|
|
350
|
+
</div>
|
|
351
|
+
<span class="folio">8</span></article>
|
|
352
|
+
<article class="page" data-ch="2" hidden><div style="display:flex;flex-direction:column;justify-content:center;height:100%;min-height:520px">
|
|
353
|
+
<div style="font-family:Oswald,sans-serif;font-weight:700;font-size:clamp(120px,16vw,190px);line-height:.9;color:var(--mustard-deep)">02</div>
|
|
354
|
+
<h2 class="chap" style="margin-top:18px">Inside a feature</h2>
|
|
355
|
+
<p style="font-size:19px;margin-top:6px">Rooms, one door, and the rule inside the house.</p>
|
|
356
|
+
<div class="bar" style="margin-top:28px;max-width:320px;border-color:var(--ink)"><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div></div>
|
|
357
|
+
<p class="small" style="margin-top:8px">Chapter 2 of 10</p>
|
|
358
|
+
</div><span class="folio">9</span></article>
|
|
359
|
+
<article class="page" data-ch="2" hidden><span class="chaptag">Chapter 2: Inside a feature</span>
|
|
360
|
+
<h2 class="chap">Chapter 2:</h2>
|
|
361
|
+
<p class="sub">Inside a feature</p>
|
|
362
|
+
<p>Step 1 put every feature in its own folder. Now the question is: what goes <i>inside</i> that folder?</p>
|
|
363
|
+
<p>The answer is simple. A feature is like a small house. It has rooms inside, and it has one front door. People outside can only use the front door.</p>
|
|
364
|
+
<div class="insight"><b>INSIGHT #2</b><span>A feature is a small app with rooms inside and one door: index.ts.</span></div>
|
|
365
|
+
<h4>Scope of this step</h4>
|
|
366
|
+
<p>The rooms of a feature, the door, and one rule inside the house. The example feature is <code>cart</code>.</p>
|
|
367
|
+
<span class="folio">10</span></article>
|
|
368
|
+
<article class="page" data-ch="2" hidden><span class="chaptag">Chapter 2: Inside a feature</span>
|
|
369
|
+
<h3>The house: features/cart</h3>
|
|
370
|
+
<svg class="feat diagram" viewBox="0 0 360 330" role="img" aria-label="The cart feature as a house with rooms ui, model, api, lib and types. The only way in is the door index.ts at the bottom.">
|
|
371
|
+
<path class="s" data-r="14,10,332,262,5"/>
|
|
372
|
+
<text class="tl" x="28" y="34">FEATURES / CART</text>
|
|
373
|
+
<path class="s" data-r="30,50,140,82,9"/>
|
|
374
|
+
<text class="t" x="100" y="86">UI</text>
|
|
375
|
+
<text class="ts" x="100" y="104">what the user sees</text>
|
|
376
|
+
<path class="s" data-r="190,50,140,82,12"/>
|
|
377
|
+
<text class="t" x="260" y="86">MODEL</text>
|
|
378
|
+
<text class="ts" x="260" y="104">Redux state</text>
|
|
379
|
+
<path class="s" d="M174 91 L186 91"/><path class="s" d="M180 85 L188 91 L180 97"/>
|
|
380
|
+
<path class="s" data-r="30,150,92,72,15"/>
|
|
381
|
+
<text class="t" x="76" y="184">API</text>
|
|
382
|
+
<text class="ts" x="76" y="202">server calls</text>
|
|
383
|
+
<path class="s" data-r="134,150,92,72,18"/>
|
|
384
|
+
<text class="t" x="180" y="184">LIB</text>
|
|
385
|
+
<text class="ts" x="180" y="202">small helpers</text>
|
|
386
|
+
<path class="s" data-r="238,150,92,72,21"/>
|
|
387
|
+
<text class="t" x="284" y="184">TYPES</text>
|
|
388
|
+
<text class="ts" x="284" y="202">data shapes</text>
|
|
389
|
+
<rect class="door" x="130" y="258" width="100" height="30"/>
|
|
390
|
+
<text class="td" x="180" y="278" style="font-size:13px;font-weight:700">index.ts</text>
|
|
391
|
+
<path class="s" d="M180 322 L180 296"/><path class="s" d="M174 304 L180 294 L186 304"/>
|
|
392
|
+
<text class="ts" x="180" y="316" dx="44">outside world</text>
|
|
393
|
+
</svg>
|
|
394
|
+
<p class="small">Not every room is required. Create a room only when the feature needs it. A tiny feature can have only <code>ui/</code> and <code>index.ts</code>.</p>
|
|
395
|
+
<span class="folio">11</span></article>
|
|
396
|
+
<article class="page" data-ch="2" hidden><span class="chaptag">Chapter 2: Inside a feature</span>
|
|
397
|
+
<h3>The rooms, one by one</h3>
|
|
398
|
+
<pre>features/cart/
|
|
399
|
+
├─ <span class="hl">index.ts</span> <span class="c">the door</span>
|
|
400
|
+
├─ ui/
|
|
401
|
+
│ ├─ CartPage.tsx
|
|
402
|
+
│ ├─ CartItem.tsx
|
|
403
|
+
│ └─ AddToCartButton.tsx
|
|
404
|
+
├─ model/
|
|
405
|
+
│ ├─ cartSlice.ts <span class="c">Redux slice</span>
|
|
406
|
+
│ └─ selectors.ts
|
|
407
|
+
├─ api/
|
|
408
|
+
│ └─ cartApi.ts <span class="c">(step 6)</span>
|
|
409
|
+
├─ lib/
|
|
410
|
+
│ └─ calcTotal.ts
|
|
411
|
+
└─ types.ts</pre>
|
|
412
|
+
<ul class="legend">
|
|
413
|
+
<li><code>ui/</code> React components. Things the user sees and clicks.</li>
|
|
414
|
+
<li><code>model/</code> The state: Redux slice, actions, selectors.</li>
|
|
415
|
+
<li><code>api/</code> Talking to the server.</li>
|
|
416
|
+
<li><code>lib/</code> Small helper functions used only by this feature.</li>
|
|
417
|
+
<li><code>types.ts</code> TypeScript shapes of the data, like <code>CartLine</code>.</li>
|
|
418
|
+
</ul>
|
|
419
|
+
<span class="folio">12</span></article>
|
|
420
|
+
<article class="page" data-ch="2" hidden><span class="chaptag">Chapter 2: Inside a feature</span>
|
|
421
|
+
<h3>The door: index.ts</h3>
|
|
422
|
+
<p>The door decides what the outside world can use. Anything not listed here is private.</p>
|
|
423
|
+
<pre><span class="c">// features/cart/index.ts</span>
|
|
424
|
+
export { CartPage } from './ui/CartPage'
|
|
425
|
+
export { AddToCartButton } from './ui/AddToCartButton'
|
|
426
|
+
export { cartReducer } from './model/cartSlice'
|
|
427
|
+
export { selectCartCount } from './model/selectors'
|
|
428
|
+
<span class="c">// CartItem, calcTotal: not here, so private</span></pre>
|
|
429
|
+
<div class="pubpriv">
|
|
430
|
+
<div class="pub"><h4>Public</h4>Listed in <code>index.ts</code>. Others may use it.</div>
|
|
431
|
+
<div><h4>Private</h4>Not listed. Can change or be deleted freely.</div>
|
|
432
|
+
</div>
|
|
433
|
+
<h4>One rule inside the house</h4>
|
|
434
|
+
<ul class="rules">
|
|
435
|
+
<li><b>ui may use model. model never uses ui.</b> State does not know about buttons or pages.</li>
|
|
436
|
+
</ul>
|
|
437
|
+
<pre><span class="c">// ui/CartPage.tsx</span>
|
|
438
|
+
import { selectCartCount } from '../model/selectors'
|
|
439
|
+
<span class="ok">✔ ui → model</span>
|
|
440
|
+
<span class="c">// model/cartSlice.ts</span>
|
|
441
|
+
import { CartPage } from '../ui/CartPage'
|
|
442
|
+
<span class="bad">✘ model → ui</span></pre>
|
|
443
|
+
<p class="small">Inside the house, files use short paths like <code>../model</code>. From outside, only <code>@/features/cart</code>.</p>
|
|
444
|
+
<span class="folio">13</span></article>
|
|
445
|
+
<article class="page" data-ch="2" hidden><span class="chaptag">Chapter 2: Inside a feature</span>
|
|
446
|
+
<h2 class="chap" style="font-size:clamp(36px,4.4vw,48px);margin-top:0">Résumé</h2>
|
|
447
|
+
<div class="resume">
|
|
448
|
+
<p><b>A feature is a small house.</b> Rooms: ui, model, api, lib, types.</p>
|
|
449
|
+
<p><b>One door.</b> <code>index.ts</code> lists what others may use. The rest is private.</p>
|
|
450
|
+
<p><b>Inside rule.</b> ui may use model. model never uses ui.</p>
|
|
451
|
+
<p><b>Only needed rooms.</b> Small feature, few rooms.</p>
|
|
452
|
+
</div>
|
|
453
|
+
<div class="check">
|
|
454
|
+
<h3>Checkpoint 2</h3>
|
|
455
|
+
<p style="font-size:15px;margin-bottom:10px">3 small questions. Short answers. Write them in the chat.</p>
|
|
456
|
+
<ol>
|
|
457
|
+
<li>Which room: <code>ui</code>, <code>model</code> or <code>lib</code>?<br>a) <code>cartSlice.ts</code> (the Redux state)<br>b) <code>CartItem.tsx</code> (one row in the cart list)<br>c) <code>calcTotal.ts</code> (adds the prices)</li>
|
|
458
|
+
<li>The checkout feature needs <code>CartItem</code>, but it is private. What is right: <b>A</b> or <b>B</b>?<br>A) Import <code>'@/features/cart/ui/CartItem'</code><br>B) Add <code>CartItem</code> to cart’s <code>index.ts</code>, then import <code>'@/features/cart'</code></li>
|
|
459
|
+
<li>Which import is a mistake: <b>A</b> or <b>B</b>?<br>A) In <code>ui/CartPage.tsx</code>: <code>from '../model/selectors'</code><br>B) In <code>model/cartSlice.ts</code>: <code>from '../ui/CartItem'</code></li>
|
|
460
|
+
</ol>
|
|
461
|
+
<p class="reply">Reply like this: 1) a=… b=… c=… 2) … 3) …</p>
|
|
462
|
+
</div>
|
|
463
|
+
<span class="folio">14</span></article>
|
|
464
|
+
<article class="page" data-ch="3" hidden><div style="display:flex;flex-direction:column;justify-content:center;height:100%;min-height:520px">
|
|
465
|
+
<div style="font-family:Oswald,sans-serif;font-weight:700;font-size:clamp(120px,16vw,190px);line-height:.9;color:var(--mustard-deep)">03</div>
|
|
466
|
+
<h2 class="chap" style="margin-top:18px">The app shell</h2>
|
|
467
|
+
<p style="font-size:19px;margin-top:6px">The four files that connect everything.</p>
|
|
468
|
+
<div class="bar" style="margin-top:28px;max-width:320px;border-color:var(--ink)"><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div></div>
|
|
469
|
+
<p class="small" style="margin-top:8px">Chapter 3 of 10</p>
|
|
470
|
+
</div><span class="folio">15</span></article>
|
|
471
|
+
<article class="page" data-ch="3" hidden><span class="chaptag">Chapter 3: The app shell</span>
|
|
472
|
+
<h2 class="chap">Chapter 3:</h2>
|
|
473
|
+
<p class="sub">The app shell</p>
|
|
474
|
+
<p>The features are houses. But houses alone do nothing. They need electricity and a street.</p>
|
|
475
|
+
<p>In a React app, the electricity is the <b>Redux store</b> (shared state). The street is the <b>router</b> (which page shows for which URL). The <code>app/</code> folder connects every house to both.</p>
|
|
476
|
+
<div class="insight"><b>INSIGHT #3</b><span>app/ is the power panel. It connects features to the store and the router. It builds nothing itself.</span></div>
|
|
477
|
+
<h4>Scope of this step</h4>
|
|
478
|
+
<p>The four files in <code>app/</code>, the order they run in, and what must never go inside them.</p>
|
|
479
|
+
<span class="folio">16</span></article>
|
|
480
|
+
<article class="page" data-ch="3" hidden><span class="chaptag">Chapter 3: The app shell</span>
|
|
481
|
+
<h3>How the app starts</h3>
|
|
482
|
+
<svg class="feat diagram" viewBox="0 0 360 392" role="img" aria-label="Start order: index.html, then main.tsx, then providers.tsx, which uses store.ts and router.tsx, which both use the feature doors.">
|
|
483
|
+
<path class="s" data-r="110,6,140,48,3"/>
|
|
484
|
+
<text class="t" x="180" y="30">INDEX.HTML</text>
|
|
485
|
+
<text class="ts" x="180" y="46">has div#root</text>
|
|
486
|
+
<path class="s" d="M180 58 L180 82"/><path class="s" d="M173 74 L180 84 L187 74"/>
|
|
487
|
+
|
|
488
|
+
<path class="s" data-r="110,88,140,48,8"/>
|
|
489
|
+
<text class="t" x="180" y="112">MAIN.TSX</text>
|
|
490
|
+
<text class="ts" x="180" y="128">starts React</text>
|
|
491
|
+
<path class="s" d="M180 140 L180 164"/><path class="s" d="M173 156 L180 166 L187 156"/>
|
|
492
|
+
|
|
493
|
+
<path class="s" data-r="100,170,160,48,12"/>
|
|
494
|
+
<text class="t" x="180" y="194">PROVIDERS.TSX</text>
|
|
495
|
+
<text class="ts" x="180" y="210">plugs in store + router</text>
|
|
496
|
+
<path class="s" d="M140 222 L100 252"/><path class="s" d="M100 242 L98 254 L110 252"/>
|
|
497
|
+
<path class="s" d="M220 222 L260 252"/><path class="s" d="M250 252 L262 254 L260 242"/>
|
|
498
|
+
|
|
499
|
+
<path class="s" data-r="24,258,140,48,15"/>
|
|
500
|
+
<text class="t" x="94" y="282">STORE.TS</text>
|
|
501
|
+
<text class="ts" x="94" y="298">electricity</text>
|
|
502
|
+
<path class="s" data-r="196,258,140,48,19"/>
|
|
503
|
+
<text class="t" x="266" y="282">ROUTER.TSX</text>
|
|
504
|
+
<text class="ts" x="266" y="298">street</text>
|
|
505
|
+
<path class="s" d="M94 310 L94 336"/><path class="s" d="M87 328 L94 338 L101 328"/>
|
|
506
|
+
<path class="s" d="M266 310 L266 336"/><path class="s" d="M259 328 L266 338 L273 328"/>
|
|
507
|
+
|
|
508
|
+
<rect class="door" x="24" y="342" width="312" height="40"/>
|
|
509
|
+
<text class="td" x="180" y="367" style="font-size:13px;font-weight:700">FEATURE DOORS (index.ts)</text>
|
|
510
|
+
</svg>
|
|
511
|
+
<p class="small">The yellow bar is the only contact point between <code>app/</code> and the features. app never opens a room directly.</p>
|
|
512
|
+
<span class="folio">17</span></article>
|
|
513
|
+
<article class="page" data-ch="3" hidden><span class="chaptag">Chapter 3: The app shell</span>
|
|
514
|
+
<h3>The four files</h3>
|
|
515
|
+
<pre><span class="c">// app/main.tsx: start React</span>
|
|
516
|
+
import { StrictMode } from 'react'
|
|
517
|
+
import { createRoot } from 'react-dom/client'
|
|
518
|
+
import { Providers } from './providers'
|
|
519
|
+
|
|
520
|
+
createRoot(document.getElementById('root')!)
|
|
521
|
+
.render(<StrictMode><Providers /></StrictMode>)
|
|
522
|
+
|
|
523
|
+
<span class="c">// app/providers.tsx: plug in store + router</span>
|
|
524
|
+
import { Provider } from 'react-redux'
|
|
525
|
+
import { RouterProvider } from 'react-router'
|
|
526
|
+
import { store } from './store'
|
|
527
|
+
import { router } from './router'
|
|
528
|
+
|
|
529
|
+
export function Providers() {
|
|
530
|
+
return (
|
|
531
|
+
<Provider store={store}>
|
|
532
|
+
<RouterProvider router={router} />
|
|
533
|
+
</Provider>
|
|
534
|
+
)
|
|
535
|
+
}</pre>
|
|
536
|
+
<p class="small"><code>main.tsx</code> runs once. <code>providers.tsx</code> wraps the whole app, so every feature can reach the store and the router.</p>
|
|
537
|
+
<span class="folio">18</span></article>
|
|
538
|
+
<article class="page" data-ch="3" hidden><span class="chaptag">Chapter 3: The app shell</span>
|
|
539
|
+
<h3>store.ts and router.tsx</h3>
|
|
540
|
+
<pre><span class="c">// app/store.ts: one line per feature</span>
|
|
541
|
+
import { configureStore } from '@reduxjs/toolkit'
|
|
542
|
+
import { cartReducer } from '@/features/cart'
|
|
543
|
+
import { authReducer } from '@/features/auth'
|
|
544
|
+
|
|
545
|
+
export const store = configureStore({
|
|
546
|
+
reducer: {
|
|
547
|
+
<span class="hl">cart: cartReducer,</span>
|
|
548
|
+
<span class="hl">auth: authReducer,</span>
|
|
549
|
+
},
|
|
550
|
+
})
|
|
551
|
+
|
|
552
|
+
<span class="c">// app/router.tsx: one line per page</span>
|
|
553
|
+
import { createBrowserRouter } from 'react-router'
|
|
554
|
+
import { CartPage } from '@/features/cart'
|
|
555
|
+
import { LoginPage } from '@/features/auth'
|
|
556
|
+
|
|
557
|
+
export const router = createBrowserRouter([
|
|
558
|
+
<span class="hl">{ path: '/cart', element: <CartPage /> },</span>
|
|
559
|
+
<span class="hl">{ path: '/login', element: <LoginPage /> },</span>
|
|
560
|
+
])</pre>
|
|
561
|
+
<p class="small">Each yellow line is one feature plugged in. Remove a feature, remove its yellow lines. That is the deletion test from step 1.</p>
|
|
562
|
+
<h4>Never inside app/</h4>
|
|
563
|
+
<ul class="rules">
|
|
564
|
+
<li><b>No business logic.</b> No <code>fetch</code>, no price math, no forms. Those live in a feature.</li>
|
|
565
|
+
</ul>
|
|
566
|
+
<p class="small">Open question for step 5: features need the store’s types, but Rule A forbids importing from <code>app/</code>. Step 5 solves this.</p>
|
|
567
|
+
<span class="folio">19</span></article>
|
|
568
|
+
<article class="page" data-ch="3" hidden><span class="chaptag">Chapter 3: The app shell</span>
|
|
569
|
+
<h2 class="chap" style="font-size:clamp(36px,4.4vw,48px);margin-top:0">Résumé</h2>
|
|
570
|
+
<div class="resume">
|
|
571
|
+
<p><b>app/ is the power panel.</b> It connects, it builds nothing.</p>
|
|
572
|
+
<p><b>Start order.</b> index.html → main.tsx → providers.tsx → store + router.</p>
|
|
573
|
+
<p><b>One line per feature.</b> In store.ts and router.tsx, always through the door.</p>
|
|
574
|
+
<p><b>No business logic</b> in app/.</p>
|
|
575
|
+
</div>
|
|
576
|
+
<div class="check">
|
|
577
|
+
<h3>Checkpoint 3</h3>
|
|
578
|
+
<p style="font-size:15px;margin-bottom:10px">3 small questions. Short answers. Write them in the chat.</p>
|
|
579
|
+
<ol>
|
|
580
|
+
<li>Put in start order: A) <code>providers.tsx</code> B) <code>main.tsx</code> C) <code>index.html</code></li>
|
|
581
|
+
<li>Someone writes <code>fetch('/api/cart')</code> inside <code>app/store.ts</code>. Good or mistake? Where should it go?</li>
|
|
582
|
+
<li>The auth feature is deleted. Which 2 files in <code>app/</code> need a line removed?<br><code>main.tsx</code>, <code>providers.tsx</code>, <code>store.ts</code>, <code>router.tsx</code></li>
|
|
583
|
+
</ol>
|
|
584
|
+
<p class="reply">Reply like this: 1) … 2) … 3) …</p>
|
|
585
|
+
</div>
|
|
586
|
+
<span class="folio">20</span></article>
|
|
587
|
+
<article class="page" data-ch="4" hidden><div style="display:flex;flex-direction:column;justify-content:center;height:100%;min-height:520px">
|
|
588
|
+
<div style="font-family:Oswald,sans-serif;font-weight:700;font-size:clamp(120px,16vw,190px);line-height:.9;color:var(--mustard-deep)">04</div>
|
|
589
|
+
<h2 class="chap" style="margin-top:18px">Routes per feature</h2>
|
|
590
|
+
<p style="font-size:19px;margin-top:6px">Each feature brings its own pages.</p>
|
|
591
|
+
<div class="bar" style="margin-top:28px;max-width:320px;border-color:var(--ink)"><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div></div>
|
|
592
|
+
<p class="small" style="margin-top:8px">Chapter 4 of 10</p>
|
|
593
|
+
</div><span class="folio">21</span></article>
|
|
594
|
+
<article class="page" data-ch="4" hidden><span class="chaptag">Chapter 4: Routes per feature</span>
|
|
595
|
+
<h2 class="chap">Chapter 4:</h2>
|
|
596
|
+
<p class="sub">Routes per feature</p>
|
|
597
|
+
<p>In step 3, <code>app/router.tsx</code> listed every page itself. With 3 pages this is fine. With 40 pages it becomes one long file that every feature touches.</p>
|
|
598
|
+
<p>Also, every page is downloaded at the start, even pages the user never opens.</p>
|
|
599
|
+
<div class="insight"><b>INSIGHT #4</b><span>Each feature owns its own list of pages. app/router.tsx only joins the lists.</span></div>
|
|
600
|
+
<h4>Scope of this step</h4>
|
|
601
|
+
<p>A <code>routes.tsx</code> file per feature, a root layout in <code>app/</code>, and loading pages only when needed (<code>lazy</code>).</p>
|
|
602
|
+
<span class="folio">22</span></article>
|
|
603
|
+
<article class="page" data-ch="4" hidden><span class="chaptag">Chapter 4: Routes per feature</span>
|
|
604
|
+
<h3>Each house brings its own pages</h3>
|
|
605
|
+
<svg class="feat diagram" viewBox="0 0 360 330" role="img" aria-label="Three features, auth, cart and products, each send a routes list through their door into app/router.tsx, which joins them under one root layout.">
|
|
606
|
+
<path class="s" data-r="10,8,104,86,3"/>
|
|
607
|
+
<text class="t" x="62" y="38">AUTH</text>
|
|
608
|
+
<text class="ts" x="62" y="58">/login</text>
|
|
609
|
+
<text class="ts" x="62" y="74">/register</text>
|
|
610
|
+
<path class="s" data-r="128,8,104,86,7"/>
|
|
611
|
+
<text class="t" x="180" y="38">CART</text>
|
|
612
|
+
<text class="ts" x="180" y="58">/cart</text>
|
|
613
|
+
<text class="ts" x="180" y="74">/cart/checkout</text>
|
|
614
|
+
<path class="s" data-r="246,8,104,86,11"/>
|
|
615
|
+
<text class="t" x="298" y="38">PRODUCTS</text>
|
|
616
|
+
<text class="ts" x="298" y="58">/</text>
|
|
617
|
+
<text class="ts" x="298" y="74">/products/:id</text>
|
|
618
|
+
<rect class="door" x="37" y="88" width="50" height="14"/><text class="td" x="62" y="98">routes</text>
|
|
619
|
+
<rect class="door" x="155" y="88" width="50" height="14"/><text class="td" x="180" y="98">routes</text>
|
|
620
|
+
<rect class="door" x="273" y="88" width="50" height="14"/><text class="td" x="298" y="98">routes</text>
|
|
621
|
+
|
|
622
|
+
<path class="s" d="M62 106 L150 150"/><path class="s" d="M140 142 L152 151 L138 154"/>
|
|
623
|
+
<path class="s" d="M180 106 L180 146"/><path class="s" d="M173 138 L180 148 L187 138"/>
|
|
624
|
+
<path class="s" d="M298 106 L210 150"/><path class="s" d="M222 154 L208 151 L220 142"/>
|
|
625
|
+
|
|
626
|
+
<path class="s" data-r="70,154,220,56,15"/>
|
|
627
|
+
<text class="t" x="180" y="180">APP/ROUTER.TSX</text>
|
|
628
|
+
<text class="ts" x="180" y="198">joins the lists</text>
|
|
629
|
+
<path class="s" d="M180 214 L180 240"/><path class="s" d="M173 232 L180 242 L187 232"/>
|
|
630
|
+
|
|
631
|
+
<path class="s" data-r="30,246,300,76,19"/>
|
|
632
|
+
<text class="tl" x="44" y="268">ROOT LAYOUT</text>
|
|
633
|
+
<text class="ts" x="180" y="292" style="font-size:12px">header + <Outlet /></text>
|
|
634
|
+
<text class="ts" x="180" y="310">the page for the URL appears in the Outlet</text>
|
|
635
|
+
</svg>
|
|
636
|
+
<p class="small">The feature decides <i>which</i> pages it has. <code>app/</code> decides <i>where</i> they sit in the whole app.</p>
|
|
637
|
+
<span class="folio">23</span></article>
|
|
638
|
+
<article class="page" data-ch="4" hidden><span class="chaptag">Chapter 4: Routes per feature</span>
|
|
639
|
+
<h3>The feature side</h3>
|
|
640
|
+
<pre><span class="c">// features/cart/routes.tsx</span>
|
|
641
|
+
import type { RouteObject } from 'react-router'
|
|
642
|
+
|
|
643
|
+
export const cartRoutes: RouteObject[] = [
|
|
644
|
+
{
|
|
645
|
+
path: 'cart',
|
|
646
|
+
<span class="hl">lazy: () => import('./ui/CartPage')</span>
|
|
647
|
+
.then((m) => ({ Component: m.CartPage })),
|
|
648
|
+
},
|
|
649
|
+
{
|
|
650
|
+
path: 'cart/checkout',
|
|
651
|
+
lazy: () => import('./ui/CheckoutPage')
|
|
652
|
+
.then((m) => ({ Component: m.CheckoutPage })),
|
|
653
|
+
},
|
|
654
|
+
]
|
|
655
|
+
|
|
656
|
+
<span class="c">// features/cart/index.ts (the door)</span>
|
|
657
|
+
export { cartRoutes } from './routes'
|
|
658
|
+
export { CartBadge } from './ui/CartBadge'</pre>
|
|
659
|
+
<h4>What lazy does</h4>
|
|
660
|
+
<p class="small">Without <code>lazy</code>, the cart page code downloads when the app starts. With <code>lazy</code>, Vite puts it in a separate file, and the browser downloads it only when the user opens <code>/cart</code>.</p>
|
|
661
|
+
<p class="small">Paths here have no <code>/</code> at the start (<code>'cart'</code>). They sit inside the root layout.</p>
|
|
662
|
+
<span class="folio">24</span></article>
|
|
663
|
+
<article class="page" data-ch="4" hidden><span class="chaptag">Chapter 4: Routes per feature</span>
|
|
664
|
+
<h3>The app side</h3>
|
|
665
|
+
<pre><span class="c">// app/router.tsx</span>
|
|
666
|
+
import { createBrowserRouter } from 'react-router'
|
|
667
|
+
import { RootLayout } from './RootLayout'
|
|
668
|
+
import { authRoutes } from '@/features/auth'
|
|
669
|
+
import { cartRoutes } from '@/features/cart'
|
|
670
|
+
import { productsRoutes } from '@/features/products'
|
|
671
|
+
|
|
672
|
+
export const router = createBrowserRouter([
|
|
673
|
+
{
|
|
674
|
+
path: '/',
|
|
675
|
+
element: <RootLayout />,
|
|
676
|
+
children: [
|
|
677
|
+
<span class="hl">...productsRoutes,</span>
|
|
678
|
+
<span class="hl">...cartRoutes,</span>
|
|
679
|
+
<span class="hl">...authRoutes,</span>
|
|
680
|
+
],
|
|
681
|
+
},
|
|
682
|
+
])
|
|
683
|
+
|
|
684
|
+
<span class="c">// app/RootLayout.tsx</span>
|
|
685
|
+
import { Outlet } from 'react-router'
|
|
686
|
+
import { CartBadge } from '@/features/cart'
|
|
687
|
+
|
|
688
|
+
export function RootLayout() {
|
|
689
|
+
return (
|
|
690
|
+
<>
|
|
691
|
+
<header>Shop <CartBadge /></header>
|
|
692
|
+
<Outlet />
|
|
693
|
+
</>
|
|
694
|
+
)
|
|
695
|
+
}</pre>
|
|
696
|
+
<p class="small">The layout is wiring, so it lives in <code>app/</code>. The badge knows the cart, so it lives in <code>features/cart</code> and comes through the door. Rule A holds: app → features.</p>
|
|
697
|
+
<span class="folio">25</span></article>
|
|
698
|
+
<article class="page" data-ch="4" hidden><span class="chaptag">Chapter 4: Routes per feature</span>
|
|
699
|
+
<h2 class="chap" style="font-size:clamp(36px,4.4vw,48px);margin-top:0">Résumé</h2>
|
|
700
|
+
<div class="resume">
|
|
701
|
+
<p><b>Feature owns its pages.</b> <code>features/x/routes.tsx</code>, exported from the door.</p>
|
|
702
|
+
<p><b>app joins.</b> <code>router.tsx</code> spreads each list into the root layout.</p>
|
|
703
|
+
<p><b>Root layout</b> in app: header + <code><Outlet /></code>.</p>
|
|
704
|
+
<p><b>lazy</b> downloads a page only when its URL opens.</p>
|
|
705
|
+
</div>
|
|
706
|
+
<div class="check">
|
|
707
|
+
<h3>Checkpoint 4</h3>
|
|
708
|
+
<p style="font-size:15px;margin-bottom:10px">3 small questions. Short answers. Write them in the chat.</p>
|
|
709
|
+
<ol>
|
|
710
|
+
<li>Where is <code>{ path: 'cart/checkout', … }</code> written: <b>A</b> or <b>B</b>?<br>A) <code>app/router.tsx</code><br>B) <code>features/cart/routes.tsx</code></li>
|
|
711
|
+
<li>The header shows a user avatar from auth. Where does <code>UserMenu.tsx</code> live: <b>A</b> or <b>B</b>?<br>A) <code>app/RootLayout.tsx</code> folder<br>B) <code>features/auth/ui/</code>, exported from the door</li>
|
|
712
|
+
<li>With <code>lazy</code>, when does the browser download the checkout page code?</li>
|
|
713
|
+
</ol>
|
|
714
|
+
<p class="reply">Reply like this: 1) … 2) … 3) …</p>
|
|
715
|
+
</div>
|
|
716
|
+
<span class="folio">26</span></article>
|
|
717
|
+
<article class="page" data-ch="5" hidden><div style="display:flex;flex-direction:column;justify-content:center;height:100%;min-height:520px">
|
|
718
|
+
<div style="font-family:Oswald,sans-serif;font-weight:700;font-size:clamp(120px,16vw,190px);line-height:.9;color:var(--mustard-deep)">05</div>
|
|
719
|
+
<h2 class="chap" style="margin-top:18px">State per feature</h2>
|
|
720
|
+
<p style="font-size:19px;margin-top:6px">One drawer of the store per feature.</p>
|
|
721
|
+
<div class="bar" style="margin-top:28px;max-width:320px;border-color:var(--ink)"><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div></div>
|
|
722
|
+
<p class="small" style="margin-top:8px">Chapter 5 of 10</p>
|
|
723
|
+
</div><span class="folio">27</span></article>
|
|
724
|
+
<article class="page" data-ch="5" hidden><span class="chaptag">Chapter 5: State per feature</span>
|
|
725
|
+
<h2 class="chap">Chapter 5:</h2>
|
|
726
|
+
<p class="sub">State per feature</p>
|
|
727
|
+
<p>The Redux store is one big cabinet. Without rules, every file opens every drawer, and nobody knows who changed what.</p>
|
|
728
|
+
<p>The fix: one drawer per feature. The cart feature owns the <code>cart</code> drawer. It writes the code that changes it and the code that reads it. <code>app/store.ts</code> only puts the drawers in the cabinet.</p>
|
|
729
|
+
<div class="insight"><b>INSIGHT #5</b><span>Each feature owns its drawer of the store. Its selectors know only their own drawer.</span></div>
|
|
730
|
+
<h4>Scope of this step</h4>
|
|
731
|
+
<p>A slice, its selectors, using them in ui, and how two features talk without making a circle.</p>
|
|
732
|
+
<span class="folio">28</span></article>
|
|
733
|
+
<article class="page" data-ch="5" hidden><span class="chaptag">Chapter 5: State per feature</span>
|
|
734
|
+
<h3>The cabinet and its drawers</h3>
|
|
735
|
+
<svg class="feat diagram" viewBox="0 0 360 318" role="img" aria-label="The store is a cabinet with three drawers: auth, cart, products. Each feature holds the key to its own drawer only. app/store.ts builds the cabinet.">
|
|
736
|
+
<path class="s" data-r="70,8,220,46,3"/>
|
|
737
|
+
<text class="t" x="180" y="30">APP/STORE.TS</text>
|
|
738
|
+
<text class="ts" x="180" y="46">builds the cabinet</text>
|
|
739
|
+
<path class="s" d="M180 58 L180 80"/><path class="s" d="M173 72 L180 82 L187 72"/>
|
|
740
|
+
|
|
741
|
+
<path class="s" data-r="40,86,280,168,7"/>
|
|
742
|
+
<text class="tl" x="54" y="108">STORE</text>
|
|
743
|
+
<path class="s" data-r="58,118,244,36,11"/>
|
|
744
|
+
<rect class="door" x="160" y="132" width="40" height="8"/>
|
|
745
|
+
<text class="t" x="96" y="142" style="font-size:14px">auth</text>
|
|
746
|
+
<path class="s" data-r="58,162,244,36,13"/>
|
|
747
|
+
<rect class="door" x="160" y="176" width="40" height="8"/>
|
|
748
|
+
<text class="t" x="96" y="186" style="font-size:14px">cart</text>
|
|
749
|
+
<path class="s" data-r="58,206,244,36,17"/>
|
|
750
|
+
<rect class="door" x="160" y="220" width="40" height="8"/>
|
|
751
|
+
<text class="t" x="108" y="230" style="font-size:14px">products</text>
|
|
752
|
+
|
|
753
|
+
<text class="ts" x="270" y="141">features/auth</text>
|
|
754
|
+
<text class="ts" x="270" y="185">features/cart</text>
|
|
755
|
+
<text class="ts" x="270" y="229">features/products</text>
|
|
756
|
+
|
|
757
|
+
<path class="s" data-r="40,268,280,42,21"/>
|
|
758
|
+
<text class="ts" x="180" y="294" style="font-size:12px">store key 'cart' = slice name 'cart'</text>
|
|
759
|
+
</svg>
|
|
760
|
+
<p class="small">The drawer name is a contract. The slice says <code>name: 'cart'</code>, and <code>store.ts</code> must mount it as <code>cart: cartReducer</code>. Same word in both places.</p>
|
|
761
|
+
<span class="folio">29</span></article>
|
|
762
|
+
<article class="page" data-ch="5" hidden><span class="chaptag">Chapter 5: State per feature</span>
|
|
763
|
+
<h3>The drawer code</h3>
|
|
764
|
+
<pre><span class="c">// features/cart/model/cartSlice.ts</span>
|
|
765
|
+
import { createSlice, type PayloadAction }
|
|
766
|
+
from '@reduxjs/toolkit'
|
|
767
|
+
import type { CartLine } from '../types'
|
|
768
|
+
|
|
769
|
+
export type CartState = { lines: CartLine[] }
|
|
770
|
+
const initialState: CartState = { lines: [] }
|
|
771
|
+
|
|
772
|
+
export const cartSlice = createSlice({
|
|
773
|
+
name: 'cart',
|
|
774
|
+
initialState,
|
|
775
|
+
reducers: {
|
|
776
|
+
added(state, action: PayloadAction<CartLine>) {
|
|
777
|
+
state.lines.push(action.payload)
|
|
778
|
+
},
|
|
779
|
+
removed(state, action: PayloadAction<string>) {
|
|
780
|
+
state.lines = state.lines
|
|
781
|
+
.filter((l) => l.id !== action.payload)
|
|
782
|
+
},
|
|
783
|
+
},
|
|
784
|
+
})
|
|
785
|
+
export const { added, removed } = cartSlice.actions
|
|
786
|
+
export const cartReducer = cartSlice.reducer
|
|
787
|
+
|
|
788
|
+
<span class="c">// features/cart/model/selectors.ts</span>
|
|
789
|
+
import type { CartState } from './cartSlice'
|
|
790
|
+
<span class="hl">type WithCart = { cart: CartState }</span>
|
|
791
|
+
|
|
792
|
+
export const selectCartCount =
|
|
793
|
+
(s: WithCart) => s.cart.lines.length</pre>
|
|
794
|
+
<p class="small"><b>Answer to the open question:</b> the selector does not ask for the whole store type. It asks only for “something with a <code>cart</code> drawer”. No import from <code>app/</code>. Rule A holds.</p>
|
|
795
|
+
<span class="folio">30</span></article>
|
|
796
|
+
<article class="page" data-ch="5" hidden><span class="chaptag">Chapter 5: State per feature</span>
|
|
797
|
+
<h3>Using the drawer in ui</h3>
|
|
798
|
+
<pre><span class="c">// features/cart/ui/CartBadge.tsx</span>
|
|
799
|
+
import { useSelector } from 'react-redux'
|
|
800
|
+
import { selectCartCount } from '../model/selectors'
|
|
801
|
+
|
|
802
|
+
export function CartBadge() {
|
|
803
|
+
const count = useSelector(selectCartCount)
|
|
804
|
+
return <span>{count}</span>
|
|
805
|
+
}</pre>
|
|
806
|
+
<h4>Two features talking</h4>
|
|
807
|
+
<p class="small">When the user logs out, the cart must be empty. Cart listens to the auth action through auth’s door.</p>
|
|
808
|
+
<pre><span class="c">// features/cart/model/cartSlice.ts (add)</span>
|
|
809
|
+
import { loggedOut } from '@/features/auth'
|
|
810
|
+
<span class="ok">✔ cart → auth, through the door</span>
|
|
811
|
+
|
|
812
|
+
extraReducers: (builder) => {
|
|
813
|
+
builder.addCase(loggedOut, () => initialState)
|
|
814
|
+
},</pre>
|
|
815
|
+
<ul class="rules" style="counter-reset:r 2">
|
|
816
|
+
<li><b>No circles.</b> If cart imports auth, auth never imports cart.</li>
|
|
817
|
+
</ul>
|
|
818
|
+
<p class="small"><code>import type</code> counts too. A type import from <code>app/</code> inside a feature still breaks Rule A.</p>
|
|
819
|
+
<span class="folio">31</span></article>
|
|
820
|
+
<article class="page" data-ch="5" hidden><span class="chaptag">Chapter 5: State per feature</span>
|
|
821
|
+
<h2 class="chap" style="font-size:clamp(36px,4.4vw,48px);margin-top:0">Résumé</h2>
|
|
822
|
+
<div class="resume">
|
|
823
|
+
<p><b>One drawer per feature.</b> The slice lives in <code>features/x/model/</code>.</p>
|
|
824
|
+
<p><b>Same name twice.</b> Slice <code>name</code> = key in <code>store.ts</code>.</p>
|
|
825
|
+
<p><b>Small selector type.</b> <code>{ cart: CartState }</code>, not the whole store. No import from app.</p>
|
|
826
|
+
<p><b>Rule C.</b> Features may listen to each other through doors, never in a circle.</p>
|
|
827
|
+
</div>
|
|
828
|
+
<div class="check">
|
|
829
|
+
<h3>Checkpoint 5</h3>
|
|
830
|
+
<p style="font-size:15px;margin-bottom:10px">3 small questions. Short answers. Write them in the chat.</p>
|
|
831
|
+
<ol>
|
|
832
|
+
<li>Where is the code that adds an item to the cart state: <b>A</b> or <b>B</b>?<br>A) <code>app/store.ts</code><br>B) <code>features/cart/model/cartSlice.ts</code></li>
|
|
833
|
+
<li>Inside <code>features/cart</code>: <code>import type { RootState } from '@/app/store'</code>. Is a rule broken? Which one?</li>
|
|
834
|
+
<li>Cart already imports <code>loggedOut</code> from auth. Now auth wants <code>selectCartCount</code> from cart. Allowed? Why?</li>
|
|
835
|
+
</ol>
|
|
836
|
+
<p class="reply">Reply like this: 1) … 2) … 3) …</p>
|
|
837
|
+
</div>
|
|
838
|
+
<span class="folio">32</span></article>
|
|
839
|
+
<article class="page" data-ch="6" hidden><div style="display:flex;flex-direction:column;justify-content:center;height:100%;min-height:520px">
|
|
840
|
+
<div style="font-family:Oswald,sans-serif;font-weight:700;font-size:clamp(120px,16vw,190px);line-height:.9;color:var(--mustard-deep)">06</div>
|
|
841
|
+
<h2 class="chap" style="margin-top:18px">Server data per feature</h2>
|
|
842
|
+
<p style="font-size:19px;margin-top:6px">Two kinds of data, one empty pipe.</p>
|
|
843
|
+
<div class="bar" style="margin-top:28px;max-width:320px;border-color:var(--ink)"><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div></div>
|
|
844
|
+
<p class="small" style="margin-top:8px">Chapter 6 of 10</p>
|
|
845
|
+
</div><span class="folio">33</span></article>
|
|
846
|
+
<article class="page" data-ch="6" hidden><span class="chaptag">Chapter 6: Server data per feature</span>
|
|
847
|
+
<h2 class="chap">Chapter 6:</h2>
|
|
848
|
+
<p class="sub">Server data per feature</p>
|
|
849
|
+
<p>Some data is born in the browser: “is the menu open?”. Some data is born on the server: the product list, the saved cart.</p>
|
|
850
|
+
<p>Server data needs loading, errors, caching and refreshing. Writing this by hand inside slices is the most common source of spaghetti. RTK Query does it for you.</p>
|
|
851
|
+
<div class="insight"><b>INSIGHT #6</b><span>Server data lives in the RTK Query cache, not in a slice. shared/ gives one empty pipe. Each feature adds its own taps.</span></div>
|
|
852
|
+
<h4>Scope of this step</h4>
|
|
853
|
+
<p>Two kinds of data, one base API in <code>shared/api</code>, and endpoints injected by each feature.</p>
|
|
854
|
+
<span class="folio">34</span></article>
|
|
855
|
+
<article class="page" data-ch="6" hidden><span class="chaptag">Chapter 6: Server data per feature</span>
|
|
856
|
+
<h3>One pipe, many taps</h3>
|
|
857
|
+
<svg class="feat diagram" viewBox="0 0 360 330" role="img" aria-label="Features auth, cart and products each inject their endpoints into one base API in shared/api, which talks to the server.">
|
|
858
|
+
<path class="s" data-r="10,8,104,62,3"/>
|
|
859
|
+
<text class="t" x="62" y="34">AUTH</text>
|
|
860
|
+
<text class="ts" x="62" y="54">login, me</text>
|
|
861
|
+
<path class="s" data-r="128,8,104,62,7"/>
|
|
862
|
+
<text class="t" x="180" y="34">CART</text>
|
|
863
|
+
<text class="ts" x="180" y="54">getCart, addToCart</text>
|
|
864
|
+
<path class="s" data-r="246,8,104,62,11"/>
|
|
865
|
+
<text class="t" x="298" y="34">PRODUCTS</text>
|
|
866
|
+
<text class="ts" x="298" y="54">getProducts</text>
|
|
867
|
+
|
|
868
|
+
<path class="s" d="M62 74 L62 140"/><path class="s" d="M55 132 L62 142 L69 132"/>
|
|
869
|
+
<path class="s" d="M180 74 L180 140"/><path class="s" d="M173 132 L180 142 L187 132"/>
|
|
870
|
+
<path class="s" d="M298 74 L298 140"/><path class="s" d="M291 132 L298 142 L305 132"/>
|
|
871
|
+
<text class="ts" x="121" y="112" style="font-size:10px">injectEndpoints</text>
|
|
872
|
+
<text class="ts" x="239" y="112" style="font-size:10px">injectEndpoints</text>
|
|
873
|
+
|
|
874
|
+
<rect class="door" x="10" y="146" width="340" height="56"/>
|
|
875
|
+
<text class="td" x="180" y="170" style="font-size:15px;font-weight:700">SHARED/API: baseApi</text>
|
|
876
|
+
<text class="td" x="180" y="190" style="font-size:11px">base URL, no endpoints, no business words</text>
|
|
877
|
+
|
|
878
|
+
<path class="s" d="M180 206 L180 240"/><path class="s" d="M173 232 L180 242 L187 232"/>
|
|
879
|
+
<path class="s" data-r="110,246,140,50,19"/>
|
|
880
|
+
<text class="t" x="180" y="272">SERVER</text>
|
|
881
|
+
<text class="ts" x="180" y="288">/api</text>
|
|
882
|
+
<text class="ts" x="180" y="320">app/store.ts plugs the pipe in once</text>
|
|
883
|
+
</svg>
|
|
884
|
+
<p class="small">shared knows only “there is a server at <code>/api</code>”. Which URLs exist is business, so each feature adds its own.</p>
|
|
885
|
+
<span class="folio">35</span></article>
|
|
886
|
+
<article class="page" data-ch="6" hidden><span class="chaptag">Chapter 6: Server data per feature</span>
|
|
887
|
+
<h3>Two kinds of data</h3>
|
|
888
|
+
<div class="pubpriv">
|
|
889
|
+
<div class="pub"><h4>Server data</h4>Lives in a database. Product list, saved cart, user profile. Goes in <code>features/x/api/</code> with RTK Query.</div>
|
|
890
|
+
<div><h4>Browser data</h4>Lives only in this tab. Menu open, selected filter, form draft. Goes in a slice in <code>features/x/model/</code>.</div>
|
|
891
|
+
</div>
|
|
892
|
+
<p class="small">Never copy server data into a slice. Then there are two copies, and they will disagree.</p>
|
|
893
|
+
<h4>The empty pipe</h4>
|
|
894
|
+
<pre><span class="c">// shared/api/baseApi.ts</span>
|
|
895
|
+
import { createApi, fetchBaseQuery }
|
|
896
|
+
from '@reduxjs/toolkit/query/react'
|
|
897
|
+
|
|
898
|
+
export const baseApi = createApi({
|
|
899
|
+
reducerPath: 'api',
|
|
900
|
+
baseQuery: fetchBaseQuery({ baseUrl: '/api' }),
|
|
901
|
+
<span class="hl">endpoints: () => ({}),</span>
|
|
902
|
+
})
|
|
903
|
+
|
|
904
|
+
<span class="c">// app/store.ts (add)</span>
|
|
905
|
+
import { baseApi } from '@/shared/api'
|
|
906
|
+
reducer: {
|
|
907
|
+
[baseApi.reducerPath]: baseApi.reducer,
|
|
908
|
+
cart: cartReducer,
|
|
909
|
+
},
|
|
910
|
+
middleware: (getDefault) =>
|
|
911
|
+
getDefault().concat(baseApi.middleware),</pre>
|
|
912
|
+
<p class="small"><code>app → shared</code> is allowed: it still points down.</p>
|
|
913
|
+
<span class="folio">36</span></article>
|
|
914
|
+
<article class="page" data-ch="6" hidden><span class="chaptag">Chapter 6: Server data per feature</span>
|
|
915
|
+
<h3>The feature adds its taps</h3>
|
|
916
|
+
<pre><span class="c">// features/cart/api/cartApi.ts</span>
|
|
917
|
+
import { baseApi } from '@/shared/api'
|
|
918
|
+
import type { CartLine } from '../types'
|
|
919
|
+
|
|
920
|
+
<span class="hl">const api = baseApi.enhanceEndpoints({</span>
|
|
921
|
+
<span class="hl"> addTagTypes: ['Cart'],</span>
|
|
922
|
+
<span class="hl">})</span>
|
|
923
|
+
|
|
924
|
+
export const cartApi = api.injectEndpoints({
|
|
925
|
+
endpoints: (build) => ({
|
|
926
|
+
getCart: build.query<CartLine[], void>({
|
|
927
|
+
query: () => 'cart',
|
|
928
|
+
providesTags: ['Cart'],
|
|
929
|
+
}),
|
|
930
|
+
addToCart: build.mutation<void, CartLine>({
|
|
931
|
+
query: (line) => ({
|
|
932
|
+
url: 'cart', method: 'POST', body: line,
|
|
933
|
+
}),
|
|
934
|
+
invalidatesTags: ['Cart'],
|
|
935
|
+
}),
|
|
936
|
+
}),
|
|
937
|
+
})
|
|
938
|
+
|
|
939
|
+
export const { useGetCartQuery, useAddToCartMutation }
|
|
940
|
+
= cartApi</pre>
|
|
941
|
+
<p class="small">The tag word <code>'Cart'</code> is added here, in the feature. <code>invalidatesTags</code> tells RTK Query: after adding, load the cart again.</p>
|
|
942
|
+
<pre><span class="c">// features/cart/ui/CartPage.tsx</span>
|
|
943
|
+
const { data: lines = [], isLoading } = useGetCartQuery()
|
|
944
|
+
const [addToCart] = useAddToCartMutation()</pre>
|
|
945
|
+
<span class="folio">37</span></article>
|
|
946
|
+
<article class="page" data-ch="6" hidden><span class="chaptag">Chapter 6: Server data per feature</span>
|
|
947
|
+
<h2 class="chap" style="font-size:clamp(36px,4.4vw,48px);margin-top:0">Résumé</h2>
|
|
948
|
+
<div class="resume">
|
|
949
|
+
<p><b>Two kinds of data.</b> Server data → RTK Query. Browser data → slice.</p>
|
|
950
|
+
<p><b>One empty pipe.</b> <code>shared/api/baseApi.ts</code>, no endpoints, no business words.</p>
|
|
951
|
+
<p><b>Feature adds taps.</b> <code>injectEndpoints</code> and its own tag words in <code>features/x/api/</code>.</p>
|
|
952
|
+
<p><b>app plugs it in once.</b> One reducer line, one middleware line.</p>
|
|
953
|
+
</div>
|
|
954
|
+
<div class="check">
|
|
955
|
+
<h3>Checkpoint 6</h3>
|
|
956
|
+
<p style="font-size:15px;margin-bottom:10px">3 small questions. Short answers. Write them in the chat.</p>
|
|
957
|
+
<ol>
|
|
958
|
+
<li>The product list from the database. Where: <b>A</b> or <b>B</b>?<br>A) a slice in <code>features/products/model/</code><br>B) an endpoint in <code>features/products/api/</code></li>
|
|
959
|
+
<li>“Is the filter panel open?” (only in this tab). Where: <b>A</b> or <b>B</b>?<br>A) an RTK Query endpoint<br>B) a slice in <code>features/products/model/</code></li>
|
|
960
|
+
<li>Someone writes <code>tagTypes: ['Cart', 'Product']</code> inside <code>shared/api/baseApi.ts</code>. What is wrong?</li>
|
|
961
|
+
</ol>
|
|
962
|
+
<p class="reply">Reply like this: 1) … 2) … 3) …</p>
|
|
963
|
+
</div>
|
|
964
|
+
<span class="folio">38</span></article>
|
|
965
|
+
<article class="page" data-ch="7" hidden><div style="display:flex;flex-direction:column;justify-content:center;height:100%;min-height:520px">
|
|
966
|
+
<div style="font-family:Oswald,sans-serif;font-weight:700;font-size:clamp(120px,16vw,190px);line-height:.9;color:var(--mustard-deep)">07</div>
|
|
967
|
+
<h2 class="chap" style="margin-top:18px">The shared layer</h2>
|
|
968
|
+
<p style="font-size:19px;margin-top:6px">The toolbox and the two tests to enter it.</p>
|
|
969
|
+
<div class="bar" style="margin-top:28px;max-width:320px;border-color:var(--ink)"><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div></div>
|
|
970
|
+
<p class="small" style="margin-top:8px">Chapter 7 of 10</p>
|
|
971
|
+
</div><span class="folio">39</span></article>
|
|
972
|
+
<article class="page" data-ch="7" hidden><span class="chaptag">Chapter 7: The shared layer</span>
|
|
973
|
+
<h2 class="chap">Chapter 7:</h2>
|
|
974
|
+
<p class="sub">The shared layer</p>
|
|
975
|
+
<p>shared/ is the toolbox. A hammer does not know if it builds a shop or a school. A <code>Button</code> does not know if it adds to a cart or sends a message.</p>
|
|
976
|
+
<p>The danger: shared becomes a junk drawer. Every file that “might be useful later” lands there, and slowly shared starts to know the business.</p>
|
|
977
|
+
<div class="insight"><b>INSIGHT #7</b><span>shared/ holds tools that work in any app. Two tests decide what enters: the copy test and the rule of two.</span></div>
|
|
978
|
+
<h4>Scope of this step</h4>
|
|
979
|
+
<p>The four drawers of shared, the two tests, and how a shared tool gets data without knowing the business.</p>
|
|
980
|
+
<span class="folio">40</span></article>
|
|
981
|
+
<article class="page" data-ch="7" hidden><span class="chaptag">Chapter 7: The shared layer</span>
|
|
982
|
+
<h3>The toolbox</h3>
|
|
983
|
+
<svg class="feat diagram" viewBox="0 0 360 300" role="img" aria-label="Features take tools from shared. shared has four drawers, ui, lib, api and config, each with its own index.ts. Shared never looks up at features.">
|
|
984
|
+
<path class="s" data-r="40,8,280,50,3"/>
|
|
985
|
+
<text class="t" x="180" y="32">FEATURES</text>
|
|
986
|
+
<text class="ts" x="180" y="48">take tools</text>
|
|
987
|
+
<path class="s" d="M180 62 L180 92"/><path class="s" d="M173 84 L180 94 L187 84"/>
|
|
988
|
+
|
|
989
|
+
<path class="s" data-r="10,98,340,170,7"/>
|
|
990
|
+
<text class="tl" x="24" y="120">SHARED</text>
|
|
991
|
+
<path class="s" data-r="24,132,150,58,11"/>
|
|
992
|
+
<text class="t" x="99" y="158">UI</text>
|
|
993
|
+
<text class="ts" x="99" y="176">Button, Modal, Card</text>
|
|
994
|
+
<path class="s" data-r="186,132,150,58,13"/>
|
|
995
|
+
<text class="t" x="261" y="158">LIB</text>
|
|
996
|
+
<text class="ts" x="261" y="176">formatMoney, debounce</text>
|
|
997
|
+
<path class="s" data-r="24,198,150,58,17"/>
|
|
998
|
+
<text class="t" x="99" y="224">API</text>
|
|
999
|
+
<text class="ts" x="99" y="242">baseApi (step 6)</text>
|
|
1000
|
+
<path class="s" data-r="186,198,150,58,19"/>
|
|
1001
|
+
<text class="t" x="261" y="224">CONFIG</text>
|
|
1002
|
+
<text class="ts" x="261" y="242">env values</text>
|
|
1003
|
+
<rect class="door" x="134" y="126" width="36" height="12"/><text class="td" x="152" y="135" style="font-size:7.5px">index</text>
|
|
1004
|
+
<rect class="door" x="296" y="126" width="36" height="12"/><text class="td" x="314" y="135" style="font-size:7.5px">index</text>
|
|
1005
|
+
<rect class="door" x="134" y="192" width="36" height="12"/><text class="td" x="152" y="201" style="font-size:7.5px">index</text>
|
|
1006
|
+
<rect class="door" x="296" y="192" width="36" height="12"/><text class="td" x="314" y="201" style="font-size:7.5px">index</text>
|
|
1007
|
+
|
|
1008
|
+
<text class="ts" x="180" y="292">each drawer has a door: import from '@/shared/ui'</text>
|
|
1009
|
+
</svg>
|
|
1010
|
+
<pre><span class="c">// shared/config/env.ts</span>
|
|
1011
|
+
export const env = {
|
|
1012
|
+
apiUrl: import.meta.env.VITE_API_URL ?? '/api',
|
|
1013
|
+
}</pre>
|
|
1014
|
+
<p class="small">Vite reads <code>VITE_</code> values from <code>.env</code> files. One file reads them, so the rest of the code uses <code>env.apiUrl</code>.</p>
|
|
1015
|
+
<span class="folio">41</span></article>
|
|
1016
|
+
<article class="page" data-ch="7" hidden><span class="chaptag">Chapter 7: The shared layer</span>
|
|
1017
|
+
<h3>Two tests to enter shared</h3>
|
|
1018
|
+
<ul class="rules" style="counter-reset:r">
|
|
1019
|
+
<li><b>Copy test.</b> Copy the file into a totally different app (a blog, a game). Does it work with no change? Then it may be shared.</li>
|
|
1020
|
+
<li><b>Rule of two.</b> Move a file to shared only when a <i>second</i> feature needs it. Until then, it stays in its feature.</li>
|
|
1021
|
+
</ul>
|
|
1022
|
+
<pre><span class="c">// passes the copy test</span>
|
|
1023
|
+
export function formatMoney(n: number, cur: string)
|
|
1024
|
+
<span class="ok">✔ shared/lib</span>
|
|
1025
|
+
|
|
1026
|
+
<span class="c">// fails: knows CartLine</span>
|
|
1027
|
+
export function calcCartTotal(lines: CartLine[])
|
|
1028
|
+
<span class="bad">✘ stays in features/cart/lib</span></pre>
|
|
1029
|
+
<p class="small">Why the rule of two: moving a file early is a guess. Moving it later is a fact. Moving it out of a feature is easy; pulling business out of shared is hard.</p>
|
|
1030
|
+
<span class="folio">42</span></article>
|
|
1031
|
+
<article class="page" data-ch="7" hidden><span class="chaptag">Chapter 7: The shared layer</span>
|
|
1032
|
+
<h3>Shared tools get data by props</h3>
|
|
1033
|
+
<p>A shared tool never reads the store or a feature. The feature gives it everything through props.</p>
|
|
1034
|
+
<pre><span class="c">// shared/ui/Card.tsx: knows nothing</span>
|
|
1035
|
+
type Props = {
|
|
1036
|
+
title: string
|
|
1037
|
+
image: string
|
|
1038
|
+
children?: React.ReactNode
|
|
1039
|
+
}
|
|
1040
|
+
export function Card({ title, image, children }: Props) {
|
|
1041
|
+
return (
|
|
1042
|
+
<div className="card">
|
|
1043
|
+
<img src={image} alt="" />
|
|
1044
|
+
<h3>{title}</h3>
|
|
1045
|
+
{children}
|
|
1046
|
+
</div>
|
|
1047
|
+
)
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
<span class="c">// features/products/ui/ProductCard.tsx</span>
|
|
1051
|
+
import { Card } from '@/shared/ui'
|
|
1052
|
+
import { formatMoney } from '@/shared/lib'
|
|
1053
|
+
import { AddToCartButton } from '@/features/cart'
|
|
1054
|
+
|
|
1055
|
+
export function ProductCard({ p }: { p: Product }) {
|
|
1056
|
+
return (
|
|
1057
|
+
<span class="hl"><Card title={p.name} image={p.image}></span>
|
|
1058
|
+
{formatMoney(p.price, 'MAD')}
|
|
1059
|
+
<AddToCartButton productId={p.id} />
|
|
1060
|
+
</Card>
|
|
1061
|
+
)
|
|
1062
|
+
}</pre>
|
|
1063
|
+
<p class="small">This is the fix for the broken <code>shared/ui/ProductCard.tsx</code> from checkpoint 1: the generic box goes to shared, the product version stays in the feature.</p>
|
|
1064
|
+
<span class="folio">43</span></article>
|
|
1065
|
+
<article class="page" data-ch="7" hidden><span class="chaptag">Chapter 7: The shared layer</span>
|
|
1066
|
+
<h2 class="chap" style="font-size:clamp(36px,4.4vw,48px);margin-top:0">Résumé</h2>
|
|
1067
|
+
<div class="resume">
|
|
1068
|
+
<p><b>Four drawers.</b> ui, lib, api, config. Each with its own <code>index.ts</code>.</p>
|
|
1069
|
+
<p><b>Copy test.</b> Works in any other app with no change.</p>
|
|
1070
|
+
<p><b>Rule of two.</b> Move to shared only when a second feature needs it.</p>
|
|
1071
|
+
<p><b>Props, not store.</b> Shared tools receive data, they never fetch or select it.</p>
|
|
1072
|
+
</div>
|
|
1073
|
+
<div class="check">
|
|
1074
|
+
<h3>Checkpoint 7</h3>
|
|
1075
|
+
<p style="font-size:15px;margin-bottom:10px">3 small questions. Short answers. Write them in the chat.</p>
|
|
1076
|
+
<ol>
|
|
1077
|
+
<li>Which passes the copy test: <b>A</b> or <b>B</b>?<br>A) <code>debounce(fn, ms)</code><br>B) <code>isProductInStock(p: Product)</code></li>
|
|
1078
|
+
<li>Only the cart feature uses <code>DateLabel.tsx</code>. Move it to shared now? Why?</li>
|
|
1079
|
+
<li>A shared <code>Modal</code> must close when the user logs out. Should <code>Modal</code> import <code>selectUser</code> from auth? If not, how does it know?</li>
|
|
1080
|
+
</ol>
|
|
1081
|
+
<p class="reply">Reply like this: 1) … 2) … 3) …</p>
|
|
1082
|
+
</div>
|
|
1083
|
+
<span class="folio">44</span></article>
|
|
1084
|
+
<article class="page" data-ch="8" hidden><div style="display:flex;flex-direction:column;justify-content:center;height:100%;min-height:520px">
|
|
1085
|
+
<div style="font-family:Oswald,sans-serif;font-weight:700;font-size:clamp(120px,16vw,190px);line-height:.9;color:var(--mustard-deep)">08</div>
|
|
1086
|
+
<h2 class="chap" style="margin-top:18px">Enforcing boundaries</h2>
|
|
1087
|
+
<p style="font-size:19px;margin-top:6px">Rules written into the linter.</p>
|
|
1088
|
+
<div class="bar" style="margin-top:28px;max-width:320px;border-color:var(--ink)"><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg" style="border-color:var(--ink);"></div><div class="seg" style="border-color:var(--ink);"></div></div>
|
|
1089
|
+
<p class="small" style="margin-top:8px">Chapter 8 of 10</p>
|
|
1090
|
+
</div><span class="folio">45</span></article>
|
|
1091
|
+
<article class="page" data-ch="8" hidden><span class="chaptag">Chapter 8: Enforcing boundaries</span>
|
|
1092
|
+
<h2 class="chap">Chapter 8:</h2>
|
|
1093
|
+
<p class="sub">Enforcing boundaries</p>
|
|
1094
|
+
<p>Rules A, B and C live in your head. Heads forget. AI tools never knew them. One tired evening, one long prompt, and the rules are broken without anyone noticing.</p>
|
|
1095
|
+
<p>The fix: write the rules into the linter. Then a broken rule is a red error, with the rule name in the message.</p>
|
|
1096
|
+
<div class="insight"><b>INSIGHT #8</b><span>A rule only on paper will be broken. A rule in the linter fails the build.</span></div>
|
|
1097
|
+
<h4>Scope of this step</h4>
|
|
1098
|
+
<p>The <code>@/</code> alias, the <code>lint</code> block in <code>vite.config.ts</code>, and one command: <code>vp check</code>.</p>
|
|
1099
|
+
<span class="folio">46</span></article>
|
|
1100
|
+
<article class="page" data-ch="8" hidden><span class="chaptag">Chapter 8: Enforcing boundaries</span>
|
|
1101
|
+
<h3>Three guards</h3>
|
|
1102
|
+
<svg class="feat diagram" viewBox="0 0 360 300" role="img" aria-label="vp check runs three guards: guard A blocks upward imports, guard B blocks deep imports into a feature, guard C blocks circles.">
|
|
1103
|
+
<path class="s" data-r="100,8,160,46,3"/>
|
|
1104
|
+
<text class="t" x="180" y="30">VP CHECK</text>
|
|
1105
|
+
<text class="ts" x="180" y="46">format + lint + types</text>
|
|
1106
|
+
<path class="s" d="M140 58 L66 98"/><path class="s" d="M74 90 L64 100 L78 101"/>
|
|
1107
|
+
<path class="s" d="M180 58 L180 96"/><path class="s" d="M173 88 L180 98 L187 88"/>
|
|
1108
|
+
<path class="s" d="M220 58 L294 98"/><path class="s" d="M282 101 L296 100 L286 90"/>
|
|
1109
|
+
|
|
1110
|
+
<path class="s" data-r="10,104,104,120,7"/>
|
|
1111
|
+
<rect class="door" x="40" y="96" width="44" height="18"/><text class="td" x="62" y="109" style="font-size:11px;font-weight:700">A</text>
|
|
1112
|
+
<text class="t" x="62" y="142" style="font-size:14px">UP</text>
|
|
1113
|
+
<text class="ts" x="62" y="162">shared → features</text>
|
|
1114
|
+
<text class="ts" x="62" y="178">features → app</text>
|
|
1115
|
+
<text class="badt" x="62" y="206" text-anchor="middle">blocked</text>
|
|
1116
|
+
|
|
1117
|
+
<path class="s" data-r="128,104,104,120,11"/>
|
|
1118
|
+
<rect class="door" x="158" y="96" width="44" height="18"/><text class="td" x="180" y="109" style="font-size:11px;font-weight:700">B</text>
|
|
1119
|
+
<text class="t" x="180" y="142" style="font-size:14px">DEEP</text>
|
|
1120
|
+
<text class="ts" x="180" y="162">@/features/x/ui/…</text>
|
|
1121
|
+
<text class="ts" x="180" y="178">../../ out of a room</text>
|
|
1122
|
+
<text class="badt" x="180" y="206" text-anchor="middle">blocked</text>
|
|
1123
|
+
|
|
1124
|
+
<path class="s" data-r="246,104,104,120,13"/>
|
|
1125
|
+
<rect class="door" x="276" y="96" width="44" height="18"/><text class="td" x="298" y="109" style="font-size:11px;font-weight:700">C</text>
|
|
1126
|
+
<text class="t" x="298" y="142" style="font-size:14px">CIRCLE</text>
|
|
1127
|
+
<text class="ts" x="298" y="162">cart → auth → cart</text>
|
|
1128
|
+
<text class="badt" x="298" y="206" text-anchor="middle">blocked</text>
|
|
1129
|
+
|
|
1130
|
+
<path class="s" data-r="10,240,340,52,17"/>
|
|
1131
|
+
<text class="ts" x="180" y="262" style="font-size:12px">error message names the rule:</text>
|
|
1132
|
+
<text class="ts" x="180" y="280" style="font-size:12px;fill:var(--bad)">“Rule B: import a feature only from its door.”</text>
|
|
1133
|
+
</svg>
|
|
1134
|
+
<p class="small">The new guard on <code>../../</code>: inside a feature, use short relative paths. To leave the folder, use <code>@/</code>. Then guard B can see it.</p>
|
|
1135
|
+
<span class="folio">47</span></article>
|
|
1136
|
+
<article class="page" data-ch="8" hidden><span class="chaptag">Chapter 8: Enforcing boundaries</span>
|
|
1137
|
+
<h3>The @/ alias, in two files</h3>
|
|
1138
|
+
<p><code>@/</code> means <code>src/</code>. Two different programs read your imports, so both must know it.</p>
|
|
1139
|
+
<pre><span class="c">// tsconfig.json: for TypeScript and the editor</span>
|
|
1140
|
+
{
|
|
1141
|
+
"compilerOptions": {
|
|
1142
|
+
"paths": { "@/*": ["./src/*"] }
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
<span class="c">// vite.config.ts: for Vite (dev + build)</span>
|
|
1147
|
+
import { fileURLToPath } from 'node:url'
|
|
1148
|
+
import { defineConfig } from 'vite-plus'
|
|
1149
|
+
import react from '@vitejs/plugin-react'
|
|
1150
|
+
|
|
1151
|
+
export default defineConfig({
|
|
1152
|
+
plugins: [react()],
|
|
1153
|
+
resolve: {
|
|
1154
|
+
alias: {
|
|
1155
|
+
'@': fileURLToPath(new URL('./src', import.meta.url)),
|
|
1156
|
+
},
|
|
1157
|
+
},
|
|
1158
|
+
<span class="c">// lint: { … } on the next page</span>
|
|
1159
|
+
})</pre>
|
|
1160
|
+
<ul class="legend">
|
|
1161
|
+
<li>Missing in <code>tsconfig.json</code>: red lines in the editor, app still runs.</li>
|
|
1162
|
+
<li>Missing in <code>vite.config.ts</code>: editor is happy, app fails to start.</li>
|
|
1163
|
+
</ul>
|
|
1164
|
+
<span class="folio">48</span></article>
|
|
1165
|
+
<article class="page" data-ch="8" hidden><span class="chaptag">Chapter 8: Enforcing boundaries</span>
|
|
1166
|
+
<h3>The lint block</h3>
|
|
1167
|
+
<pre><span class="c">// vite.config.ts (continued)</span>
|
|
1168
|
+
const door = {
|
|
1169
|
+
regex: '^@/features/[^/]+/.+',
|
|
1170
|
+
message: 'Rule B: import a feature only from its door.',
|
|
1171
|
+
}
|
|
1172
|
+
const deep = {
|
|
1173
|
+
regex: '^(\\.\\./){2,}',
|
|
1174
|
+
message: 'Too deep: leave the folder with @/.',
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
lint: {
|
|
1178
|
+
plugins: ['typescript', 'react', 'import'],
|
|
1179
|
+
rules: {
|
|
1180
|
+
<span class="hl">'import/no-cycle': 'error',</span> <span class="c">// Rule C</span>
|
|
1181
|
+
'no-restricted-imports':
|
|
1182
|
+
['error', { patterns: [door, deep] }],
|
|
1183
|
+
},
|
|
1184
|
+
overrides: [
|
|
1185
|
+
{
|
|
1186
|
+
files: ['src/features/**'],
|
|
1187
|
+
rules: { 'no-restricted-imports': ['error', {
|
|
1188
|
+
patterns: [door, deep, {
|
|
1189
|
+
<span class="hl">regex: '^@/app(/|$)',</span>
|
|
1190
|
+
message: 'Rule A: features never import app.',
|
|
1191
|
+
}],
|
|
1192
|
+
}] },
|
|
1193
|
+
},
|
|
1194
|
+
{
|
|
1195
|
+
files: ['src/shared/**'],
|
|
1196
|
+
rules: { 'no-restricted-imports': ['error', {
|
|
1197
|
+
patterns: [deep, {
|
|
1198
|
+
<span class="hl">regex: '^@/(features|app)(/|$)',</span>
|
|
1199
|
+
message: 'Rule A: shared knows no features.',
|
|
1200
|
+
}],
|
|
1201
|
+
}] },
|
|
1202
|
+
},
|
|
1203
|
+
],
|
|
1204
|
+
},</pre>
|
|
1205
|
+
<p class="small">An override <i>replaces</i> the rule for its files, so <code>door</code> and <code>deep</code> are repeated inside. Type imports are checked too, same as step 5. Keep this config a plain object: the linter cannot read conditional or async config.</p>
|
|
1206
|
+
<span class="folio">49</span></article>
|
|
1207
|
+
<article class="page" data-ch="8" hidden><span class="chaptag">Chapter 8: Enforcing boundaries</span>
|
|
1208
|
+
<h2 class="chap" style="font-size:clamp(36px,4.4vw,48px);margin-top:0">Résumé</h2>
|
|
1209
|
+
<div class="resume">
|
|
1210
|
+
<p><b>Alias in two files.</b> <code>tsconfig.json</code> for TypeScript, <code>vite.config.ts</code> for Vite.</p>
|
|
1211
|
+
<p><b>Rules A, B in lint.</b> <code>no-restricted-imports</code> with one override per zone.</p>
|
|
1212
|
+
<p><b>Rule C in lint.</b> <code>import/no-cycle</code>.</p>
|
|
1213
|
+
<p><b>One command.</b> <code>vp check</code> after every change, and after every AI answer.</p>
|
|
1214
|
+
</div>
|
|
1215
|
+
<div class="check">
|
|
1216
|
+
<h3>Checkpoint 8</h3>
|
|
1217
|
+
<p style="font-size:15px;margin-bottom:10px">3 small questions. Short answers. Write them in the chat.</p>
|
|
1218
|
+
<ol>
|
|
1219
|
+
<li>In <code>features/cart</code>: <code>import { authSlice } from '@/features/auth/model/authSlice'</code>. Which error message appears?</li>
|
|
1220
|
+
<li>The alias is only in <code>vite.config.ts</code>. What happens: <b>A</b> or <b>B</b>?<br>A) App runs, editor shows red lines<br>B) App fails, editor is happy</li>
|
|
1221
|
+
<li>AI writes <code>shared/ui/Header.tsx</code> with <code>import { CartBadge } from '@/features/cart'</code>. What does <code>vp check</code> say, and where should the header go?</li>
|
|
1222
|
+
</ol>
|
|
1223
|
+
<p class="reply">Reply like this: 1) … 2) … 3) …</p>
|
|
1224
|
+
</div>
|
|
1225
|
+
<span class="folio">50</span></article>
|
|
1226
|
+
<article class="page" data-ch="9" hidden><div style="display:flex;flex-direction:column;justify-content:center;height:100%;min-height:520px">
|
|
1227
|
+
<div style="font-family:Oswald,sans-serif;font-weight:700;font-size:clamp(120px,16vw,190px);line-height:.9;color:var(--mustard-deep)">09</div>
|
|
1228
|
+
<h2 class="chap" style="margin-top:18px">One prompt, one module</h2>
|
|
1229
|
+
<p style="font-size:19px;margin-top:6px">Letting AI work inside a small box.</p>
|
|
1230
|
+
<div class="bar" style="margin-top:28px;max-width:320px;border-color:var(--ink)"><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg" style="border-color:var(--ink);"></div></div>
|
|
1231
|
+
<p class="small" style="margin-top:8px">Chapter 9 of 10</p>
|
|
1232
|
+
</div><span class="folio">51</span></article>
|
|
1233
|
+
<article class="page" data-ch="9" hidden><span class="chaptag">Chapter 9: One prompt, one module</span>
|
|
1234
|
+
<h2 class="chap">Chapter 9:</h2>
|
|
1235
|
+
<p class="sub">One prompt, one module</p>
|
|
1236
|
+
<p>AI writes fast and writes everywhere. Ask for “a wishlist feature”, and it may edit ten folders, invent a new helper in shared, and import from three features.</p>
|
|
1237
|
+
<p>The answer is not a smarter AI. It is a smaller box. Tell it exactly which room to work in, which doors it may use, and check the result with tools.</p>
|
|
1238
|
+
<div class="insight"><b>INSIGHT #9</b><span>Write the door yourself. Let AI fill one room at a time. Check every answer with vp check and git diff.</span></div>
|
|
1239
|
+
<h4>Scope of this step</h4>
|
|
1240
|
+
<p>The 4-step loop, a rules file for the AI, a prompt template, and the diff test.</p>
|
|
1241
|
+
<span class="folio">52</span></article>
|
|
1242
|
+
<article class="page" data-ch="9" hidden><span class="chaptag">Chapter 9: One prompt, one module</span>
|
|
1243
|
+
<h3>The loop</h3>
|
|
1244
|
+
<svg class="feat diagram" viewBox="0 0 360 320" role="img" aria-label="A loop of four boxes: write the door, prompt one room, vp check, review the diff, then back to prompt the next room.">
|
|
1245
|
+
<path class="s" data-r="70,20,104,104,3"/>
|
|
1246
|
+
<text class="t" x="122" y="62">1. WRITE</text>
|
|
1247
|
+
<text class="t" x="122" y="82">THE DOOR</text>
|
|
1248
|
+
<text class="ts" x="122" y="104">you, not AI</text>
|
|
1249
|
+
<path class="s" data-r="186,20,104,104,7"/>
|
|
1250
|
+
<text class="t" x="238" y="62">2. PROMPT</text>
|
|
1251
|
+
<text class="t" x="238" y="82">ONE ROOM</text>
|
|
1252
|
+
<text class="ts" x="238" y="104">ui, model or api</text>
|
|
1253
|
+
<path class="s" data-r="186,136,104,104,11"/>
|
|
1254
|
+
<text class="t" x="238" y="180">3. VP</text>
|
|
1255
|
+
<text class="t" x="238" y="200">CHECK</text>
|
|
1256
|
+
<text class="ts" x="238" y="222">must be green</text>
|
|
1257
|
+
<path class="s" data-r="70,136,104,104,13"/>
|
|
1258
|
+
<text class="t" x="122" y="180">4. REVIEW</text>
|
|
1259
|
+
<text class="t" x="122" y="200">THE DIFF</text>
|
|
1260
|
+
<text class="ts" x="122" y="222">git diff --stat</text>
|
|
1261
|
+
|
|
1262
|
+
<path class="s" d="M176 66 L184 66"/><path class="s" d="M178 60 L186 66 L178 72"/>
|
|
1263
|
+
<path class="s" d="M238 126 L238 134"/><path class="s" d="M232 128 L238 136 L244 128"/>
|
|
1264
|
+
<path class="s" d="M184 188 L176 188"/><path class="s" d="M182 182 L174 188 L182 194"/>
|
|
1265
|
+
<path class="s" d="M122 244 L122 280 L312 280 L312 72 L296 72"/><path class="s" d="M302 66 L294 72 L302 78"/>
|
|
1266
|
+
<text class="ts" x="216" y="300">next room</text>
|
|
1267
|
+
</svg>
|
|
1268
|
+
<p class="small">Red in step 3 or a surprise in step 4: send the error back to the AI, or throw the answer away (<code>git restore .</code>) and prompt again with a smaller box.</p>
|
|
1269
|
+
<span class="folio">53</span></article>
|
|
1270
|
+
<article class="page" data-ch="9" hidden><span class="chaptag">Chapter 9: One prompt, one module</span>
|
|
1271
|
+
<h3>A rules file for the AI</h3>
|
|
1272
|
+
<p>Most AI coding tools read a rules file at the root of the project (for example <code>AGENTS.md</code> or <code>CLAUDE.md</code>). Write the house rules once there.</p>
|
|
1273
|
+
<pre><span class="c"># AGENTS.md</span>
|
|
1274
|
+
## Architecture
|
|
1275
|
+
- src/app: wiring only. No business logic.
|
|
1276
|
+
- src/features/<name>: one business feature.
|
|
1277
|
+
Rooms: ui, model, api, lib, types.ts.
|
|
1278
|
+
Door: index.ts. Only the door is public.
|
|
1279
|
+
- src/shared: no business words.
|
|
1280
|
+
Copy test + rule of two.
|
|
1281
|
+
|
|
1282
|
+
## Imports
|
|
1283
|
+
- Down only: app → features → shared.
|
|
1284
|
+
- Other feature: '@/features/<name>' only.
|
|
1285
|
+
- No circles.
|
|
1286
|
+
- Inside a feature: relative paths.
|
|
1287
|
+
|
|
1288
|
+
## Data
|
|
1289
|
+
- Server data: RTK Query, injectEndpoints
|
|
1290
|
+
in features/<name>/api.
|
|
1291
|
+
- Browser data: slice in features/<name>/model.
|
|
1292
|
+
|
|
1293
|
+
## Done means
|
|
1294
|
+
- vp check is green.</pre>
|
|
1295
|
+
<span class="folio">54</span></article>
|
|
1296
|
+
<article class="page" data-ch="9" hidden><span class="chaptag">Chapter 9: One prompt, one module</span>
|
|
1297
|
+
<h3>The prompt template</h3>
|
|
1298
|
+
<pre>Task: build the wishlist <span class="hl">model</span>
|
|
1299
|
+
(slice + selectors).
|
|
1300
|
+
|
|
1301
|
+
Work ONLY in:
|
|
1302
|
+
src/features/wishlist/model/
|
|
1303
|
+
|
|
1304
|
+
You may import from:
|
|
1305
|
+
- inside src/features/wishlist/
|
|
1306
|
+
- @/shared/*
|
|
1307
|
+
- @/features/auth (door only)
|
|
1308
|
+
|
|
1309
|
+
Allowed outside the folder:
|
|
1310
|
+
- one line in src/app/store.ts
|
|
1311
|
+
|
|
1312
|
+
Do NOT:
|
|
1313
|
+
- change src/features/wishlist/index.ts
|
|
1314
|
+
- create files in src/shared/
|
|
1315
|
+
- import from @/app or inside other features
|
|
1316
|
+
|
|
1317
|
+
Answer: list of files first, then code.</pre>
|
|
1318
|
+
<h4>The diff test</h4>
|
|
1319
|
+
<p class="small">After the answer, run <code>git diff --stat</code>. Compare the changed files with the “Work ONLY in” and “Allowed outside” lines. Any other file is a surprise. Surprises are rejected or questioned, never accepted quietly.</p>
|
|
1320
|
+
<p class="small">Work on a git branch per feature. Then deleting a bad attempt is one command.</p>
|
|
1321
|
+
<span class="folio">55</span></article>
|
|
1322
|
+
<article class="page" data-ch="9" hidden><span class="chaptag">Chapter 9: One prompt, one module</span>
|
|
1323
|
+
<h2 class="chap" style="font-size:clamp(36px,4.4vw,48px);margin-top:0">Résumé</h2>
|
|
1324
|
+
<div class="resume">
|
|
1325
|
+
<p><b>Door first.</b> You write <code>index.ts</code> and <code>types.ts</code>. AI fills rooms.</p>
|
|
1326
|
+
<p><b>One room per prompt.</b> Small box, small answer, easy review.</p>
|
|
1327
|
+
<p><b>Rules file.</b> <code>AGENTS.md</code> holds the house rules for every prompt.</p>
|
|
1328
|
+
<p><b>Two checks.</b> <code>vp check</code> green, then <code>git diff --stat</code> with no surprises.</p>
|
|
1329
|
+
</div>
|
|
1330
|
+
<div class="check">
|
|
1331
|
+
<h3>Checkpoint 9</h3>
|
|
1332
|
+
<p style="font-size:15px;margin-bottom:10px">3 small questions. Short answers. Write them in the chat.</p>
|
|
1333
|
+
<ol>
|
|
1334
|
+
<li>Put the loop in order: A) <code>vp check</code> B) write the door C) prompt one room D) review the diff</li>
|
|
1335
|
+
<li>The prompt is the template on page 5. <code>git diff --stat</code> shows: <code>wishlist/model/slice.ts</code>, <code>wishlist/model/selectors.ts</code>, <code>shared/ui/HeartIcon.tsx</code>, <code>app/store.ts</code>. Which file is the surprise?</li>
|
|
1336
|
+
<li>AI wrote 600 lines for the whole wishlist (ui + model + api) in one answer. What is the better way?</li>
|
|
1337
|
+
</ol>
|
|
1338
|
+
<p class="reply">Reply like this: 1) … 2) … 3) …</p>
|
|
1339
|
+
</div>
|
|
1340
|
+
<span class="folio">56</span></article>
|
|
1341
|
+
<article class="page" data-ch="10" hidden><div style="display:flex;flex-direction:column;justify-content:center;height:100%;min-height:520px">
|
|
1342
|
+
<div style="font-family:Oswald,sans-serif;font-weight:700;font-size:clamp(120px,16vw,190px);line-height:.9;color:var(--mustard-deep)">10</div>
|
|
1343
|
+
<h2 class="chap" style="margin-top:18px">Tests per module</h2>
|
|
1344
|
+
<p style="font-size:19px;margin-top:6px">Proving behavior, room by room.</p>
|
|
1345
|
+
<div class="bar" style="margin-top:28px;max-width:320px;border-color:var(--ink)"><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div><div class="seg done" style="border-color:var(--ink);background:var(--ink)"></div></div>
|
|
1346
|
+
<p class="small" style="margin-top:8px">Chapter 10 of 10</p>
|
|
1347
|
+
</div><span class="folio">57</span></article>
|
|
1348
|
+
<article class="page" data-ch="10" hidden><span class="chaptag">Chapter 10: Tests per module</span>
|
|
1349
|
+
<h2 class="chap">Chapter 10:</h2>
|
|
1350
|
+
<p class="sub">Tests per module</p>
|
|
1351
|
+
<p><code>vp check</code> proves the code is in the right place. It does not prove the code is <i>correct</i>. A total of 25 can be wrong and still pass every lint rule.</p>
|
|
1352
|
+
<p>Tests prove behavior. And in this house, tests follow the same idea as everything else: they live in the room they test.</p>
|
|
1353
|
+
<div class="insight"><b>INSIGHT #10</b><span>A test lives next to the file it tests. Delete the feature, and its tests go with it.</span></div>
|
|
1354
|
+
<h4>Scope of this step</h4>
|
|
1355
|
+
<p>Where tests live, the <code>test</code> block, and the two easiest rooms to test: <code>lib</code> and <code>model</code>.</p>
|
|
1356
|
+
<span class="folio">58</span></article>
|
|
1357
|
+
<article class="page" data-ch="10" hidden><span class="chaptag">Chapter 10: Tests per module</span>
|
|
1358
|
+
<h3>Tests sit in the room</h3>
|
|
1359
|
+
<svg class="feat diagram" viewBox="0 0 360 300" role="img" aria-label="Inside features/cart, each room holds its own test files: lib has many easy tests, model has reducer tests, ui has a few tests later.">
|
|
1360
|
+
<path class="s" data-r="10,8,340,212,5"/>
|
|
1361
|
+
<text class="tl" x="24" y="32">FEATURES / CART</text>
|
|
1362
|
+
<path class="s" data-r="24,48,100,150,9"/>
|
|
1363
|
+
<text class="t" x="74" y="76">LIB</text>
|
|
1364
|
+
<rect class="door" x="36" y="92" width="76" height="16"/><text class="td" x="74" y="104">test</text>
|
|
1365
|
+
<rect class="door" x="36" y="114" width="76" height="16"/><text class="td" x="74" y="126">test</text>
|
|
1366
|
+
<rect class="door" x="36" y="136" width="76" height="16"/><text class="td" x="74" y="148">test</text>
|
|
1367
|
+
<text class="ts" x="74" y="182">easiest, start here</text>
|
|
1368
|
+
|
|
1369
|
+
<path class="s" data-r="130,48,100,150,13"/>
|
|
1370
|
+
<text class="t" x="180" y="76">MODEL</text>
|
|
1371
|
+
<rect class="door" x="142" y="92" width="76" height="16"/><text class="td" x="180" y="104">test</text>
|
|
1372
|
+
<rect class="door" x="142" y="114" width="76" height="16"/><text class="td" x="180" y="126">test</text>
|
|
1373
|
+
<text class="ts" x="180" y="182">no React needed</text>
|
|
1374
|
+
|
|
1375
|
+
<path class="s" data-r="236,48,100,150,17"/>
|
|
1376
|
+
<text class="t" x="286" y="76">UI</text>
|
|
1377
|
+
<rect class="door" x="248" y="92" width="76" height="16" style="opacity:.45"/><text class="td" x="286" y="104">later</text>
|
|
1378
|
+
<text class="ts" x="286" y="182">needs a renderer</text>
|
|
1379
|
+
|
|
1380
|
+
<text class="ts" x="180" y="250" style="font-size:12px">calcTotal.ts → calcTotal.test.ts</text>
|
|
1381
|
+
<text class="ts" x="180" y="270" style="font-size:12px">cartSlice.ts → cartSlice.test.ts</text>
|
|
1382
|
+
<text class="ts" x="180" y="290">same folder, same name + .test</text>
|
|
1383
|
+
</svg>
|
|
1384
|
+
<p class="small">Test files are code inside the feature, so the lint guards from step 8 apply to them too.</p>
|
|
1385
|
+
<span class="folio">59</span></article>
|
|
1386
|
+
<article class="page" data-ch="10" hidden><span class="chaptag">Chapter 10: Tests per module</span>
|
|
1387
|
+
<h3>Config and a lib test</h3>
|
|
1388
|
+
<pre><span class="c">// vite.config.ts (add)</span>
|
|
1389
|
+
test: {
|
|
1390
|
+
include: ['src/**/*.test.{ts,tsx}'],
|
|
1391
|
+
},
|
|
1392
|
+
|
|
1393
|
+
<span class="c">// features/cart/lib/calcTotal.ts</span>
|
|
1394
|
+
export function calcTotal(lines: CartLine[]) {
|
|
1395
|
+
return lines.reduce(
|
|
1396
|
+
(sum, l) => sum + l.price * l.qty, 0)
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
<span class="c">// features/cart/lib/calcTotal.test.ts</span>
|
|
1400
|
+
import { describe, expect, it } from 'vite-plus/test'
|
|
1401
|
+
import { calcTotal } from './calcTotal'
|
|
1402
|
+
|
|
1403
|
+
describe('calcTotal', () => {
|
|
1404
|
+
it('adds price × qty', () => {
|
|
1405
|
+
expect(calcTotal([
|
|
1406
|
+
{ id: 'a', price: 10, qty: 2 },
|
|
1407
|
+
{ id: 'b', price: 5, qty: 1 },
|
|
1408
|
+
])).toBe(25)
|
|
1409
|
+
})
|
|
1410
|
+
it('is 0 for an empty cart', () => {
|
|
1411
|
+
expect(calcTotal([])).toBe(0)
|
|
1412
|
+
})
|
|
1413
|
+
})</pre>
|
|
1414
|
+
<p class="small">Run once: <code>vp test</code>. Keep running while you work: <code>vp test watch</code>. With Vite+, test helpers come from <code>vite-plus/test</code>, so no separate Vitest install.</p>
|
|
1415
|
+
<span class="folio">60</span></article>
|
|
1416
|
+
<article class="page" data-ch="10" hidden><span class="chaptag">Chapter 10: Tests per module</span>
|
|
1417
|
+
<h3>A model test</h3>
|
|
1418
|
+
<p>A reducer is a plain function: old state + action → new state. Call it directly. No React, no store, no browser.</p>
|
|
1419
|
+
<pre><span class="c">// features/cart/model/cartSlice.test.ts</span>
|
|
1420
|
+
import { describe, expect, it } from 'vite-plus/test'
|
|
1421
|
+
import { cartReducer, added } from './cartSlice'
|
|
1422
|
+
import { loggedOut } from '@/features/auth'
|
|
1423
|
+
|
|
1424
|
+
const line = { id: 'a', price: 10, qty: 1 }
|
|
1425
|
+
|
|
1426
|
+
describe('cartSlice', () => {
|
|
1427
|
+
it('adds a line', () => {
|
|
1428
|
+
const s = cartReducer(undefined, added(line))
|
|
1429
|
+
expect(s.lines).toEqual([line])
|
|
1430
|
+
})
|
|
1431
|
+
it('empties on logout', () => {
|
|
1432
|
+
const s = cartReducer({ lines: [line] }, loggedOut())
|
|
1433
|
+
expect(s.lines).toEqual([])
|
|
1434
|
+
})
|
|
1435
|
+
})</pre>
|
|
1436
|
+
<p class="small"><code>undefined</code> as state means “start from <code>initialState</code>”. The second test checks the link between two features from step 5.</p>
|
|
1437
|
+
<span class="folio">61</span></article>
|
|
1438
|
+
<article class="page" data-ch="10" hidden><span class="chaptag">Chapter 10: Tests per module</span>
|
|
1439
|
+
<h3>The full loop</h3>
|
|
1440
|
+
<p>Step 9’s loop gets one more check. A room is done only when both are green.</p>
|
|
1441
|
+
<ul class="rules" style="counter-reset:r">
|
|
1442
|
+
<li><b>vp check</b> is green: right place, right imports, types OK.</li>
|
|
1443
|
+
<li><b>vp test</b> is green: right behavior.</li>
|
|
1444
|
+
</ul>
|
|
1445
|
+
<h4>AI and tests</h4>
|
|
1446
|
+
<p class="small">Tests are a good first job for AI: small, inside one room, easy to review. One prompt:</p>
|
|
1447
|
+
<pre>Write tests for
|
|
1448
|
+
src/features/cart/lib/calcTotal.ts.
|
|
1449
|
+
File: calcTotal.test.ts, same folder.
|
|
1450
|
+
Import test helpers from 'vite-plus/test'.
|
|
1451
|
+
Do NOT change calcTotal.ts.</pre>
|
|
1452
|
+
<p class="small">Read every test the AI writes. A test that checks the wrong answer is worse than no test.</p>
|
|
1453
|
+
<span class="folio">62</span></article>
|
|
1454
|
+
<article class="page" data-ch="10" hidden><span class="chaptag">Chapter 10: Tests per module</span>
|
|
1455
|
+
<h2 class="chap" style="font-size:clamp(32px,4vw,44px);margin-top:0">Course résumé</h2>
|
|
1456
|
+
<ol class="course">
|
|
1457
|
+
<li><span><b>Three zones.</b> app wires, features own business, shared knows no business. Imports go down.</span></li>
|
|
1458
|
+
<li><span><b>A feature is a house.</b> Rooms: ui, model, api, lib, types. One door: <code>index.ts</code>.</span></li>
|
|
1459
|
+
<li><span><b>app is the power panel.</b> main → providers → store + router. No business logic.</span></li>
|
|
1460
|
+
<li><span><b>Routes per feature.</b> <code>routes.tsx</code> in the feature, joined in <code>app/router.tsx</code>, pages <code>lazy</code>.</span></li>
|
|
1461
|
+
<li><span><b>State per feature.</b> One slice per drawer. Selectors ask only for their drawer. No circles.</span></li>
|
|
1462
|
+
<li><span><b>Server data.</b> RTK Query, empty <code>baseApi</code> in shared, <code>injectEndpoints</code> in the feature.</span></li>
|
|
1463
|
+
<li><span><b>Shared.</b> Copy test + rule of two. Data through props.</span></li>
|
|
1464
|
+
<li><span><b>Guards.</b> Alias in two files. Rules A, B, C in the lint block. <code>vp check</code>.</span></li>
|
|
1465
|
+
<li><span><b>AI loop.</b> Door first, one room per prompt, check, diff test.</span></li>
|
|
1466
|
+
<li><span><b>Tests in the room.</b> lib and model first. <code>vp test</code>.</span></li>
|
|
1467
|
+
</ol>
|
|
1468
|
+
<span class="folio">63</span></article>
|
|
1469
|
+
<article class="page" data-ch="10" hidden><span class="chaptag">Chapter 10: Tests per module</span>
|
|
1470
|
+
<h2 class="chap" style="font-size:clamp(36px,4.4vw,48px);margin-top:0">Résumé</h2>
|
|
1471
|
+
<div class="resume">
|
|
1472
|
+
<p><b>Tests next to the file.</b> <code>x.ts</code> → <code>x.test.ts</code>, same folder.</p>
|
|
1473
|
+
<p><b>Start with lib and model.</b> Plain functions, no React.</p>
|
|
1474
|
+
<p><b>Two greens.</b> <code>vp check</code> and <code>vp test</code>.</p>
|
|
1475
|
+
</div>
|
|
1476
|
+
<div class="check">
|
|
1477
|
+
<h3>Final checkpoint</h3>
|
|
1478
|
+
<p style="font-size:15px;margin-bottom:10px">3 small questions. Short answers. Write them in the chat.</p>
|
|
1479
|
+
<ol>
|
|
1480
|
+
<li>Where does the test for <code>features/cart/lib/calcTotal.ts</code> go: <b>A</b> or <b>B</b>?<br>A) <code>src/tests/calcTotal.test.ts</code><br>B) <code>features/cart/lib/calcTotal.test.ts</code></li>
|
|
1481
|
+
<li>Does a reducer test need React? Why?</li>
|
|
1482
|
+
<li>The whole course in one move: <code>features/wishlist</code> is deleted. What happens to (a) its tests, (b) <code>app/</code>, (c) <code>shared/</code>?</li>
|
|
1483
|
+
</ol>
|
|
1484
|
+
<p class="reply">Reply like this: 1) … 2) … 3) a=… b=… c=…</p>
|
|
1485
|
+
</div>
|
|
1486
|
+
<span class="folio">64</span></article>
|
|
1487
|
+
<article class="page" data-ch="11" hidden><span class="chaptag">Reference</span>
|
|
1488
|
+
<div class="ruleset">
|
|
1489
|
+
<h2 class="chap" style="font-size:clamp(34px,4.2vw,46px);margin-top:0">The rules</h2>
|
|
1490
|
+
<h4>Imports</h4>
|
|
1491
|
+
<ul class="rules" style="counter-reset:r">
|
|
1492
|
+
<li><b>Down only.</b> app → features → shared. Never up. Type imports count.</li>
|
|
1493
|
+
<li><b>Use the door.</b> Another feature only through <code>@/features/x</code> (its <code>index.ts</code>).</li>
|
|
1494
|
+
<li><b>No circles.</b> If A imports B, B never imports A.</li>
|
|
1495
|
+
</ul>
|
|
1496
|
+
<h4>Zones</h4>
|
|
1497
|
+
<ul class="legend">
|
|
1498
|
+
<li><code>app/</code> wiring only: main, providers, store, router, root layout. No business logic.</li>
|
|
1499
|
+
<li><code>features/x/</code> one business concept. Rooms: ui, model, api, lib, types. Only needed rooms.</li>
|
|
1500
|
+
<li><code>shared/</code> no business words. Enters by copy test + rule of two. Data through props.</li>
|
|
1501
|
+
</ul>
|
|
1502
|
+
<h4>Inside a feature</h4>
|
|
1503
|
+
<ul class="legend">
|
|
1504
|
+
<li>ui may use model. model never uses ui.</li>
|
|
1505
|
+
<li>Relative paths inside, no <code>../../</code> out. Leave with <code>@/</code>.</li>
|
|
1506
|
+
<li>Libraries from node_modules: allowed in every zone.</li>
|
|
1507
|
+
</ul>
|
|
1508
|
+
<h4>Data</h4>
|
|
1509
|
+
<ul class="legend">
|
|
1510
|
+
<li>Server data → RTK Query, <code>injectEndpoints</code> in <code>features/x/api</code>, tag words in the feature.</li>
|
|
1511
|
+
<li>Browser data → slice in <code>features/x/model</code>. Selectors ask only for their drawer.</li>
|
|
1512
|
+
<li>Never two copies of the same data.</li>
|
|
1513
|
+
</ul>
|
|
1514
|
+
</div>
|
|
1515
|
+
<span class="folio">65</span></article>
|
|
1516
|
+
<article class="page" data-ch="11" hidden><span class="chaptag">Reference</span>
|
|
1517
|
+
<div class="sheet">
|
|
1518
|
+
<h2 class="chap" style="font-size:clamp(34px,4.2vw,46px);margin-top:0">Cheat sheet</h2>
|
|
1519
|
+
<pre>src/
|
|
1520
|
+
├─ app/ main.tsx providers.tsx store.ts
|
|
1521
|
+
│ router.tsx RootLayout.tsx
|
|
1522
|
+
├─ features/x/
|
|
1523
|
+
│ ├─ index.ts routes.tsx types.ts
|
|
1524
|
+
│ ├─ ui/ model/ api/ lib/
|
|
1525
|
+
│ └─ *.test.ts next to the file
|
|
1526
|
+
└─ shared/ ui/ lib/ api/baseApi.ts config/</pre>
|
|
1527
|
+
<h4>Where does this file go?</h4>
|
|
1528
|
+
<ol class="steps">
|
|
1529
|
+
<li>Knows a business word? → <code>features/that-word/</code></li>
|
|
1530
|
+
<li>Only connects pieces? → <code>app/</code></li>
|
|
1531
|
+
<li>Works in any app and 2 features need it? → <code>shared/</code></li>
|
|
1532
|
+
</ol>
|
|
1533
|
+
<h4>Commands</h4>
|
|
1534
|
+
<pre>vp dev run the app
|
|
1535
|
+
vp check format + lint + types
|
|
1536
|
+
vp test run tests once
|
|
1537
|
+
vp test watch tests while working
|
|
1538
|
+
git diff --stat what the AI touched</pre>
|
|
1539
|
+
<h4>AI loop</h4>
|
|
1540
|
+
<p class="small">1 write the door → 2 prompt one room → 3 <code>vp check</code> + <code>vp test</code> → 4 diff test against the allowed list → next room.</p>
|
|
1541
|
+
<h4>Deletion test</h4>
|
|
1542
|
+
<p class="small">Delete <code>features/x</code>, run <code>vp check</code>. Errors only in <code>app/</code> (one store line, one router line) = boundaries hold.</p>
|
|
1543
|
+
</div>
|
|
1544
|
+
<span class="folio">66</span></article>
|
|
1545
|
+
</div>
|
|
1546
|
+
<nav class="nav" aria-label="Pages">
|
|
1547
|
+
<button id="prev" type="button">Previous</button>
|
|
1548
|
+
<span class="mid"><span class="where" id="where" aria-live="polite"></span><button id="toIndex" type="button">Index</button></span>
|
|
1549
|
+
<button id="next" type="button">Next</button>
|
|
1550
|
+
</nav>
|
|
1551
|
+
</div>
|
|
1552
|
+
<script>
|
|
1553
|
+
(function(){
|
|
1554
|
+
function rng(seed){return function(){seed=(seed*16807)%2147483647;return (seed-1)/2147483646;};}
|
|
1555
|
+
document.querySelectorAll('[data-r]').forEach(function(el){
|
|
1556
|
+
var a=el.getAttribute('data-r').split(',').map(Number);
|
|
1557
|
+
var x=a[0],y=a[1],w=a[2],h=a[3],r=rng(a[4]*7919+13);
|
|
1558
|
+
var j=function(){return Math.round((r()-0.5)*32)/10;};
|
|
1559
|
+
var d='';
|
|
1560
|
+
for(var p=0;p<2;p++){d+='M'+(x+j())+' '+(y+j())+' L'+(x+w+j())+' '+(y+j())+' L'+(x+w+j())+' '+(y+h+j())+' L'+(x+j())+' '+(y+h+j())+' Z ';}
|
|
1561
|
+
el.setAttribute('d',d);
|
|
1562
|
+
});
|
|
1563
|
+
var pages=[].slice.call(document.querySelectorAll('.page'));
|
|
1564
|
+
var N=pages.length, segs=[].slice.call(document.querySelectorAll('.seg'));
|
|
1565
|
+
var mq=window.matchMedia('(max-width:820px)');
|
|
1566
|
+
var i=0;
|
|
1567
|
+
try{var s=parseInt(localStorage.getItem('fe-course-page'),10); if(s>=0&&s<N) i=s;}catch(e){}
|
|
1568
|
+
var prev=document.getElementById('prev'), next=document.getElementById('next'), where=document.getElementById('where');
|
|
1569
|
+
var meta=document.getElementById('meta'), bar=document.getElementById('bar');
|
|
1570
|
+
function per(){return mq.matches?1:2;}
|
|
1571
|
+
function align(){ if(per()===2) i=i-(i%2); }
|
|
1572
|
+
function show(){
|
|
1573
|
+
align();
|
|
1574
|
+
var p=per();
|
|
1575
|
+
pages.forEach(function(pg,k){
|
|
1576
|
+
var vis=k>=i&&k<i+p;
|
|
1577
|
+
pg.hidden=!vis;
|
|
1578
|
+
pg.classList.toggle('left',vis&&p===2&&k===i);
|
|
1579
|
+
pg.classList.toggle('right',vis&&p===2&&k===i+1);
|
|
1580
|
+
});
|
|
1581
|
+
var last=Math.min(i+p,N);
|
|
1582
|
+
where.textContent=(p===2&&last-i===2?'Pages '+(i+1)+'–'+last:'Page '+(i+1))+' of '+N;
|
|
1583
|
+
prev.disabled=i===0; next.disabled=i+p>=N;
|
|
1584
|
+
var ch=0; for(var k=i;k<last;k++){ch=Math.max(ch,+pages[k].dataset.ch);}
|
|
1585
|
+
segs.forEach(function(sg,k){
|
|
1586
|
+
sg.className='seg'+(ch>10||k<ch-1?' done':(k===ch-1?' current':''));
|
|
1587
|
+
});
|
|
1588
|
+
meta.textContent= ch===0?'Introduction':(ch>10?'Rules and cheat sheet':'Chapter '+ch+' of 10');
|
|
1589
|
+
bar.setAttribute('aria-valuenow',String(ch>10?10:Math.max(ch-1,0)));
|
|
1590
|
+
try{localStorage.setItem('fe-course-page',String(i));}catch(e){}
|
|
1591
|
+
}
|
|
1592
|
+
function go(n){ i=Math.max(0,Math.min(N-1,n)); show(); window.scrollTo({top:0}); }
|
|
1593
|
+
prev.addEventListener('click',function(){go(i-per());});
|
|
1594
|
+
next.addEventListener('click',function(){go(i+per());});
|
|
1595
|
+
document.getElementById('toIndex').addEventListener('click',function(){go(1);});
|
|
1596
|
+
document.querySelectorAll('[data-go]').forEach(function(b){b.addEventListener('click',function(){go(+b.dataset.go);});});
|
|
1597
|
+
document.addEventListener('keydown',function(e){
|
|
1598
|
+
if(e.target.closest&&e.target.closest('input,textarea'))return;
|
|
1599
|
+
if(e.key==='ArrowRight')next.click(); if(e.key==='ArrowLeft')prev.click();
|
|
1600
|
+
});
|
|
1601
|
+
if(mq.addEventListener) mq.addEventListener('change',show);
|
|
1602
|
+
show();
|
|
1603
|
+
})();
|
|
1604
|
+
</script>
|
|
1605
|
+
</body>
|
|
1606
|
+
</html>
|