mirror of
https://github.com/Sosokker/HomieCare.git
synced 2025-12-19 02:04:03 +01:00
Add recommendation card UI
This commit is contained in:
parent
fcb485c7fc
commit
2e21b9ffa7
18
frontend/src/api/RecommendData.tsx
Normal file
18
frontend/src/api/RecommendData.tsx
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
const fetchRecommendation = async (
|
||||||
|
data: HealthData,
|
||||||
|
): Promise<RecommendationData[] | null> => {
|
||||||
|
try {
|
||||||
|
const response = await axios.post<RecommendationData[]>(
|
||||||
|
'http://127.0.0.1:8000/api/v1/recommend/recommendation/',
|
||||||
|
data,
|
||||||
|
);
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching recommendation:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default fetchRecommendation;
|
||||||
55
frontend/src/components/Alert/Alert.tsx
Normal file
55
frontend/src/components/Alert/Alert.tsx
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import React, { useEffect, useState } from 'react';
|
||||||
|
import DangerAlert from './DangerAlert';
|
||||||
|
import GoodAlert from './GoodAlert';
|
||||||
|
import WarningAlert from './WarningAlert';
|
||||||
|
import fetchRecommendation from '../../api/RecommendData';
|
||||||
|
|
||||||
|
const Alert: React.FC<HealthData> = ({
|
||||||
|
indoor_temp,
|
||||||
|
outdoor_temp,
|
||||||
|
outdoor_pm25,
|
||||||
|
outdoor_pm10,
|
||||||
|
outdoor_humidity,
|
||||||
|
}) => {
|
||||||
|
const [recommendation, setRecommendation] = useState<
|
||||||
|
string | null | undefined
|
||||||
|
>(null);
|
||||||
|
const [message, setMessage] = useState<string | undefined>('');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchData = async () => {
|
||||||
|
const data = await fetchRecommendation({
|
||||||
|
indoor_temp: indoor_temp,
|
||||||
|
outdoor_temp: outdoor_temp,
|
||||||
|
outdoor_pm25: outdoor_pm25,
|
||||||
|
outdoor_pm10: outdoor_pm10,
|
||||||
|
outdoor_humidity: outdoor_humidity,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (data && data.length > 0) {
|
||||||
|
// priority: danger > warning > good
|
||||||
|
for (let i = 0; i < data.length; i++) {
|
||||||
|
if (data[i].status === 'danger' || data[i].status === 'warning') {
|
||||||
|
setRecommendation(data[i].status);
|
||||||
|
setMessage(data[i].recommendation);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchData();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (recommendation === 'good') {
|
||||||
|
return <GoodAlert message={message} />;
|
||||||
|
} else if (recommendation === 'warning') {
|
||||||
|
return <WarningAlert message={message} />;
|
||||||
|
} else if (recommendation === 'danger') {
|
||||||
|
return <DangerAlert message={message} />;
|
||||||
|
} else {
|
||||||
|
return <GoodAlert message="No tips today" />;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Alert;
|
||||||
@ -19,9 +19,7 @@ const DangerAlert: React.FC<Message> = ({ message }) => {
|
|||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<h5 className="mb-3 font-semibold text-[#B45454]">
|
<h5 className="mb-3 font-semibold text-[#B45454]">Dangerous!</h5>
|
||||||
There were 1 errors with your submission
|
|
||||||
</h5>
|
|
||||||
<ul>
|
<ul>
|
||||||
<li className="leading-relaxed text-[#CD5D5D]">{message}</li>
|
<li className="leading-relaxed text-[#CD5D5D]">{message}</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
@ -20,7 +20,7 @@ const GoodAlert: React.FC<Message> = ({ message }) => {
|
|||||||
</div>
|
</div>
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<h5 className="mb-3 text-lg font-semibold text-black dark:text-[#34D399] ">
|
<h5 className="mb-3 text-lg font-semibold text-black dark:text-[#34D399] ">
|
||||||
Message Sent Successfully
|
Everything is good
|
||||||
</h5>
|
</h5>
|
||||||
<p className="text-base leading-relaxed text-body">{message}</p>
|
<p className="text-base leading-relaxed text-body">{message}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
13
frontend/src/types/Recommend.ts
Normal file
13
frontend/src/types/Recommend.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
interface HealthData {
|
||||||
|
indoor_temp: number | undefined;
|
||||||
|
outdoor_temp: number | undefined;
|
||||||
|
outdoor_pm25: number | undefined;
|
||||||
|
outdoor_pm10: number | undefined;
|
||||||
|
outdoor_humidity: number | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RecommendationData {
|
||||||
|
timestamp?: Date;
|
||||||
|
recommendation?: string;
|
||||||
|
status?: string;
|
||||||
|
}
|
||||||
@ -1,3 +1,3 @@
|
|||||||
interface Message {
|
interface Message {
|
||||||
message: string;
|
message: string | undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user