This commit is contained in:
Ewen 2023-09-19 17:56:27 +02:00
parent c4694e142a
commit 4eea46b26b
7 changed files with 130 additions and 6 deletions

View file

@ -5,10 +5,27 @@
import { onMount } from "svelte"; import { onMount } from "svelte";
import type { Grid } from "$lib/types"; import type { Grid } from "$lib/types";
import { snakeToCamelCase } from "$lib/case"; import { snakeToCamelCase } from "$lib/case";
import { createForm } from "svelte-forms-lib";
import * as yup from "yup";
let grid: Grid | null; let grid: Grid | null;
$: grid = null; $: grid = null;
// form
const { form, errors, state, handleChange, handleSubmit } = createForm({
initialValues: {
width: "",
height: "",
},
validationSchema: yup.object().shape({
width: yup.number().positive().moreThan(0),
heigt: yup.number().positive().moreThan(0),
}),
onSubmit: (values) => {
submitPopulate(values);
},
});
function fetchData() { function fetchData() {
try { try {
axios.get("grids/" + $page.params.id + "/").then((data) => { axios.get("grids/" + $page.params.id + "/").then((data) => {
@ -19,6 +36,17 @@
} }
} }
function submitPopulate(values) {
try {
axios.patch("grids/" + $page.params.id + "/", {
width: values.width,
height: values.height,
});
} catch (e) {
console.log(e);
}
}
onMount(() => { onMount(() => {
fetchData(); fetchData();
}); });
@ -26,6 +54,44 @@
<main> <main>
{#if grid} {#if grid}
id : {grid.id}, width: {grid.width}, height: {grid.height} {#if grid.words.length == 0}
<form on:submit={handleSubmit}>
<div class="flex flex-row">
<div>
<label for="width" class="block">Ledander :</label>
<input
type="number"
name="width"
class="block"
bind:value={$form.width}
/>
{#if $errors.width}
<span class="text-red-500 text-sm"
>{$errors.width}</span
>
{/if}
</div>
<div>
<label for="height" class="block">Uhelder :</label>
<input
type="number"
name="height"
class="block"
bind:value={$form.height}
/>
{#if $errors.height}
<span class="text-red-500 text-sm"
>{$errors.height}</span
>
{/if}
</div>
</div>
<button
type="submit"
class="bg-violet-600 hover:bg-violet-700 rounded-md text-white p-1 px-2 justify-center"
>Leuniañ ar gael</button
>
</form>
{/if}
{/if} {/if}
</main> </main>

View file

@ -7,8 +7,7 @@
import { deleteFromArray } from "$lib/arrays"; import { deleteFromArray } from "$lib/arrays";
import { snakeToCamelCase } from "$lib/case"; import { snakeToCamelCase } from "$lib/case";
import { onMount, tick } from "svelte"; import { onMount } from "svelte";
import type { AxiosResponse } from "axios";
import { createForm } from "svelte-forms-lib"; import { createForm } from "svelte-forms-lib";
import * as yup from "yup"; import * as yup from "yup";

View file

@ -0,0 +1,22 @@
# Generated by Django 4.2.5 on 2023-09-19 14:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("grids", "0004_grid_height_grid_width"),
]
operations = [
migrations.AlterField(
model_name="grid",
name="height",
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name="grid",
name="width",
field=models.IntegerField(default=0),
),
]

View file

@ -0,0 +1,22 @@
# Generated by Django 4.2.5 on 2023-09-19 14:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("grids", "0005_alter_grid_height_alter_grid_width"),
]
operations = [
migrations.AlterField(
model_name="grid",
name="height",
field=models.IntegerField(default=1),
),
migrations.AlterField(
model_name="grid",
name="width",
field=models.IntegerField(default=1),
),
]

View file

@ -15,14 +15,18 @@ 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() width = models.IntegerField(default=1)
height = models.IntegerField() height = models.IntegerField(default=1)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if not hasattr(self, "level") or self.level is None: if not hasattr(self, "level") or self.level is None:
self.level = Level.objects.first() self.level = Level.objects.first()
super(Grid, self).save(*args, **kwargs) super(Grid, self).save(*args, **kwargs)
def populate(self):
print(self.width)
print(self.height)
class Direction: class Direction:
RIGHT = 1 RIGHT = 1

View file

@ -31,7 +31,7 @@ class WordSerializer(serializers.ModelSerializer):
class GridSerializer(serializers.ModelSerializer): class GridSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Grid model = Grid
fields = ["id", "words"] fields = ["id", "words", "width", "height", "words"]
class PlacementSerializer(serializers.ModelSerializer): class PlacementSerializer(serializers.ModelSerializer):

View file

@ -27,6 +27,17 @@ class GridViewSet(viewsets.ModelViewSet):
queryset = Grid.objects.all() queryset = Grid.objects.all()
serializer_class = GridSerializer serializer_class = GridSerializer
def partial_update(self, request, pk=None):
instance = self.get_object()
print(request.data)
serializer = self.get_serializer(instance, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
instance.populate()
serializer = self.get_serializer(instance)
return Response(serializer.data)
class PlacementViewSet(viewsets.ModelViewSet): class PlacementViewSet(viewsets.ModelViewSet):
queryset = Placement.objects.all() queryset = Placement.objects.all()