From d58624e7bf830d6676d10160f39e6a6b9ee8198b Mon Sep 17 00:00:00 2001 From: Haritha Kotte Date: Thu, 26 Sep 2024 11:34:11 +0530 Subject: [PATCH] Call api to get query result --- app/controllers/queries_controller.rb | 6 ++++++ config/routes.rb | 6 +++++- nextjs-frontend/components/recipeForm.tsx | 13 +++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/controllers/queries_controller.rb b/app/controllers/queries_controller.rb index 151bd48..6d00d62 100644 --- a/app/controllers/queries_controller.rb +++ b/app/controllers/queries_controller.rb @@ -1,6 +1,12 @@ class QueriesController < ApplicationController before_action :set_query, only: %i[ show update destroy ] + def get_query_results + # Get query results from python script + query_results = "Something" + render json: { query_results: query_results } + end + # GET /queries def index @queries = Query.all diff --git a/config/routes.rb b/config/routes.rb index 7d679ff..072ab74 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,9 @@ Rails.application.routes.draw do - resources :queries + resources :queries do + collection do + get :get_query_results + end + end # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. diff --git a/nextjs-frontend/components/recipeForm.tsx b/nextjs-frontend/components/recipeForm.tsx index 315b5e4..36e2db6 100644 --- a/nextjs-frontend/components/recipeForm.tsx +++ b/nextjs-frontend/components/recipeForm.tsx @@ -7,10 +7,19 @@ const RecipeForm = () => { const [inputValue, setInputValue] = useState(""); const [output, setOutput] = useState(""); - const handleSubmit = (e: React.FormEvent) => { + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); // Handle form submission logic here and set the output - setOutput(inputValue); // Example output + try { + const response = await fetch( + "http://localhost:3001/queries/get_query_results" + ); + const data = await response.json(); + console.log("Server response:", data); + setOutput(data.query_results); // Example output + } catch (error) { + console.error("Error:", error); + } console.log("Submitted:", inputValue); };