This commit is contained in:
Ewen 2023-09-17 21:16:04 +02:00
parent 3601fc81e8
commit c4694e142a
7 changed files with 104 additions and 6 deletions

View file

@ -1,10 +1,13 @@
<script lang="ts"> <script lang="ts">
export let onClick = (e?) => {}; export let onClick = (e?) => {};
export let square = false;
</script> </script>
<button <button
type="button" type="button"
class="bg-violet-600 hover:bg-violet-700 rounded-md text-white p-1 w-6 h-6 justify-center" class="bg-violet-600 hover:bg-violet-700 rounded-md text-white p-1 px-2 justify-center {square
? 'w-6 h-6'
: ''}"
on:click|preventDefault={onClick} on:click|preventDefault={onClick}
> >
<slot /> <slot />

View file

@ -11,3 +11,10 @@ export interface Word {
definition: string; definition: string;
level: Level; level: Level;
} }
export interface Grid {
id: number;
words: Word[];
width: number;
height: number;
}

View file

@ -1,7 +1,22 @@
<script> <script>
import { page } from "$app/stores"; import { page } from "$app/stores";
import ButtonMain from "$lib/ButtonMain.svelte";
import axios from "$lib/api";
function newGrid() {
try {
axios.post("grids/").then((data) => {
const gridId = data.data.id;
window.location.href = "/ma_chaeliou/" + gridId;
});
} catch (e) {
console.log(e);
}
}
</script> </script>
<main> <main>
{$page.url.pathname} <ButtonMain onClick={newGrid}
><span class="fa-solid fa-plus mr-2" />Krouiñ ur gael</ButtonMain
>
</main> </main>

View file

@ -0,0 +1,31 @@
<script lang="ts">
import { page } from "$app/stores";
import ButtonMain from "$lib/ButtonMain.svelte";
import axios from "$lib/api";
import { onMount } from "svelte";
import type { Grid } from "$lib/types";
import { snakeToCamelCase } from "$lib/case";
let grid: Grid | null;
$: grid = null;
function fetchData() {
try {
axios.get("grids/" + $page.params.id + "/").then((data) => {
grid = snakeToCamelCase(data.data);
});
} catch (e) {
console.log(e);
}
}
onMount(() => {
fetchData();
});
</script>
<main>
{#if grid}
id : {grid.id}, width: {grid.width}, height: {grid.height}
{/if}
</main>

View file

@ -37,8 +37,13 @@
function fetchLevels() { function fetchLevels() {
try { try {
promiseLevels = axios.get("levels/").then((data) => { promiseLevels = axios
.get("levels/")
.then((data) => {
levels = snakeToCamelCase(data.data); levels = snakeToCamelCase(data.data);
})
.then(() => {
$form.level = levels[0].id.toString();
}); });
} catch (e) { } catch (e) {
console.log(e); console.log(e);
@ -97,6 +102,12 @@
onMount(() => { onMount(() => {
fetchData(); fetchData();
fetchLevels(); fetchLevels();
window.addEventListener("keyup", (e) => {
if (e.key == "Escape") {
toggleFormNew();
}
});
}); });
</script> </script>
@ -139,7 +150,7 @@
class="p-1 rounded-md border" class="p-1 rounded-md border"
> >
{#await promiseLevels then} {#await promiseLevels then}
{#each levels as level, i} {#each levels as level}
<option value={level.levelNumber}> <option value={level.levelNumber}>
{level.levelString}</option {level.levelString}</option
> >

View file

@ -0,0 +1,24 @@
# Generated by Django 4.2.5 on 2023-09-17 19:14
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("grids", "0003_rename_string_number_level_level_string"),
]
operations = [
migrations.AddField(
model_name="grid",
name="height",
field=models.IntegerField(default=0),
preserve_default=False,
),
migrations.AddField(
model_name="grid",
name="width",
field=models.IntegerField(default=0),
preserve_default=False,
),
]

View file

@ -15,6 +15,13 @@ class Word(models.Model):
class Grid(models.Model): class Grid(models.Model):
level = models.ForeignKey(Level, on_delete=models.CASCADE, related_name="grids") level = models.ForeignKey(Level, on_delete=models.CASCADE, related_name="grids")
words = models.ManyToManyField(Word, through="Placement") words = models.ManyToManyField(Word, through="Placement")
width = models.IntegerField()
height = models.IntegerField()
def save(self, *args, **kwargs):
if not hasattr(self, "level") or self.level is None:
self.level = Level.objects.first()
super(Grid, self).save(*args, **kwargs)
class Direction: class Direction: