From e19becd1748217e0c33fb3c8a71dbe8e2d160b14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Juvenal?= Date: Wed, 28 Aug 2024 10:51:30 -0300 Subject: [PATCH] Simplify and add login required to TourGuideAssistantView --- example/assets/js/App.tsx | 2 +- example/assets/js/components/Chat/Chat.tsx | 12 +++++- .../js/components/TourGuide/TourGuide.tsx | 42 +++++++++++++++---- example/demo/views.py | 10 ++--- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/example/assets/js/App.tsx b/example/assets/js/App.tsx index 6fa300e..7bd7044 100644 --- a/example/assets/js/App.tsx +++ b/example/assets/js/App.tsx @@ -75,7 +75,7 @@ const ExampleIndex = () => { message: ( <> You must be logged in to engage with the examples. Please{" "} - + log in {" "} to continue. diff --git a/example/assets/js/components/Chat/Chat.tsx b/example/assets/js/components/Chat/Chat.tsx index 1ab333f..be6fa6c 100644 --- a/example/assets/js/components/Chat/Chat.tsx +++ b/example/assets/js/components/Chat/Chat.tsx @@ -30,6 +30,7 @@ import { useMessageList, useThreadList, } from "django-ai-assistant-client"; +import { Link } from "react-router-dom"; function ChatMessage({ message, @@ -174,8 +175,15 @@ export function Chat({ assistantId }: { assistantId: string }) { notifications.show({ title: "Login Required", - message: - "You must be logged in to engage with the examples. Please log in to continue.", + message: ( + <> + You must be logged in to engage with the examples. Please{" "} + + log in + {" "} + to continue. + + ), color: "red", autoClose: 5000, withCloseButton: true, diff --git a/example/assets/js/components/TourGuide/TourGuide.tsx b/example/assets/js/components/TourGuide/TourGuide.tsx index aa5a1c0..1baecc1 100644 --- a/example/assets/js/components/TourGuide/TourGuide.tsx +++ b/example/assets/js/components/TourGuide/TourGuide.tsx @@ -7,8 +7,12 @@ import { Group, } from "@mantine/core"; import { useEffect, useState } from "react"; +import { notifications } from "@mantine/notifications"; +import { Link } from "react-router-dom"; export function TourGuide() { + const [showLoginNotification, setShowLoginNotification] = + useState(false); const [latitude, setLatitude] = useState(""); const [longitude, setLongitude] = useState(""); const [attractions, setAttractions] = useState([]); @@ -24,22 +28,42 @@ export function TourGuide() { ); }, []); - function findAttractions() { + async function findAttractions() { if (!latitude || !longitude) { return; } setLoading(true); - fetch(`/tour-guide/?coordinate=${latitude},${longitude}`) - .then((response) => response.json()) - .then((data: any) => { - console.log(data); - - setAttractions(data.nearby_attractions); - }) - .finally(() => setLoading(false)); + const response = await fetch(`/tour-guide/?coordinate=${latitude},${longitude}`); + const data = await response.json(); + if (data.error) { + setShowLoginNotification(true); + } else { + setAttractions(data.nearby_attractions); + } + setLoading(false) } + useEffect(() => { + if (!showLoginNotification) return; + + notifications.show({ + title: "Login Required", + message: ( + <> + You must be logged in to engage with the examples. Please{" "} + + log in + {" "} + to continue. + + ), + color: "red", + autoClose: 5000, + withCloseButton: true, + }); + }, [showLoginNotification]); + return ( diff --git a/example/demo/views.py b/example/demo/views.py index 467b768..74c2a1c 100644 --- a/example/demo/views.py +++ b/example/demo/views.py @@ -3,7 +3,6 @@ from django.contrib import messages from django.http import JsonResponse from django.shortcuts import get_object_or_404, redirect, render -from django.utils import timezone from django.views import View from django.views.generic.base import TemplateView @@ -112,16 +111,15 @@ def post(self, request, *args, **kwargs): class TourGuideAssistantView(View): def get(self, request, *args, **kwargs): + if not request.user.is_authenticated: + return JsonResponse({"error": "You must be logged in to use this feature."}, status=401) + coordinates = request.GET.get("coordinate") if not coordinates: return JsonResponse({}) - thread = create_thread( - name=f"{timezone.now().isoformat()} - Tour Guide Chat", user=request.user - ) - a = TourGuideAIAssistant() - data = a.run(f"My coordinates are: ({coordinates})", thread.id) + data = a.run(f"My coordinates are: ({coordinates})") return JsonResponse(json.loads(data))