Skip to content

Commit

Permalink
Simplify and add login required to TourGuideAssistantView
Browse files Browse the repository at this point in the history
  • Loading branch information
fjsj committed Aug 28, 2024
1 parent e67f0aa commit e19becd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion example/assets/js/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const ExampleIndex = () => {
message: (
<>
You must be logged in to engage with the examples. Please{" "}
<Link to="admin/" target="_blank">
<Link to="/admin/" target="_blank">
log in
</Link>{" "}
to continue.
Expand Down
12 changes: 10 additions & 2 deletions example/assets/js/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
useMessageList,
useThreadList,
} from "django-ai-assistant-client";
import { Link } from "react-router-dom";

function ChatMessage({
message,
Expand Down Expand Up @@ -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{" "}
<Link to="/admin/" target="_blank">
log in
</Link>{" "}
to continue.
</>
),
color: "red",
autoClose: 5000,
withCloseButton: true,
Expand Down
42 changes: 33 additions & 9 deletions example/assets/js/components/TourGuide/TourGuide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>(false);
const [latitude, setLatitude] = useState("");
const [longitude, setLongitude] = useState("");
const [attractions, setAttractions] = useState([]);
Expand All @@ -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{" "}
<Link to="/admin/" target="_blank">
log in
</Link>{" "}
to continue.
</>
),
color: "red",
autoClose: 5000,
withCloseButton: true,
});
}, [showLoginNotification]);

return (
<Container>
<LoadingOverlay visible={loading} />
Expand Down
10 changes: 4 additions & 6 deletions example/demo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))

0 comments on commit e19becd

Please sign in to comment.