From ba07581f31f2bd7b694c73965e9764978422b12f Mon Sep 17 00:00:00 2001 From: Patryk Date: Sat, 26 Apr 2025 00:43:45 +0200 Subject: [PATCH] init add Notice --- ArtisanConnect/api/notices.jsx | 39 ++++++--- ArtisanConnect/app/(tabs)/notice/create.jsx | 87 +++++++++++++++++++-- 2 files changed, 106 insertions(+), 20 deletions(-) diff --git a/ArtisanConnect/api/notices.jsx b/ArtisanConnect/api/notices.jsx index 1d198c2..22f8893 100644 --- a/ArtisanConnect/api/notices.jsx +++ b/ArtisanConnect/api/notices.jsx @@ -1,20 +1,35 @@ const API_URL = "https://testowe.zikor.pl/api/v1/notices/"; export async function listNotices() { - const response = await fetch(`${API_URL}get/all`); - const data = await response.json(); - if (!response.ok) { - throw new Error("Error"); - } - return data; + const response = await fetch(`${API_URL}get/all`); + const data = await response.json(); + if (!response.ok) { + throw new Error("Error"); + } + return data; } export async function getNoticeById(noticeId) { - const response = await fetch(`${API_URL}get/${noticeId}`); + const response = await fetch(`${API_URL}get/${noticeId}`); - const data = await response.json(); - if (!response.ok) { - throw new Error("Error"); - } - return data; + const data = await response.json(); + if (!response.ok) { + throw new Error("Error"); + } + return data; +} + +export async function createNotice(notice) { + console.log("Notice created", notice); + // const response = await fetch(`${API_URL}add`, { + // method: "POST", + // headers: { + // "Content-Type": "application/json", + // }, + // body: JSON.stringify(notice), + // }); + // console.log("Response", response); + // if (!response.ok) { + // throw new Error("Error"); + // } } diff --git a/ArtisanConnect/app/(tabs)/notice/create.jsx b/ArtisanConnect/app/(tabs)/notice/create.jsx index 0bfcf58..c019fa1 100644 --- a/ArtisanConnect/app/(tabs)/notice/create.jsx +++ b/ArtisanConnect/app/(tabs)/notice/create.jsx @@ -1,3 +1,4 @@ +import { useState } from "react"; import { Button, ButtonText } from "@/components/ui/button"; import { FormControl } from "@/components/ui/form-control"; import { Input, InputField } from "@/components/ui/input"; @@ -16,35 +17,101 @@ import { SelectDragIndicatorWrapper, SelectItem, } from "@/components/ui/select"; + import { ChevronDownIcon } from "@/components/ui/icon"; +import { useMutation } from "@tanstack/react-query"; +import { createNotice } from "@/api/notices"; export default function CreateNotice() { + const [title, setTitle] = useState(""); + const [description, setDescription] = useState(""); + const [price, setPrice] = useState(""); + const [category, setCategory] = useState(""); + const [error, setError] = useState({ + title: false, + description: false, + price: false, + category: false, + }); + + const noticeMutation = useMutation({ + mutationFn: () => + createNotice({ + title: title, + clientId: 1, + description: description, + price: parseFloat(price), + category: category, + status: "ACTIVE", + }), + onSuccess: () => { + console.log("Notice created successfully"); + }, + onError: (error) => { + console.error("Error creating notice"); + }, + }); + + const addNotice = () => { + setError({ + title: !title, + description: !description, + price: !price, + category: !category, + }); + + if (!title || !description || !price || !category) { + console.log("Error in form"); + return; + } + noticeMutation.mutate(); + }; + return ( Tytuł - - + + setTitle(value)} + /> Opis - Cena - - + + setPrice(value)} + /> Kategoria - setCategory(value)} + isInvalid={error.category} + > @@ -62,7 +129,11 @@ export default function CreateNotice() { -