Code cleanup for multiple pages and components

This commit is contained in:
Keannu Bernasol 2023-09-20 21:16:54 +08:00
parent 14e14b8bb6
commit 68778cea7a
5 changed files with 22 additions and 13 deletions

View file

@ -1,26 +1,26 @@
import { Callout } from "react-native-maps";
import { RawLocationType } from "../../interfaces/Interfaces";
import { LocationType, RawLocationType } from "../../interfaces/Interfaces";
import styles from "../../styles";
import { Text } from "react-native";
// Map popup for user's location
type props = {
location: RawLocationType;
location: LocationType;
studying: boolean;
subject?: string;
};
export default function CustomMapCallout(props: props) {
let { location, studying, subject } = props;
if (location && location.coords) {
if (location && location.latitude && location.longitude) {
if (studying) {
return (
<Callout>
<Text style={styles.text_black_tiny}>
You are here {"\n"}
X: {Math.round(location.coords.longitude) + "\n"}
Z: {Math.round(location.coords.latitude) + "\n"}
X: {Math.round(location.longitude) + "\n"}
Z: {Math.round(location.latitude) + "\n"}
Studying: {subject}
</Text>
</Callout>
@ -30,8 +30,8 @@ export default function CustomMapCallout(props: props) {
<Callout>
<Text style={styles.text_black_tiny}>
You are here {"\n"}
X: {Math.round(location.coords.longitude) + "\n"}
Z: {Math.round(location.coords.latitude)}
X: {Math.round(location.longitude) + "\n"}
Z: {Math.round(location.latitude)}
</Text>
</Callout>
);

View file

@ -44,7 +44,7 @@ import GetDistanceFromUSTP from "../../components/GetDistance/GetDistanceFromUST
export default function Home() {
// Switch this condition to see the main map when debugging
const map_debug = false;
const map_debug = true;
const navigation = useNavigation<RootDrawerParamList>();
const [location, setLocation] = useState<RawLocationType | null>(null);
const [dist, setDist] = useState<number | null>(null);
@ -119,6 +119,7 @@ export default function Home() {
const [studying, setStudying] = useState(false);
const [subject, setSubject] = useState("");
const [buttonLabel, setButtonLabel] = useState("Start studying");
const [student_status, setStudentStatus] = useState<StudentStatusType>();
const StudentStatus = useQuery({
queryKey: ["user_status"],
queryFn: async () => {
@ -140,6 +141,8 @@ export default function Home() {
} else if (data[1].active == false) {
setButtonLabel("Start Studying");
}
setStudentStatus(data[1]);
console.log(student_status);
},
onError: (error: Error) => {
toast.show(String(error), {
@ -373,7 +376,7 @@ export default function Home() {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
}}
draggable
draggable={student_status?.active}
onDragEnd={(e) => {
const newLocation = e.nativeEvent.coordinate;
const distance = GetDistance(
@ -402,7 +405,10 @@ export default function Home() {
pinColor={colors.primary_1}
>
<CustomMapCallout
location={location}
location={{
latitude: location.coords.latitude,
longitude: location.coords.longitude,
}}
studying={studying}
subject={subject}
/>

View file

@ -8,6 +8,7 @@ import {
RootDrawerParamList,
StudentStatusType,
StudentStatusReturnType,
StudentStatusPatchType,
} from "../../interfaces/Interfaces";
import Button from "../../components/Button/Button";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
@ -61,7 +62,7 @@ export default function StartStudying({ route }: any) {
});
const mutation = useMutation({
mutationFn: async (info: StudentStatusType) => {
mutationFn: async (info: StudentStatusPatchType) => {
const data = await PatchStudentStatus(info);
if (data[0] == false) {
return Promise.reject(new Error(JSON.stringify(data[1])));

View file

@ -9,6 +9,7 @@ import {
OptionType,
StudentStatusType,
PatchUserInfoType,
StudentStatusPatchType,
} from "../../interfaces/Interfaces";
import Button from "../../components/Button/Button";
import { Image } from "react-native";
@ -33,7 +34,7 @@ export default function SubjectsPage() {
// Student Status
const studentstatus_mutation = useMutation({
mutationFn: async (info: StudentStatusType) => {
mutationFn: async (info: StudentStatusPatchType) => {
const data = await PatchStudentStatus(info);
if (data[0] != true) {
return Promise.reject(new Error());

View file

@ -18,6 +18,7 @@ import {
OptionType,
StudentStatusType,
PatchUserInfoType,
StudentStatusPatchType,
} from "../../interfaces/Interfaces";
import Button from "../../components/Button/Button";
import { Image } from "react-native";
@ -50,7 +51,7 @@ export default function UserInfoPage() {
// Student Status
const studentstatus_mutation = useMutation({
mutationFn: async (info: StudentStatusType) => {
mutationFn: async (info: StudentStatusPatchType) => {
const data = await PatchStudentStatus(info);
if (data[0] != true) {
return Promise.reject(new Error());