Skip to content

Commit d26998a

Browse files
author
belledw
committed
task data can now be fetched from backend and displayed on pet task carousel
1 parent a114115 commit d26998a

4 files changed

Lines changed: 87 additions & 14 deletions

File tree

client/src/components/ui/pet-task-carousel.tsx

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useEffect, useState } from "react";
2+
13
import {
24
Carousel,
35
CarouselContent,
@@ -7,22 +9,76 @@ import {
79
} from "@/components/ui/carousel";
810
import { TaskCard } from "@/components/ui/task-card";
911

12+
interface taskInfoObject {
13+
descr: string;
14+
due: string;
15+
forPet: number;
16+
id: number;
17+
isComplete: boolean;
18+
title: string;
19+
}
20+
21+
interface taskInfoResponse {
22+
task: taskInfoObject;
23+
}
24+
1025
export function TaskCarousel() {
26+
const [taskInfo, setTaskInfo] = useState<taskInfoResponse>();
27+
28+
useEffect(() => {
29+
const fetchData = async () => {
30+
const data = await fetch("http://localhost:8000/tasks/");
31+
const taskInfo = await data.json();
32+
setTaskInfo(taskInfo);
33+
};
34+
35+
fetchData();
36+
}, []);
37+
38+
// Not very efficient, and need to modify for multiple pets.
39+
function getTaskInfo(id: number, item: keyof taskInfoObject) {
40+
if (taskInfo) {
41+
let task: keyof taskInfoResponse;
42+
for (task in taskInfo) {
43+
if (taskInfo[task].id == id) {
44+
return taskInfo[task][item].toString();
45+
}
46+
}
47+
} else {
48+
return "";
49+
}
50+
}
51+
52+
function formatDueDate(id: number, type: "day" | "time") {
53+
const due = getTaskInfo(id, "due") || "";
54+
const newDue = due.slice(0, 10);
55+
const date = new Date(newDue);
56+
const newDate = date.toString();
57+
58+
if (type === "day") {
59+
const finalDate = newDate.slice(0, 16);
60+
return finalDate;
61+
} else if (type === "time") {
62+
const finalTime = newDate.slice(16, 21);
63+
return finalTime;
64+
}
65+
}
66+
1167
return (
1268
<Carousel className="w-fit max-w-full">
1369
<CarouselContent>
1470
<CarouselItem key={1} className="basis-sm lg:basis-md -ml-10 mr-5">
1571
<TaskCard
16-
date="Wednesday, July 7th, 2025"
17-
title="Feed Spaghetti"
72+
date={formatDueDate(1, "day") || ""}
73+
title={getTaskInfo(1, "title") || ""}
1874
usrImg=""
1975
usrAlt="@username"
2076
usrColour="bg-gray-200"
2177
usrInit="CS"
2278
usrName="You"
2379
usrAssignee="exampletask"
24-
time="7:00 am"
25-
descr="50.0 g x Premium Cat Biscuits"
80+
time={formatDueDate(1, "time") || ""}
81+
descr={getTaskInfo(1, "descr") || ""}
2682
/>
2783
</CarouselItem>
2884
<CarouselItem key={2} className="basis-sm lg:basis-md -ml-10 mr-5">

client/src/pages/pet.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ export default function Default() {
5050
const months = Math.floor((yearsAndMonths % 1) * 12);
5151
if (years == 1 && months == 1) {
5252
return `${years} year ${months} month`;
53-
} else if (years > 1 && months == 1) {
53+
} else if ((years > 1 || years == 0) && months == 1) {
5454
return `${years} years ${months} month`;
55-
} else if (years == 1 && months > 1) {
55+
} else if (years == 1 && (months > 1 || months == 0)) {
5656
return `${years} year ${months} months`;
5757
} else {
5858
return `${years} years ${months} months`;
@@ -68,9 +68,9 @@ export default function Default() {
6868
petImg={petInfo?.img || ""}
6969
petAlt="Spaghetti's Profile Image"
7070
petInit="S"
71-
petName={petInfo?.name || "Loading..."}
72-
petType={petInfo?.type || "Loading..."}
73-
petAge={formatPetAge() || "Loading..."}
71+
petName={petInfo?.name || ""}
72+
petType={petInfo?.type || ""}
73+
petAge={formatPetAge() || ""}
7474
/>
7575

7676
<div className="mx-[7vw]">
@@ -80,9 +80,9 @@ export default function Default() {
8080

8181
{settingsMenu === true && (
8282
<PetSettings
83-
petName={petInfo?.name || "Loading..."}
84-
petType={petInfo?.type || "Loading..."}
85-
petDOB={petInfo?.dob || "Loading..."}
83+
petName={petInfo?.name || ""}
84+
petType={petInfo?.type || ""}
85+
petDOB={petInfo?.dob || ""}
8686
onClickClose={() => setSettingsMenu(false)}
8787
/>
8888
)}

server/tasks/serializers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from rest_framework import serializers
2+
from .models import Task
3+
4+
class TaskSerializer(serializers.ModelSerializer):
5+
class Meta:
6+
model = Task
7+
fields = "__all__"

server/tasks/views.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
from django.shortcuts import render
2-
from django.http import HttpResponse
2+
from django.http import HttpResponse, JsonResponse
3+
from rest_framework.response import Response
4+
from rest_framework.decorators import api_view
5+
from rest_framework.renderers import JSONRenderer
6+
7+
from tasks.models import Task
8+
from tasks.serializers import TaskSerializer
39

410
# Create your views here.
11+
# https://www.django-rest-framework.org/tutorial/1-serialization/
12+
@api_view(('GET',))
513
def index(request):
6-
return HttpResponse("Hello, world. This is the tasks page of Fetch!, a pet care app.")
14+
data = Task.objects.all()
15+
serializer = TaskSerializer(data, many=True)
16+
return JsonResponse(serializer.data, safe=False)

0 commit comments

Comments
 (0)