Skip to content
Snippets Groups Projects
Commit 312ebd05 authored by Aron Nikku's avatar Aron Nikku
Browse files

Fixed form working with backend

parent bb8d577e
Branches
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
*/__pycache__/ */__pycache__/
*.pyc *.pyc
*.db
# Logs # Logs
logs logs
......
import axios from 'axios'; import axios from 'axios';
import { Character } from './types'; import { Character, CharacterCreate } from './types';
const API_BASE = 'http://localhost:8000'; const API_BASE = 'http://localhost:8000';
...@@ -12,3 +12,8 @@ export async function damageCharacter(id: number): Promise<Character> { ...@@ -12,3 +12,8 @@ export async function damageCharacter(id: number): Promise<Character> {
const response = await axios.post(`${API_BASE}/damage/${id}`); const response = await axios.post(`${API_BASE}/damage/${id}`);
return response.data; return response.data;
} }
export async function addCharacter(data: CharacterCreate): Promise<Character> {
const response = await axios.post(`${API_BASE}/characters`, data=data);
return response.data;
}
...@@ -2,6 +2,7 @@ import { Container, Title } from '@mantine/core'; ...@@ -2,6 +2,7 @@ import { Container, Title } from '@mantine/core';
import { useForm } from '@mantine/form'; import { useForm } from '@mantine/form';
import { TextInput, NumberInput, Button, Box } from '@mantine/core'; import { TextInput, NumberInput, Button, Box } from '@mantine/core';
import { addCharacter } from '../api';
function CharacterForm() { function CharacterForm() {
const form = useForm({ const form = useForm({
...@@ -19,6 +20,8 @@ function CharacterForm() { ...@@ -19,6 +20,8 @@ function CharacterForm() {
const handleSubmit = (values: typeof form.values) => { const handleSubmit = (values: typeof form.values) => {
console.log('Character data:', values); console.log('Character data:', values);
// you could pass this data to a parent or send to API, etc. // you could pass this data to a parent or send to API, etc.
addCharacter({name: values.name, max_hp: values.hp})
}; };
return ( return (
......
export interface Character { export interface Character {
id: number; id: number;
name: string; name: string;
max_hp: number; max_hp: number;
current_hp: number; current_hp: number;
is_dead: boolean; is_dead: boolean;
} }
export interface CharacterCreate {
name: string;
max_hp: number;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment