From 8ab79b7c72e82df4af24e09ffc907b3c5cf9bb5f Mon Sep 17 00:00:00 2001 From: Viktor Barzin Date: Mon, 23 Jun 2025 21:16:07 +0000 Subject: [PATCH] squash parametrs form to take less space --- crawler/frontend/src/App.tsx | 3 + .../frontend/src/components/Parameters.tsx | 77 ++++++++++++++++--- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/crawler/frontend/src/App.tsx b/crawler/frontend/src/App.tsx index 12ca163..606a663 100644 --- a/crawler/frontend/src/App.tsx +++ b/crawler/frontend/src/App.tsx @@ -39,6 +39,9 @@ const fetchData = async (user: User, baseQueyrUri: string, parameters: Parameter if (parameters.available_from) { queryString.append("let_date_available_from", parameters.available_from.toISOString()); } + if (parameters.district) { + queryString.append("district_names", parameters.district); + } const response = await fetch(baseQueyrUri + '?' + queryString, { diff --git a/crawler/frontend/src/components/Parameters.tsx b/crawler/frontend/src/components/Parameters.tsx index 1e356ef..c1d9b68 100644 --- a/crawler/frontend/src/components/Parameters.tsx +++ b/crawler/frontend/src/components/Parameters.tsx @@ -1,6 +1,8 @@ +import { getUser } from "@/auth/authService"; import { zodResolver } from "@hookform/resolvers/zod"; import { DialogTitle } from "@radix-ui/react-dialog"; -import { useState } from "react"; +import type { User } from "oidc-client-ts"; +import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { Button } from "./ui/button"; @@ -33,8 +35,28 @@ export interface ParameterValues { min_sqm?: number last_seen_days?: number available_from?: Date + district: string } +const fetchDistricts = async (user: User | null) => { + const accessToken = user?.access_token; + + const response = await fetch('/api/get_districts', + { + method: 'GET', + headers: { + 'Authorization': `Bearer ${accessToken}`, // Pass the token + 'Content-Type': 'application/json', + }, + } + ); + if (!response.ok) { + throw new Error('Error: ' + response.status); + } + const data: Response = await response.json(); + return data; +}; + export function Parameters( props: { isOpen: boolean, @@ -47,6 +69,16 @@ export function Parameters( } = useForm() const [action, setAction] = useState<'fetch-data' | 'visualize' | null>(null) const [availableFromRawInput, setAvailableFromRawInput] = useState("now"); + const [districts, setDistricts] = useState([]); + console.log(districts) + + useEffect(() => { + getUser().then(user => { + fetchDistricts(user).then(data => { + setDistricts(Object.keys(data)); + }) + }) + }, []); const formSchema = z.object({ metric: z.nativeEnum(Metric, { required_error: "Metric is required" }), @@ -58,6 +90,7 @@ export function Parameters( min_sqm: z.number().optional(), last_seen_days: z.number().min(0).optional(), available_from: z.date(), + district: z.string() }) const form = useForm>({ resolver: zodResolver(formSchema), @@ -71,6 +104,7 @@ export function Parameters( min_sqm: 50, last_seen_days: 7, available_from: new Date(), + district: '' }, }) // 2. Define a submit handler. @@ -97,12 +131,12 @@ export function Parameters(
- + ( - + Metric to visualize @@ -144,11 +178,34 @@ export function Parameters( )} /> + {/* ( + + District + + + + )} + /> */} ( - + Min square meters field.onChange(Number(e.target.value))} /> @@ -161,7 +218,7 @@ export function Parameters( control={form.control} name="min_bedrooms" render={({ field }) => ( - + Minimum number of bedrooms field.onChange(Number(e.target.value))} /> @@ -174,7 +231,7 @@ export function Parameters( control={form.control} name="max_bedrooms" render={({ field }) => ( - + Maximum number of bedrooms field.onChange(Number(e.target.value))} /> @@ -187,7 +244,7 @@ export function Parameters( control={form.control} name="min_price" render={({ field }) => ( - + Min price field.onChange(Number(e.target.value))} /> @@ -200,7 +257,7 @@ export function Parameters( control={form.control} name="max_price" render={({ field }) => ( - + Max price field.onChange(Number(e.target.value))} /> @@ -229,7 +286,7 @@ export function Parameters( control={form.control} name="last_seen_days" render={({ field }) => ( - + Last seen days field.onChange(Number(e.target.value))} />