Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6107f9d

Browse files
committed
refactor tutorial selectors
1 parentaa1a2a5 commit6107f9d

File tree

2 files changed

+47
-67
lines changed

2 files changed

+47
-67
lines changed

‎web-app/src/services/selectors/index.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,36 @@ import {MachineContext} from 'typings'
22
import*asGfrom'typings/graphql'
33
import{createSelector}from'reselect'
44

5-
exportconstcurrentLevel=({tutorial, position}:MachineContext):G.Level=>{
5+
exportconstcurrentTutorial=({tutorial}:MachineContext):G.Tutorial=>{
66
if(!tutorial){
7-
thrownewError('Tutorial not found when selecting level')
7+
thrownewError('Tutorial not found')
88
}
9-
// merge in the updated position
10-
// sent with the test to ensure consistency
11-
constlevels:G.Level[]=tutorial.version.levels
9+
returntutorial
10+
}
1211

13-
constlevel:G.Level|undefined=levels.find((l:G.Level)=>l.id===position.levelId)
12+
exportconstcurrentVersion=createSelector(
13+
currentTutorial,
14+
(tutorial:G.Tutorial)=>{
15+
if(!tutorial.version){
16+
thrownewError('Tutorial version not found')
17+
}
18+
returntutorial.version
19+
})
1420

15-
if(!level){
16-
thrownewError('Level not found when selecting level')
17-
}
18-
returnlevel
19-
}
21+
exportconstcurrentLevel=(context:MachineContext):G.Level=>createSelector(
22+
currentVersion,
23+
(version:G.TutorialVersion):G.Level=>{
24+
// merge in the updated position
25+
// sent with the test to ensure consistency
26+
constlevels:G.Level[]=version.levels
27+
28+
constlevel:G.Level|undefined=levels.find((l:G.Level)=>l.id===context.position.levelId)
29+
30+
if(!level){
31+
thrownewError('Level not found when selecting level')
32+
}
33+
returnlevel
34+
})(context)
2035

2136
exportconstcurrentStage=(context:MachineContext):G.Stage=>createSelector(
2237
currentLevel,
@@ -41,4 +56,3 @@ export const currentStep = (context: MachineContext): G.Step => createSelector(
4156
returnstep
4257
}
4358
)(context)
44-

‎web-app/src/services/state/actions/context.ts

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {assign, send} from 'xstate'
22
import*asGfrom'typings/graphql'
33
import*asCRfrom'typings'
44
import*asstoragefrom'../storage'
5+
import*asselectorsfrom'../../selectors'
56

67
exportdefault{
78
setTutorial:assign({
@@ -39,19 +40,11 @@ export default {
3940
//@ts-ignore
4041
updateStepPosition:assign({
4142
position:(context:CR.MachineContext,event:CR.MachineEvent):CR.Position=>{
42-
const{tutorial, position}=context
43-
44-
if(!tutorial){
45-
thrownewError('Tutorial not found when updating step position')
46-
}
43+
const{position}=context
4744
// merge in the updated position
4845
// sent with the test to ensure consistency
49-
//@ts-ignore
50-
conststeps:G.Step[]=context.tutorial.version
51-
.levels.find((l:G.Level)=>l.id===position.levelId)
52-
.stages.find((s:G.Stage)=>s.id===position.stageId)
53-
.steps
54-
46+
conststage:G.Stage=selectors.currentStage(context)
47+
conststeps:G.Step[]=stage.steps
5548

5649
// final step but not completed
5750
if(steps[steps.length-1].id===position.stepId){
@@ -74,18 +67,11 @@ export default {
7467
}),
7568
//@ts-ignore
7669
updateStagePosition:assign({
77-
position:(context:CR.MachineContext,event:CR.MachineEvent):CR.Position=>{
78-
const{tutorial,position}=context
70+
position:(context:CR.MachineContext):CR.Position=>{
71+
const{position}=context
7972

80-
if(!tutorial){
81-
thrownewError('Tutorial not found when updating stage position')
82-
}
83-
// merge in the updated position
84-
// sent with the test to ensure consistency
85-
//@ts-ignore
86-
conststages:G.Stage[]=tutorial.version
87-
.levels.find((l:G.Level)=>l.id===position.levelId)
88-
.stages
73+
constlevel:G.Level=selectors.currentLevel(context)
74+
conststages:G.Stage[]=level.stages
8975

9076
conststageIndex=stages.findIndex((s:G.Stage)=>s.id===position.stageId)
9177
conststage:G.Stage=stages[stageIndex+1]
@@ -103,15 +89,12 @@ export default {
10389
}),
10490
//@ts-ignore
10591
updateLevelPosition:assign({
106-
position:(context:CR.MachineContext,event:CR.MachineEvent):CR.Position=>{
107-
const{tutorial, position}=context
108-
109-
if(!tutorial){
110-
thrownewError('Tutorial not found when updating level position')
111-
}
92+
position:(context:CR.MachineContext):CR.Position=>{
93+
const{position}=context
94+
constversion=selectors.currentVersion(context)
11295
// merge in the updated position
11396
// sent with the test to ensure consistency
114-
constlevels:G.Level[]=tutorial.version.levels
97+
constlevels:G.Level[]=version.levels
11598

11699
constlevelIndex=levels.findIndex((l:G.Level)=>l.id===position.levelId)
117100
constlevel:G.Level=levels[levelIndex+1]
@@ -164,23 +147,12 @@ export default {
164147
}
165148
}),
166149
loadNext:send((context:CR.MachineContext):CR.Action=>{
167-
const{tutorial,position, progress}=context
150+
const{position, progress}=context
168151

169-
if(!tutorial){
170-
thrownewError('No tutorial found for loading next step')
171-
}
152+
constversion=selectors.currentVersion(context)
153+
constlevel=selectors.currentLevel(context)
154+
conststage=selectors.currentStage(context)
172155

173-
// has next step?
174-
constlevels:G.Level[]=tutorial.version.levels
175-
constlevel:G.Level|undefined=levels.find((l:G.Level)=>l.id===position.levelId)
176-
if(!level){
177-
thrownewError('No Level found')
178-
}
179-
conststages:G.Stage[]=level.stages
180-
conststage:G.Stage|undefined=stages.find((s:G.Stage)=>s.id===position.stageId)
181-
if(!stage){
182-
thrownewError('No Stage found')
183-
}
184156
conststeps:G.Step[]=stage.steps
185157

186158
conststepIndex=steps.findIndex((s:G.Step)=>s.id===position.stepId)
@@ -197,6 +169,7 @@ export default {
197169

198170
// has next stage?
199171

172+
const{stages}=level
200173
conststageIndex=stages.findIndex((s:G.Stage)=>s.id===position.stageId)
201174
constfinalStage=(stageIndex>-1&&stageIndex===stages.length-1)
202175
consthasNextStage=(!finalStage)
@@ -214,6 +187,7 @@ export default {
214187

215188
// has next level?
216189

190+
const{levels}=version
217191
constlevelIndex=levels.findIndex((l:G.Level)=>l.id===position.levelId)
218192
constfinalLevel=(levelIndex>-1&&levelIndex===levels.length-1)
219193
consthasNextLevel=(!finalLevel)
@@ -234,19 +208,11 @@ export default {
234208
return{type:'COMPLETED'}
235209
}),
236210
stepNext:send((context:CR.MachineContext):CR.Action=>{
237-
const{tutorial, position, progress}=context
238-
239-
if(!tutorial||!tutorial.version){
240-
thrownewError('No tutorial found when loading next step')
241-
}
211+
const{position, progress}=context
242212

243-
// TODO: protect against errors
244-
//@ts-ignore
245-
conststeps:G.Step[]=tutorial.version
246-
.levels.find((l:G.Level)=>l.id===position.levelId)
247-
.stages.find((s:G.Stage)=>s.id===position.stageId)
248-
.steps
213+
conststage:G.Stage=selectors.currentStage(context)
249214

215+
const{steps}=stage
250216
// TODO: verify not -1
251217
conststepIndex=steps.findIndex((s:G.Step)=>s.id===position.stepId)
252218
constfinalStep=stepIndex===steps.length-1

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp