wip
This commit is contained in:
parent
c4694e142a
commit
4eea46b26b
|
@ -5,10 +5,27 @@
|
|||
import { onMount } from "svelte";
|
||||
import type { Grid } from "$lib/types";
|
||||
import { snakeToCamelCase } from "$lib/case";
|
||||
import { createForm } from "svelte-forms-lib";
|
||||
import * as yup from "yup";
|
||||
|
||||
let grid: 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() {
|
||||
try {
|
||||
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(() => {
|
||||
fetchData();
|
||||
});
|
||||
|
@ -26,6 +54,44 @@
|
|||
|
||||
<main>
|
||||
{#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}
|
||||
</main>
|
||||
|
|
|
@ -7,8 +7,7 @@
|
|||
import { deleteFromArray } from "$lib/arrays";
|
||||
|
||||
import { snakeToCamelCase } from "$lib/case";
|
||||
import { onMount, tick } from "svelte";
|
||||
import type { AxiosResponse } from "axios";
|
||||
import { onMount } from "svelte";
|
||||
import { createForm } from "svelte-forms-lib";
|
||||
import * as yup from "yup";
|
||||
|
||||
|
|
22
grids/migrations/0005_alter_grid_height_alter_grid_width.py
Normal file
22
grids/migrations/0005_alter_grid_height_alter_grid_width.py
Normal 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),
|
||||
),
|
||||
]
|
22
grids/migrations/0006_alter_grid_height_alter_grid_width.py
Normal file
22
grids/migrations/0006_alter_grid_height_alter_grid_width.py
Normal 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),
|
||||
),
|
||||
]
|
|
@ -15,14 +15,18 @@ class Word(models.Model):
|
|||
class Grid(models.Model):
|
||||
level = models.ForeignKey(Level, on_delete=models.CASCADE, related_name="grids")
|
||||
words = models.ManyToManyField(Word, through="Placement")
|
||||
width = models.IntegerField()
|
||||
height = models.IntegerField()
|
||||
width = models.IntegerField(default=1)
|
||||
height = models.IntegerField(default=1)
|
||||
|
||||
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)
|
||||
|
||||
def populate(self):
|
||||
print(self.width)
|
||||
print(self.height)
|
||||
|
||||
|
||||
class Direction:
|
||||
RIGHT = 1
|
||||
|
|
|
@ -31,7 +31,7 @@ class WordSerializer(serializers.ModelSerializer):
|
|||
class GridSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Grid
|
||||
fields = ["id", "words"]
|
||||
fields = ["id", "words", "width", "height", "words"]
|
||||
|
||||
|
||||
class PlacementSerializer(serializers.ModelSerializer):
|
||||
|
|
|
@ -27,6 +27,17 @@ class GridViewSet(viewsets.ModelViewSet):
|
|||
queryset = Grid.objects.all()
|
||||
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):
|
||||
queryset = Placement.objects.all()
|
||||
|
|
Loading…
Reference in a new issue