-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.gts
131 lines (96 loc) · 2.92 KB
/
index.gts
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
119
120
121
122
123
124
125
126
127
128
129
130
import Component from '@glimmer/component';
import { tracked, cached } from '@glimmer/tracking';
import { on } from '@ember/modifier';
import { fn } from '@ember/helper';
import Success from './success';
import { toBase64, findTodaysWord } from './utils';
import { words } from './words';
import { isAttemptActive, colorForLetter } from './state/queries';
import { handleKeyDown, handleInput, initialStateFor, guess } from './state/actions';
interface Args {
// year-month-day
day: string;
}
const not = (item) => !item;
const l = console.log;
const Letter = <template>
<input
disabled={{not @active}}
class="
border border-2 border-gray-300
h-12 w-12 sm:h-16 sm:w-16
text-center text-3xl uppercase
rounded
outline-none
focus:ring-offset-2 focus:ring-4 ring-blue-400 ring-offset-slate-50
focus:border-1 focus:border-gray-100
{{colorForLetter @letter @attempt}}
{{if @attempt.isFrozen 'font-bold text-white' 'text-black'}}
"
value={{@letter.value}}
{{on 'keydown' (fn handleKeyDown @letter)}}
{{on 'input' (fn handleInput @letter)}}
...attributes
>
</template>;
const Row = <template>
{{#let (isAttemptActive @board @attempt) as |isActive|}}
<form {{on 'submit' @tryGuess}} class="flex gap-4">
{{yield isActive}}
<button type="submit" class="sr-only">Submit Attempt</button>
</form>
{{/let}}
</template>;
export default class Board extends Component<Args> {
@tracked message;
@tracked success = false;
wordList = words(this);
@cached
get todaysWord() {
return findTodaysWord(this.args.day, this.wordList.answers);
}
tryGuess = (attempt, submitEvent) => {
submitEvent.preventDefault();
guess(attempt, {
onError: this.say,
onWin: () => (this.success = true),
answer: this.todaysWord,
all: this.wordList.all
});
};
say = async (msg: string) => {
this.message = msg;
await new Promise(resolve => setTimeout(resolve, 3000));
this.message = '';
}
<template>
<div class="h-12 flex justify-center items-center">
{{#if this.message}}
{{this.message}}
{{/if}}
</div>
<div class="grid gap-4 relative p-4 sm:p-0">
<Success @show={{this.success}} />
{{#let (initialStateFor @day) as |board|}}
{{#each board as |attempt aIndex|}}
<Row
@board={{board}}
@attempt={{attempt}}
@tryGuess={{fn this.tryGuess attempt}}
as |isActive|
>
{{#each attempt.letters as |letter lIndex|}}
<Letter
@attempt={{attempt}}
@letter={{letter}}
@active={{isActive}}
aria-label="Letter {{lIndex}} for Attempt {{aIndex}}"
/>
{{/each}}
</Row>
{{/each}}
{{/let}}
{{log "Don't be a cheater" (toBase64 this.todaysWord)}}
</div>
</template>
}