pin and unpin working

This commit is contained in:
Joshua Seigler 2025-04-05 17:12:27 -07:00
parent 161149a6a2
commit 7aa4a156e7

View file

@ -76,15 +76,37 @@ function App() {
}; };
const handlePinUnpin = () => { const handlePinUnpin = () => {
if (store.selected.every((x) => x <= store.pinnedCount)) { if (store.selected.every((x) => x < store.pinnedCount)) {
// we are unpinning // we are unpinning
const puzzleStart = Array.from({ length: store.pinnedCount }, (_, i) => i)
.filter((x) => !store.selected.includes(x))
.map((x) => store.puzzle[x]);
const puzzleMiddle = store.selected.map((x) => store.puzzle[x]);
const puzzleEnd = store.puzzle.slice(store.pinnedCount);
const newPuzzle = [...puzzleStart, ...puzzleMiddle, ...puzzleEnd];
setStore({ setStore({
pinnedCount: store.pinnedCount - store.selected.length, pinnedCount: store.pinnedCount - store.selected.length,
selected: [], selected: [],
}) puzzle: newPuzzle,
});
return; return;
} }
// we are pinning // we are pinning
const puzzleStart = store.puzzle.slice(0, store.pinnedCount);
const puzzleMid = store.selected
.filter((x) => x >= store.pinnedCount)
.map((x) => store.puzzle[x]);
const puzzleEnd = Array.from(
{ length: 16 - store.pinnedCount },
(_, i) => i + store.pinnedCount
)
.filter((x) => !store.selected.includes(x))
.map((x) => store.puzzle[x]);
setStore({
pinnedCount: puzzleStart.length + puzzleMid.length,
selected: [],
puzzle: [...puzzleStart, ...puzzleMid, ...puzzleEnd]
})
}; };
return ( return (
@ -106,12 +128,14 @@ function App() {
<div class="puzzle-row"> <div class="puzzle-row">
{[0, 1, 2, 3].map((col) => { {[0, 1, 2, 3].map((col) => {
const index = 4 * row + col; const index = 4 * row + col;
const answer = getFromPuzzle(index).answer;
return ( return (
<button <button
classList={{ classList={{
"puzzle-item": true, "puzzle-item": true,
"is-selected": store.selected.includes(index), "is-selected": store.selected.includes(index),
}} }}
style={{ "view-transition-name": `puzzle-${answer}` }}
type="button" type="button"
on:click={() => { on:click={() => {
setStore({ setStore({
@ -121,7 +145,7 @@ function App() {
}); });
}} }}
> >
{getFromPuzzle(index).answer} {answer}
{store.pinnedCount > index && <div class="badge">🔒</div>} {store.pinnedCount > index && <div class="badge">🔒</div>}
</button> </button>
); );
@ -131,8 +155,13 @@ function App() {
</For> </For>
<div class="puzzle-actions"> <div class="puzzle-actions">
<div class="puzzle-actions-secondary"> <div class="puzzle-actions-secondary">
<button type="button" disabled={store.selected.length === 0}> <button
{store.selected.length > 0 && store.selected.every((x) => x <= store.pinnedCount) type="button"
disabled={store.selected.length === 0}
on:click={handlePinUnpin}
>
{store.selected.length > 0 &&
store.selected.every((x) => x < store.pinnedCount)
? "Unpin" ? "Unpin"
: "Pin"} : "Pin"}
</button> </button>