From b35d12f6b901192a2ec8066366bc2001538b2d7c Mon Sep 17 00:00:00 2001 From: Jason Lobell Date: Tue, 8 Oct 2024 18:09:55 -0400 Subject: [PATCH] fix query form --- app/query/page.tsx | 90 +++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/app/query/page.tsx b/app/query/page.tsx index f23675b..b9cc51a 100644 --- a/app/query/page.tsx +++ b/app/query/page.tsx @@ -3,18 +3,17 @@ import React, { useState } from 'react'; import { BACKEND_IP } from '../config'; // Ensure the path is correct - export default function QueryPage() { // State variables for form inputs with default values const [country, setCountry] = useState('USA'); const [startDate, setStartDate] = useState('2010-01-01'); const [endDate, setEndDate] = useState('2024-03-01'); - const [keywords, setKeywords] = useState(['all']); - const [groups, setGroups] = useState(['all']); - const [keywordInput, setKeywordInput] = useState(''); // no default input - const [groupInput, setGroupInput] = useState(''); // no default input - const [numComments, setNumComments] = useState(5); - const [postOrComment, setPostOrComment] = useState('posts'); + const [keywords, setKeywords] = useState([]); // Empty initially + const [groups, setGroups] = useState([]); // Empty initially + const [keywordInput, setKeywordInput] = useState(''); // No default input + const [groupInput, setGroupInput] = useState(''); // No default input + const [numComments, setNumComments] = useState(-1); // -1 indicates no specific input + const [postOrComment, setPostOrComment] = useState('posts'); // Default to "posts" const [numDocuments, setNumDocuments] = useState(50); // State variables for handling responses and loading state @@ -23,44 +22,43 @@ export default function QueryPage() { const [error, setError] = useState(null); // Handle form submission - // Handle form submission -const handleSubmit = async (event: React.FormEvent) => { - event.preventDefault(); - setIsLoading(true); - setError(null); - setResponse(null); - - // Ensure keywords and groups have default values if empty - const finalKeywords = keywords.length > 0 ? keywords : ['']; - const finalGroups = groups.length > 0 ? groups : ['']; - - // Encode keywords and groups as comma-separated values - const encodedKeywords = finalKeywords.map(keyword => encodeURIComponent(keyword)).join(','); - const encodedGroups = finalGroups.map(group => encodeURIComponent(group)).join(','); - - // Convert start and end dates to integer format (YYYYMMDD) - const startDateInt = parseInt(startDate.replace(/-/g, ''), 10); - const endDateInt = parseInt(endDate.replace(/-/g, ''), 10); - - // Construct the URL using query parameters - const url = `${BACKEND_IP}/query?country=${encodeURIComponent(country)}&start=${startDateInt}&end=${endDateInt}&keywords=${encodedKeywords}&groups=${encodedGroups}&num_comments=${numComments}&post_or_comment=${postOrComment}&num_documents=${numDocuments}`; - - try { - const res = await fetch(url, { method: 'GET' }); - - if (!res.ok) { - throw new Error(`Server error: ${res.status}`); + const handleSubmit = async (event: React.FormEvent) => { + event.preventDefault(); + setIsLoading(true); + setError(null); + setResponse(null); + + // Ensure keywords and groups have default values if empty + const finalKeywords = keywords.length > 0 ? keywords : ['all']; // Set 'all' if no user input + const finalGroups = groups.length > 0 ? groups : ['all']; // Set 'all' if no user input + + // Encode keywords and groups as comma-separated values + const encodedKeywords = finalKeywords.map(keyword => encodeURIComponent(keyword)).join(','); + const encodedGroups = finalGroups.map(group => encodeURIComponent(group)).join(','); + + // Convert start and end dates to integer format (YYYYMMDD) + const startDateInt = parseInt(startDate.replace(/-/g, ''), 10); + const endDateInt = parseInt(endDate.replace(/-/g, ''), 10); + + // Construct the URL using query parameters + const url = `${BACKEND_IP}/query?country=${encodeURIComponent(country)}&start=${startDateInt}&end=${endDateInt}&keywords=${encodedKeywords}&groups=${encodedGroups}&num_comments=${numComments}&post_or_comment=${postOrComment}&num_documents=${numDocuments}`; + + try { + const res = await fetch(url, { method: 'GET' }); + + if (!res.ok) { + throw new Error(`Server error: ${res.status}`); + } + + const data = await res.json(); + setResponse(data); + } catch (err: any) { + console.error('Error fetching data:', err); + setError(err.message || 'Error fetching data. Please try again.'); + } finally { + setIsLoading(false); } - - const data = await res.json(); - setResponse(data); - } catch (err: any) { - console.error('Error fetching data:', err); - setError(err.message || 'Error fetching data. Please try again.'); - } finally { - setIsLoading(false); - } -}; + }; // Handle changes in the keyword and group input fields const handleKeywordChange = (event: React.ChangeEvent) => { @@ -203,8 +201,8 @@ const handleSubmit = async (event: React.FormEvent) => { onChange={(e) => setPostOrComment(e.target.value)} style={{ color: 'black' }} > - - + +