-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage-flipper.ts
118 lines (103 loc) · 4.5 KB
/
page-flipper.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React, { useState, useEffect, useReducer, useRef } from "react";
import "./App.css";
export default function App(): JSX.Element {
return (
<div>
<h1>uhhhhhhhhhhh</h1>
<hr />
{/* <Testing /> */}
<Ting />
</div>
);
}
const TING_ACTIONS = {
BACK_PAGE: "back-page",
NEXT_PAGE: "next-page",
};
interface TingState {
wordsPage: number;
visibleWords: Array<string>;
}
interface TingActions {
type: keyof typeof TING_ACTIONS;
payload: {
words: Array<string>;
};
}
function reducer(state: TingState, { type, payload }: TingActions): TingState {
switch (type) {
case TING_ACTIONS.BACK_PAGE:
if (!state.wordsPage) return state;
return {
wordsPage: state.wordsPage - 1,
visibleWords: payload.words.slice(
(state.wordsPage - 1) * 10,
state.wordsPage * 10
),
};
case TING_ACTIONS.NEXT_PAGE:
if (state.wordsPage === 30) return state;
return {
wordsPage: state.wordsPage + 1,
visibleWords: payload.words.slice(
(state.wordsPage + 1) * 10,
(state.wordsPage + 2) * 10
),
};
default:
console.warn("inside reducer's default");
return state;
}
}
function Ting(): React.ReactElement {
const rawWords =
"about|above|add|after|again|air|all|almost|along|also|always|America|an|and|animal|another|answer|any|are|around|as|ask|at|away|back|be|because|been|before|began|begin|being|below|between|big|book|both|boy|but|by|call|came|can|car|carry|change|children|city|close|come|could|country|cut|day|did|different|do|does|don't|down|each|earth|eat|end|enough|even|every|example|eye|face|family|far|father|feet|few|find|first|follow|food|for|form|found|four|from|get|girl|give|go|good|got|great|group|grow|had|hand|hard|has|have|he|head|hear|help|her|here|high|him|his|home|house|how|idea|if|important|in|Indian|into|is|it|its|it's|just|keep|kind|know|land|large|last|later|learn|leave|left|let|letter|life|light|like|line|list|little|live|long|look|made|make|man|many|may|me|mean|men|might|mile|miss|more|most|mother|mountain|move|much|must|my|name|near|need|never|new|next|night|no|not|now|number|of|off|often|oil|old|on|once|one|only|open|or|other|our|out|over|own|page|paper|part|people|picture|place|plant|play|point|put|question|quick|quickly|quite|read|really|right|river|run|said|same|saw|say|school|sea|second|see|seem|sentence|set|she|should|show|side|small|so|some|something|sometimes|song|soon|sound|spell|start|state|still|stop|story|study|such|take|talk|tell|than|that|the|their|them|then|there|these|they|thing|think|this|those|thought|three|through|time|to|together|too|took|tree|try|turn|two|under|until|up|us|use|very|walk|want|was|watch|water|way|we|well|went|were|what|when|where|which|while|white|who|why|will|with|without|word|work|world|would|write|year|you|young|your";
const words = rawWords.split("|");
const [state, dispatch] = useReducer(reducer, {
wordsPage: 0,
visibleWords: words.slice(0, 10),
});
return (
<div>
{state.visibleWords.map((word) => (
<div key={word} className="word">
{word}
</div>
))}
<button
onClick={() =>
dispatch({
type: TING_ACTIONS.BACK_PAGE as keyof typeof TING_ACTIONS,
payload: { words },
})
}
>
go backkk
</button>
<button
onClick={() =>
dispatch({
type: TING_ACTIONS.NEXT_PAGE as keyof typeof TING_ACTIONS,
payload: { words },
})
}
>
go forward
</button>
</div>
);
}
function Testing(): JSX.Element {
const [state, dispatch] = useReducer(testred, { some: "stuf", other: 1, new: 4 });
return <div>
<button onClick={() => dispatch({type:'a', payload: {updated: 'asd'}})}>jkhjkl</button>
{state.new}
</div>;
}
function testred(state: any, { type, payload }: any) {
switch (type) {
case "a":
console.log(--state.new)
return { ...state, some: state.new.toString(), other: state.new };
}
}