Skip to content

Commit 7f1d56d

Browse files
author
Isaac Mann
committed
partially modify matching machine
1 parent c96226e commit 7f1d56d

File tree

1 file changed

+74
-48
lines changed

1 file changed

+74
-48
lines changed

src/machines/matching.ts

Lines changed: 74 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ import { Machine, assign } from 'xstate';
22

33
interface MatchingSchema {
44
states: {
5-
answering: {
5+
quiz: {
66
states: {
7-
topList: {
7+
answering: {
88
states: {
9-
unselected: {};
10-
selected: {};
11-
};
12-
};
13-
bottomList: {
14-
states: {
15-
unselected: {};
16-
selected: {};
9+
topList: {
10+
states: {
11+
unselected: {};
12+
selected: {};
13+
};
14+
};
15+
bottomList: {
16+
states: {
17+
unselected: {};
18+
selected: {};
19+
};
20+
};
1721
};
1822
};
23+
verifying: {};
1924
};
2025
};
2126
submitted: {
@@ -31,6 +36,8 @@ interface MatchingSchema {
3136
type MatchingEvents =
3237
| { type: 'SELECT_TOP'; selectedItem: any }
3338
| { type: 'SELECT_BOTTOM'; selectedItem: any }
39+
| { type: 'CONTINUE' }
40+
| { type: 'CHANGE_ANSWERS' }
3441
| { type: 'RESET' };
3542

3643
// The context (extended state) of the machine
@@ -46,64 +53,79 @@ export const matchingMachine = Machine<
4653
>(
4754
{
4855
id: 'matching',
49-
initial: 'answering',
56+
initial: 'quiz',
5057
context: {
5158
topSelectedItem: undefined,
5259
bottomSelectedItem: undefined
5360
},
5461
states: {
55-
answering: {
56-
type: 'parallel',
57-
onDone: 'submitted',
62+
quiz: {
63+
initial: 'answering',
5864
states: {
59-
topList: {
60-
initial: 'unselected',
65+
answering: {
66+
type: 'parallel',
67+
on: {
68+
CONTINUE: {
69+
target: 'verifying'
70+
}
71+
},
6172
states: {
62-
unselected: {
63-
on: {
64-
SELECT_TOP: {
65-
target: 'selected',
66-
actions: ['setTopSelectedItem']
73+
topList: {
74+
initial: 'unselected',
75+
states: {
76+
unselected: {
77+
on: {
78+
SELECT_TOP: {
79+
target: 'selected',
80+
actions: ['setTopSelectedItem']
81+
}
82+
}
83+
},
84+
selected: {
85+
on: {
86+
SELECT_TOP: {
87+
target: 'selected',
88+
actions: ['setTopSelectedItem']
89+
}
90+
}
6791
}
6892
}
6993
},
70-
selected: {
71-
on: {
72-
SELECT_TOP: {
73-
target: 'selected',
74-
actions: ['setTopSelectedItem']
94+
bottomList: {
95+
initial: 'unselected',
96+
states: {
97+
unselected: {
98+
on: {
99+
SELECT_BOTTOM: {
100+
target: 'selected',
101+
actions: ['setBottomSelectedItem']
102+
}
103+
}
104+
},
105+
selected: {
106+
on: {
107+
SELECT_BOTTOM: {
108+
target: 'selected',
109+
actions: ['setBottomSelectedItem']
110+
}
111+
}
75112
}
76-
},
77-
type: 'final'
113+
}
78114
}
79115
}
80116
},
81-
bottomList: {
82-
initial: 'unselected',
83-
states: {
84-
unselected: {
85-
on: {
86-
SELECT_BOTTOM: {
87-
target: 'selected',
88-
actions: ['setBottomSelectedItem']
89-
}
90-
}
91-
},
92-
selected: {
93-
on: {
94-
SELECT_BOTTOM: {
95-
target: 'selected',
96-
actions: ['setBottomSelectedItem']
97-
}
98-
},
99-
type: 'final'
100-
}
117+
verifying: {
118+
on: {
119+
CHANGE_ANSWERS: 'answering'
101120
}
102121
}
103122
}
104123
},
105124
submitted: {
106125
initial: 'evaluating',
126+
on: {
127+
RESET: { target: 'quiz', actions: ['clearSelection'] }
128+
},
107129
states: {
108130
evaluating: {
109131
on: {
@@ -126,6 +148,10 @@ export const matchingMachine = Machine<
126148
})),
127149
setBottomSelectedItem: assign((ctx, event: any) => ({
128150
bottomSelectedItem: event.selectedItem
151+
})),
152+
clearSelection: assign((ctx, event) => ({
153+
topSelectedItem: undefined,
154+
bottomSelectedItem: undefined
129155
}))
130156
}
131157
}

0 commit comments

Comments
 (0)