mirror of
https://github.com/lemeow125/StudE-Frontend.git
synced 2024-11-17 06:19:25 +08:00
Changed to circle rendering for student status
This commit is contained in:
parent
058b120ce9
commit
85e2a13071
1 changed files with 115 additions and 68 deletions
|
@ -176,6 +176,7 @@ export default function Home() {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Can probably just get the max distance from the array and use it as radius
|
||||||
const [student_statuses, setStudentStatuses] = useState<any>([]);
|
const [student_statuses, setStudentStatuses] = useState<any>([]);
|
||||||
// Student Status List
|
// Student Status List
|
||||||
const StudentStatusList = useQuery({
|
const StudentStatusList = useQuery({
|
||||||
|
@ -190,6 +191,7 @@ export default function Home() {
|
||||||
},
|
},
|
||||||
onSuccess: (data: StudentStatusListReturnType) => {
|
onSuccess: (data: StudentStatusListReturnType) => {
|
||||||
if (data[1]) {
|
if (data[1]) {
|
||||||
|
// Circle generation for students in a study group
|
||||||
// We first flatten the data to remove nested entries
|
// We first flatten the data to remove nested entries
|
||||||
let flattened_data = data[1].map((item) => ({
|
let flattened_data = data[1].map((item) => ({
|
||||||
active: item.active,
|
active: item.active,
|
||||||
|
@ -200,30 +202,77 @@ export default function Home() {
|
||||||
study_group: "",
|
study_group: "",
|
||||||
subject: item.subject,
|
subject: item.subject,
|
||||||
user: item.user,
|
user: item.user,
|
||||||
|
weight: 1,
|
||||||
}));
|
}));
|
||||||
|
// Dummy data
|
||||||
flattened_data.push({
|
flattened_data.push({
|
||||||
active: true,
|
active: true,
|
||||||
distance: 50,
|
distance: 50,
|
||||||
landmark: "",
|
landmark: "",
|
||||||
latitude: 9.498298904115586,
|
latitude: 8.498837,
|
||||||
longitude: 125.59552381187677,
|
longitude: 124.595422,
|
||||||
study_group: "",
|
study_group: "",
|
||||||
subject: "Introduction to Computing",
|
subject: "Introduction to Computing",
|
||||||
user: "Keannu",
|
user: "Dummy",
|
||||||
|
weight: 1,
|
||||||
});
|
});
|
||||||
// We get each unique subject
|
// We get each unique subject
|
||||||
let unique_subjects = [
|
let unique_subjects = [
|
||||||
...new Set(flattened_data.map((item) => item.subject)),
|
...new Set(flattened_data.map((item) => item.subject)),
|
||||||
];
|
];
|
||||||
console.log("Unique Subjects:", unique_subjects);
|
|
||||||
let result: any[] = [];
|
let result: any[] = [];
|
||||||
// Then append all entries belonging to that subject to its own array
|
// Then append all entries belonging to that subject to its own array
|
||||||
unique_subjects.forEach((subject) => {
|
unique_subjects.forEach((subject, index: number) => {
|
||||||
|
index++;
|
||||||
let filteredData = flattened_data.filter(
|
let filteredData = flattened_data.filter(
|
||||||
(item) => item.subject === subject && item.study_group === ""
|
(item) => item.subject === subject && item.study_group === ""
|
||||||
);
|
);
|
||||||
// We then concatenate this into the final array
|
/*console.log(
|
||||||
result = result.concat([filteredData]);
|
"Subject #",
|
||||||
|
index,
|
||||||
|
"-",
|
||||||
|
filteredData[0].subject,
|
||||||
|
filteredData
|
||||||
|
);*/
|
||||||
|
// We get the circle radius based on the furthest point
|
||||||
|
let circle_radius = Math.max(
|
||||||
|
...filteredData.map((item) => item.distance)
|
||||||
|
);
|
||||||
|
console.log(
|
||||||
|
"Radius of circle:",
|
||||||
|
Math.max(...filteredData.map((item) => item.distance))
|
||||||
|
);
|
||||||
|
// We get the circle's center by averaging all the points
|
||||||
|
// Calculate the average latitude and longitude
|
||||||
|
const totalLat = filteredData.reduce(
|
||||||
|
(sum, point) => sum + point.latitude,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
const totalLng = filteredData.reduce(
|
||||||
|
(sum, point) => sum + point.longitude,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
|
const avgLat = totalLat / filteredData.length;
|
||||||
|
const avgLng = totalLng / filteredData.length;
|
||||||
|
|
||||||
|
console.log("Center Latitude:", avgLat);
|
||||||
|
console.log("Center Longitude:", avgLng);
|
||||||
|
// We now build the object
|
||||||
|
const subjectUserMap: any = {};
|
||||||
|
filteredData.forEach((item) => {
|
||||||
|
if (!subjectUserMap["users"]) {
|
||||||
|
subjectUserMap["users"] = [];
|
||||||
|
}
|
||||||
|
subjectUserMap["subject"] = item.subject;
|
||||||
|
subjectUserMap["latitude"] = avgLat;
|
||||||
|
subjectUserMap["longitude"] = avgLng;
|
||||||
|
subjectUserMap["radius"] = circle_radius;
|
||||||
|
subjectUserMap["users"].push(item.user);
|
||||||
|
});
|
||||||
|
console.log(subjectUserMap);
|
||||||
|
|
||||||
|
result = result.concat([subjectUserMap]);
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Final Result:", result);
|
console.log("Final Result:", result);
|
||||||
|
@ -258,7 +307,6 @@ export default function Home() {
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
mapType="none"
|
|
||||||
scrollEnabled={true}
|
scrollEnabled={true}
|
||||||
zoomEnabled={true}
|
zoomEnabled={true}
|
||||||
toolbarEnabled={false}
|
toolbarEnabled={false}
|
||||||
|
@ -275,19 +323,19 @@ export default function Home() {
|
||||||
loadingBackgroundColor={colors.secondary_2}
|
loadingBackgroundColor={colors.secondary_2}
|
||||||
>
|
>
|
||||||
{student_statuses.map((student_status: any, index: number) => {
|
{student_statuses.map((student_status: any, index: number) => {
|
||||||
console.log("TEST INDEX", index, student_status);
|
const randomColorWithOpacity = `rgba(${Math.floor(
|
||||||
|
Math.random() * 256
|
||||||
|
)}, ${Math.floor(Math.random() * 256)}, ${Math.floor(
|
||||||
|
Math.random() * 256
|
||||||
|
)}, 0.7)`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Polygon
|
<>
|
||||||
key={index}
|
<Marker
|
||||||
coordinates={student_status}
|
coordinate={student_status}
|
||||||
fillColor="red"
|
pinColor={randomColorWithOpacity}
|
||||||
strokeColor="50"
|
zIndex={1000}
|
||||||
zIndex={100}
|
|
||||||
tappable
|
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
const users = student_statuses.map((item: any) => ({
|
|
||||||
user: item.user,
|
|
||||||
}));
|
|
||||||
toast.hideAll();
|
toast.hideAll();
|
||||||
toast.show(
|
toast.show(
|
||||||
<View
|
<View
|
||||||
|
@ -298,15 +346,15 @@ export default function Home() {
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Text style={styles.text_white_tiny_bold}>
|
<Text style={styles.text_white_tiny_bold}>
|
||||||
Subject: {student_status[0].subject}
|
Subject: {student_status.subject}
|
||||||
</Text>
|
</Text>
|
||||||
<Text style={styles.text_white_tiny_bold}>
|
<Text style={styles.text_white_tiny_bold}>
|
||||||
Students Studying: {student_status.length}
|
Students Studying: {student_status.users.length}
|
||||||
</Text>
|
</Text>
|
||||||
<Button
|
<Button
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
toast.show("Toledo para ma konsehal", {
|
toast.show("Joined successfully", {
|
||||||
type: "warning",
|
type: "success",
|
||||||
placement: "top",
|
placement: "top",
|
||||||
duration: 2000,
|
duration: 2000,
|
||||||
animationType: "slide-in",
|
animationType: "slide-in",
|
||||||
|
@ -332,16 +380,15 @@ export default function Home() {
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
<Circle
|
||||||
|
key={index}
|
||||||
|
center={student_status}
|
||||||
|
radius={student_status.radius}
|
||||||
|
fillColor={randomColorWithOpacity}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
<UrlTile
|
|
||||||
urlTemplate={urlProvider}
|
|
||||||
shouldReplaceMapContent={true}
|
|
||||||
maximumZ={19}
|
|
||||||
flipY={false}
|
|
||||||
zIndex={1}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Marker
|
<Marker
|
||||||
coordinate={{
|
coordinate={{
|
||||||
latitude: location.coords.latitude,
|
latitude: location.coords.latitude,
|
||||||
|
|
Loading…
Reference in a new issue