|
3 | 3 | import { writable } from 'svelte/store';
|
4 | 4 | import { dojoStore } from '$stores/dojoStore';
|
5 | 5 | import { account } from '$stores/account'
|
6 |
| - import type { Account } from 'starknet'; |
7 |
| -
|
| 6 | + import TxToast from '$lib/ui/TxToast.svelte'; |
| 7 | + import { goto } from '$app/navigation'; |
8 | 8 |
|
9 | 9 | const gridSize = 25;
|
10 | 10 | const totalCells = gridSize * gridSize;
|
11 | 11 | const grid = writable<number[]>([]);
|
12 | 12 |
|
13 | 13 | let { client } = $dojoStore;
|
14 | 14 |
|
| 15 | + let toastMessage = ''; |
| 16 | + let toastStatus = 'loading'; |
| 17 | + let showToast = false; |
15 | 18 |
|
16 | 19 | onMount(() => {
|
17 |
| - // Initialize the grid with no active cells |
18 | 20 | grid.set([]);
|
19 | 21 | });
|
20 | 22 |
|
|
35 | 37 | grid.set([]);
|
36 | 38 | }
|
37 | 39 |
|
38 |
| - function submit() { |
39 |
| - client.mapmaker.create({account: $account, objects: {objects: $grid}}); |
| 40 | + async function submit() { |
| 41 | + showToast = true; |
| 42 | + toastMessage = 'Creating map...'; |
| 43 | + toastStatus = 'loading'; |
| 44 | + try { |
| 45 | + await client.mapmaker.create({account: $account, objects: {objects: $grid}}); |
| 46 | + toastMessage = 'Map created successfully!'; |
| 47 | + toastStatus = 'success'; |
| 48 | + setTimeout(() => { |
| 49 | + goto('/client/games/openGames'); |
| 50 | + }, 2000); |
| 51 | + } catch (error) { |
| 52 | + console.error('Error creating map:', error); |
| 53 | + toastMessage = 'Failed to create map.'; |
| 54 | + toastStatus = 'error'; |
| 55 | + } |
40 | 56 | }
|
41 | 57 |
|
42 | 58 | function isActive(index: number, activeIndices: number[]): boolean {
|
|
74 | 90 | </style>
|
75 | 91 |
|
76 | 92 | <div class="controls">
|
77 |
| - <button class = "ml-4 px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-700 transition" on:click={resetGrid}>Reset Grid</button> |
78 |
| - <button class = "mr-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-700 transition" on:click={submit}>Submit</button> |
| 93 | + <button class="ml-4 px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-700 transition" on:click={resetGrid}>Reset Grid</button> |
| 94 | + <button class="mr-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-700 transition" on:click={submit}>Submit</button> |
79 | 95 | </div>
|
80 | 96 |
|
81 | 97 | <div class="grid">
|
82 | 98 | {#each Array(totalCells) as _, index}
|
83 |
| - <div |
| 99 | + <button |
| 100 | + type="button" |
84 | 101 | class="cell {isActive(index, $grid) ? 'active' : ''}"
|
85 | 102 | on:click={() => toggleCell(Math.floor(index / gridSize), index % gridSize)}
|
86 |
| - ></div> |
| 103 | + on:keydown={(e) => e.key === 'Enter' && toggleCell(Math.floor(index / gridSize), index % gridSize)} |
| 104 | + /> |
87 | 105 | {/each}
|
88 | 106 | </div>
|
| 107 | + |
| 108 | +{#if showToast} |
| 109 | + <TxToast message={toastMessage} status={toastStatus} /> |
| 110 | +{/if} |
0 commit comments