forked from JacobSNGoodwin/ranker-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pages.tsx
65 lines (58 loc) · 1.64 KB
/
Pages.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { useEffect } from 'react';
import { CSSTransition } from 'react-transition-group';
import { useSnapshot } from 'valtio';
import Create from './pages/Create';
import Join from './pages/Join';
import { Results } from './pages/Results';
import { Voting } from './pages/Voting';
import { WaitingRoom } from './pages/WaitingRoom';
import Welcome from './pages/Welcome';
import { actions, AppPage, state } from './state';
const routeConfig = {
[AppPage.Welcome]: Welcome,
[AppPage.Create]: Create,
[AppPage.Join]: Join,
[AppPage.WaitingRoom]: WaitingRoom,
[AppPage.Voting]: Voting,
[AppPage.Results]: Results,
};
const Pages: React.FC = () => {
const currentState = useSnapshot(state);
useEffect(() => {
if (
currentState.me?.id &&
currentState.poll &&
!currentState.poll?.hasStarted
) {
actions.setPage(AppPage.WaitingRoom);
}
if (currentState.me?.id && currentState.poll?.hasStarted) {
actions.setPage(AppPage.Voting);
}
if (currentState.me?.id && currentState.hasVoted) {
actions.setPage(AppPage.Results);
}
}, [
currentState.me?.id,
currentState.poll?.hasStarted,
currentState.hasVoted,
]);
return (
<>
{Object.entries(routeConfig).map(([page, Component]) => (
<CSSTransition
key={page}
in={page === currentState.currentPage}
timeout={300}
classNames="page"
unmountOnExit
>
<div className="page mobile-height max-w-screen-sm mx-auto py-8 px-4 overflow-y-auto">
<Component />
</div>
</CSSTransition>
))}
</>
);
};
export default Pages;