use createStore
This commit is contained in:
parent
b6d12c7e47
commit
d437b1e5a1
2 changed files with 50 additions and 43 deletions
15
src/App.css
15
src/App.css
|
@ -66,12 +66,12 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.puzzle {
|
.puzzle {
|
||||||
--unit: 0.5rem;
|
--unit: min(0.5rem, 1.15vw);
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: calc(1 * var(--unit));
|
gap: calc(1 * var(--unit));
|
||||||
font-size: calc(3 * var(--unit));
|
font-size: calc(3 * var(--unit));
|
||||||
font-weight: 600;
|
font-weight: 700;
|
||||||
width: calc((20 * 4 + 3) * var(--unit));
|
width: calc((20 * 4 + 3) * var(--unit));
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
|
@ -105,13 +105,14 @@
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
border-radius: calc(1 * var(--unit));
|
border-radius: calc(1 * var(--unit));
|
||||||
|
font-weight: 500;
|
||||||
|
|
||||||
&[data-level="0"] {
|
&[data-level="0"] {
|
||||||
background-color: var(--group-blue);
|
background-color: var(--group-yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
&[data-level="1"] {
|
&[data-level="1"] {
|
||||||
background-color: var(--group-yellow);
|
background-color: var(--group-blue);
|
||||||
}
|
}
|
||||||
|
|
||||||
&[data-level="2"] {
|
&[data-level="2"] {
|
||||||
|
@ -122,6 +123,12 @@
|
||||||
background-color: var(--group-purple);
|
background-color: var(--group-purple);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.puzzle-group-name {
|
||||||
|
font-weight: 700;
|
||||||
|
}
|
||||||
|
.puzzle-group-members {
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
.puzzle-actions {
|
.puzzle-actions {
|
||||||
display: flex;
|
display: flex;
|
||||||
margin-top: calc(1 * var(--unit));
|
margin-top: calc(1 * var(--unit));
|
||||||
|
|
76
src/App.tsx
76
src/App.tsx
|
@ -2,9 +2,10 @@ import { For, createMemo, createSignal } from "solid-js";
|
||||||
import connections from "./assets/connections.json";
|
import connections from "./assets/connections.json";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
import { shuffleArray } from "./utils";
|
import { shuffleArray } from "./utils";
|
||||||
|
import { createStore } from "solid-js/store";
|
||||||
|
|
||||||
type Connection = typeof connections[number]
|
type Connection = (typeof connections)[number];
|
||||||
type Answer = Connection["answers"][number]
|
type Answer = Connection["answers"][number];
|
||||||
|
|
||||||
function fromIndex(index: number): [number, number] {
|
function fromIndex(index: number): [number, number] {
|
||||||
const col = index % 4;
|
const col = index % 4;
|
||||||
|
@ -13,49 +14,48 @@ function fromIndex(index: number): [number, number] {
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [puzzleIndex, setPuzzleIndex] = createSignal(0);
|
const [store, setStore] = createStore({
|
||||||
// const [pinnedCount, setPinnedCount] = createSignal(0);
|
puzzleIndex: 0,
|
||||||
const [selected, setSelected] = createSignal<number[]>([]);
|
selected: [] as number[],
|
||||||
const [solvedGroups, setSolvedGroups] = createSignal<Answer[]>([])
|
solvedGroups: [] as Answer[],
|
||||||
const [puzzle, setPuzzle] = createSignal(
|
puzzle: shuffleArray(Array.from({ length: 16 }, (_, i) => i)),
|
||||||
shuffleArray(
|
get answers() {
|
||||||
Array(16)
|
return connections[store.puzzleIndex].answers;
|
||||||
.fill(0)
|
},
|
||||||
.map((_, i) => i)
|
});
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const answers = () => connections[puzzleIndex()].answers;
|
|
||||||
const getFromPuzzle = (index: number) => {
|
const getFromPuzzle = (index: number) => {
|
||||||
const puzzleIndex = puzzle()[index];
|
const puzzleIndex = store.puzzle[index];
|
||||||
const [groupIndex, memberIndex] = fromIndex(puzzleIndex);
|
const [groupIndex, memberIndex] = fromIndex(puzzleIndex);
|
||||||
const group = answers()[groupIndex];
|
const group = store.answers[groupIndex];
|
||||||
return {
|
return {
|
||||||
group: group.group,
|
group: group.group,
|
||||||
level: group.level,
|
level: group.level,
|
||||||
answer: answers()[groupIndex].members[memberIndex],
|
answer: store.answers[groupIndex].members[memberIndex],
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
const handleGuess = () => {
|
const handleGuess = () => {
|
||||||
const selectedAnswers = selected().map((x) => getFromPuzzle(x));
|
const selectedAnswers = store.selected.map((x) => getFromPuzzle(x));
|
||||||
const { level } = selectedAnswers[0];
|
const { level } = selectedAnswers[0];
|
||||||
const isCorrect = selectedAnswers.every(
|
const isCorrect = selectedAnswers.every((x) => x.level === level);
|
||||||
x => x.level === level
|
|
||||||
);
|
|
||||||
if (!isCorrect) {
|
if (!isCorrect) {
|
||||||
// TODO you got it wrong
|
// TODO you got it wrong
|
||||||
alert("wrong");
|
alert("wrong");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setPuzzle(puzzle().filter((x) => selected().every(s => puzzle()[s] !== x)));
|
setStore({
|
||||||
setSelected([]);
|
puzzle: store.puzzle.filter((x) => store.selected.every((s) => store.puzzle[s] !== x)),
|
||||||
const newSolvedGroup = answers().find((x) => x.level === level);
|
selected: [],
|
||||||
|
})
|
||||||
|
const newSolvedGroup = store.answers.find((x) => x.level === level);
|
||||||
if (newSolvedGroup != null) {
|
if (newSolvedGroup != null) {
|
||||||
setSolvedGroups([...solvedGroups(), newSolvedGroup])
|
setStore({
|
||||||
|
solvedGroups: [...store.solvedGroups, newSolvedGroup]
|
||||||
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// const handlePinUnpin = () => {
|
// const handlePinUnpin = () => {
|
||||||
// const sel = selected()
|
// const sel = store.selected
|
||||||
// if (sel.every(x => x <= pinnedCount())) {
|
// if (sel.every(x => x <= pinnedCount())) {
|
||||||
// // we are unpinning
|
// // we are unpinning
|
||||||
// return
|
// return
|
||||||
|
@ -67,17 +67,17 @@ function App() {
|
||||||
<main class="container">
|
<main class="container">
|
||||||
<h1>Slick Connections</h1>
|
<h1>Slick Connections</h1>
|
||||||
<div class="puzzle">
|
<div class="puzzle">
|
||||||
<For each={solvedGroups()}>
|
<For each={store.solvedGroups}>
|
||||||
{({ group, level, members }) => (
|
{({ group, level, members }) => (
|
||||||
<div class="puzzle-row">
|
<div class="puzzle-row">
|
||||||
<div class="puzzle-group" data-level={level}>
|
<div class="puzzle-group" data-level={level}>
|
||||||
<strong>{group}</strong>
|
<div class="puzzle-group-name">{group}</div>
|
||||||
<div>{members.join(', ')}</div>
|
<div class="puzzle-group-members">{members.join(", ")}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</For>
|
</For>
|
||||||
<For each={[0, 1, 2, 3].slice(0, puzzle().length / 4)}>
|
<For each={[0, 1, 2, 3].slice(0, store.puzzle.length / 4)}>
|
||||||
{(row) => (
|
{(row) => (
|
||||||
<div class="puzzle-row">
|
<div class="puzzle-row">
|
||||||
{[0, 1, 2, 3].map((col) => {
|
{[0, 1, 2, 3].map((col) => {
|
||||||
|
@ -86,15 +86,15 @@ function App() {
|
||||||
<button
|
<button
|
||||||
classList={{
|
classList={{
|
||||||
"puzzle-item": true,
|
"puzzle-item": true,
|
||||||
"is-selected": selected().includes(index),
|
"is-selected": store.selected.includes(index),
|
||||||
}}
|
}}
|
||||||
type="button"
|
type="button"
|
||||||
on:click={() => {
|
on:click={() => {
|
||||||
setSelected(
|
setStore({
|
||||||
selected().includes(index)
|
selected: store.selected.includes(index)
|
||||||
? selected().filter((x) => x !== index)
|
? store.selected.filter((x) => x !== index)
|
||||||
: [...selected(), index]
|
: [...store.selected, index],
|
||||||
);
|
});
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{getFromPuzzle(index).answer}
|
{getFromPuzzle(index).answer}
|
||||||
|
@ -105,14 +105,14 @@ function App() {
|
||||||
)}
|
)}
|
||||||
</For>
|
</For>
|
||||||
<div class="puzzle-actions">
|
<div class="puzzle-actions">
|
||||||
<button type="button" disabled={selected().length === 0}>
|
<button type="button" disabled={store.selected.length === 0}>
|
||||||
Pin
|
Pin
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
id="submitButton"
|
id="submitButton"
|
||||||
type="button"
|
type="button"
|
||||||
on:click={handleGuess}
|
on:click={handleGuess}
|
||||||
disabled={selected().length !== 4}
|
disabled={store.selected.length !== 4}
|
||||||
>
|
>
|
||||||
Submit
|
Submit
|
||||||
</button>
|
</button>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue