2023-07-03 21:22:31 +08:00
|
|
|
import axios from "axios";
|
|
|
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
|
|
import {
|
2023-08-14 23:29:53 +08:00
|
|
|
ActivationType,
|
|
|
|
LoginType,
|
|
|
|
OnboardingType,
|
|
|
|
PatchUserInfoType,
|
|
|
|
RegistrationType,
|
|
|
|
StudentStatusType,
|
2023-07-03 21:22:31 +08:00
|
|
|
} from "../../interfaces/Interfaces";
|
|
|
|
|
2023-07-27 00:06:32 +08:00
|
|
|
export let backendURL = "https://stude.keannu1.duckdns.org";
|
|
|
|
export let backendURLWebsocket = "ws://stude.keannu1.duckdns.org";
|
|
|
|
if (__DEV__) {
|
2023-09-20 17:42:37 +08:00
|
|
|
backendURL = "http://10.0.10.8:8083";
|
|
|
|
backendURLWebsocket = "ws://10.0.10.8:8083";
|
2023-07-03 21:22:31 +08:00
|
|
|
}
|
|
|
|
|
2023-07-27 00:09:43 +08:00
|
|
|
// Switch this on if you wanna run production URLs while in development
|
2023-09-06 18:13:43 +08:00
|
|
|
let use_production = false;
|
2023-08-06 14:25:09 +08:00
|
|
|
if (__DEV__ && use_production) {
|
2023-07-27 00:09:43 +08:00
|
|
|
backendURL = "https://stude.keannu1.duckdns.org";
|
|
|
|
backendURLWebsocket = "ws://stude.keannu1.duckdns.org";
|
|
|
|
}
|
|
|
|
|
2023-07-03 21:22:31 +08:00
|
|
|
const instance = axios.create({
|
|
|
|
baseURL: backendURL,
|
|
|
|
timeout: 1000,
|
|
|
|
});
|
|
|
|
|
2023-08-14 21:57:52 +08:00
|
|
|
console.log("Using backend API:", backendURL);
|
2023-07-26 10:03:25 +08:00
|
|
|
|
2023-08-07 15:03:53 +08:00
|
|
|
// 3rd Party APIs
|
|
|
|
export const urlProvider =
|
2023-08-29 16:39:50 +08:00
|
|
|
"https://openstreetmap.keannu1.duckdns.org/tile/{z}/{x}/{y}.png?";
|
2023-07-03 21:22:31 +08:00
|
|
|
// App APIs
|
|
|
|
|
2023-08-15 00:41:42 +08:00
|
|
|
// Error Handling
|
|
|
|
export function ParseError(error: any) {
|
|
|
|
if (error.response && error.response.data) {
|
|
|
|
return JSON.stringify(error.response.data)
|
|
|
|
.replaceAll(/[{}()"]/g, " ")
|
|
|
|
.replaceAll(/,/g, "\n")
|
|
|
|
.replaceAll("[", "")
|
|
|
|
.replaceAll("]", "")
|
|
|
|
.replaceAll(".", "")
|
|
|
|
.replaceAll(/"/g, "")
|
|
|
|
.replaceAll("non_field_errors", "")
|
|
|
|
.trim();
|
|
|
|
}
|
|
|
|
return "Unable to reach server";
|
|
|
|
}
|
|
|
|
|
2023-07-04 14:18:48 +08:00
|
|
|
// Token Handling
|
|
|
|
export async function getAccessToken() {
|
|
|
|
const accessToken = await AsyncStorage.getItem("access_token");
|
|
|
|
return accessToken;
|
|
|
|
}
|
2023-07-03 21:22:31 +08:00
|
|
|
|
2023-07-04 14:18:48 +08:00
|
|
|
export async function getRefreshToken() {
|
|
|
|
const refreshToken = await AsyncStorage.getItem("refresh_token");
|
|
|
|
return refreshToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setAccessToken(access: string) {
|
|
|
|
await AsyncStorage.setItem("access_token", access);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setRefreshToken(refresh: string) {
|
|
|
|
await AsyncStorage.setItem("refresh_token", refresh);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-17 18:45:25 +08:00
|
|
|
// Header Config Template for REST
|
|
|
|
export async function GetConfig() {
|
|
|
|
const accessToken = await getAccessToken();
|
|
|
|
return {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${accessToken}`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-04 14:18:48 +08:00
|
|
|
// User APIs
|
2023-08-14 23:29:53 +08:00
|
|
|
export function UserRegister(register: RegistrationType) {
|
2023-07-03 21:22:31 +08:00
|
|
|
console.log(JSON.stringify(register));
|
|
|
|
return instance
|
|
|
|
.post("/api/v1/accounts/users/", register)
|
|
|
|
.then(async (response) => {
|
2023-07-04 14:34:31 +08:00
|
|
|
return [true, response.status];
|
2023-07-03 21:22:31 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-07-06 15:37:22 +08:00
|
|
|
return [false, error_message];
|
2023-07-03 21:22:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:29:53 +08:00
|
|
|
export function UserLogin(user: LoginType) {
|
2023-07-03 21:22:31 +08:00
|
|
|
return instance
|
2023-07-03 23:19:36 +08:00
|
|
|
.post("/api/v1/accounts/jwt/create/", user)
|
2023-07-03 21:22:31 +08:00
|
|
|
.then(async (response) => {
|
2023-07-04 14:18:48 +08:00
|
|
|
setAccessToken(response.data.access);
|
|
|
|
setRefreshToken(response.data.refresh);
|
2023-07-04 17:00:22 +08:00
|
|
|
return [true];
|
2023-07-03 21:22:31 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-07-06 15:37:22 +08:00
|
|
|
return [false, error_message];
|
2023-07-03 21:22:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-04 14:18:48 +08:00
|
|
|
export async function TokenRefresh() {
|
|
|
|
const refresh = await getRefreshToken();
|
|
|
|
// console.log("Refresh token", refresh);
|
2023-07-03 23:19:36 +08:00
|
|
|
return instance
|
2023-07-04 14:18:48 +08:00
|
|
|
.post("/api/v1/accounts/jwt/refresh/", {
|
|
|
|
refresh: refresh,
|
|
|
|
})
|
2023-07-03 23:19:36 +08:00
|
|
|
.then(async (response) => {
|
2023-07-04 14:18:48 +08:00
|
|
|
setAccessToken(response.data.access);
|
2023-07-06 15:23:01 +08:00
|
|
|
return true;
|
2023-07-03 23:19:36 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-07-06 15:23:01 +08:00
|
|
|
return false;
|
2023-07-03 23:19:36 +08:00
|
|
|
});
|
|
|
|
}
|
2023-08-14 23:29:53 +08:00
|
|
|
export async function GetUserInfo() {
|
2023-07-17 18:45:25 +08:00
|
|
|
const config = await GetConfig();
|
2023-07-03 21:22:31 +08:00
|
|
|
return instance
|
2023-07-17 18:45:25 +08:00
|
|
|
.get("/api/v1/accounts/users/me/", config)
|
2023-07-03 21:22:31 +08:00
|
|
|
.then((response) => {
|
2023-07-04 14:26:49 +08:00
|
|
|
// console.log(JSON.stringify(response.data));
|
2023-07-06 20:29:04 +08:00
|
|
|
return [true, response.data];
|
2023-07-04 14:18:48 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-07-06 15:37:22 +08:00
|
|
|
return [false, error_message];
|
2023-07-03 21:22:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:29:53 +08:00
|
|
|
export async function PatchUserInfo(info: PatchUserInfoType) {
|
2023-07-17 18:45:25 +08:00
|
|
|
const config = await GetConfig();
|
|
|
|
return instance
|
|
|
|
.patch("/api/v1/accounts/users/me/", info, config)
|
|
|
|
.then((response) => {
|
|
|
|
console.log(JSON.stringify(response.data));
|
|
|
|
return [true, response.data];
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-07-17 18:45:25 +08:00
|
|
|
return [false, error_message];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:29:53 +08:00
|
|
|
export function UserActivate(activation: ActivationType) {
|
2023-07-03 21:22:31 +08:00
|
|
|
return instance
|
|
|
|
.post("/api/v1/accounts/users/activation/", activation)
|
2023-08-14 23:29:53 +08:00
|
|
|
.then(() => {
|
2023-07-03 21:22:31 +08:00
|
|
|
return true;
|
|
|
|
})
|
2023-08-14 23:29:53 +08:00
|
|
|
.catch(() => {
|
2023-07-03 21:22:31 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2023-07-04 21:25:57 +08:00
|
|
|
|
|
|
|
// App APIs
|
2023-07-06 17:19:19 +08:00
|
|
|
|
|
|
|
export async function GetCourses() {
|
|
|
|
const accessToken = await getAccessToken();
|
|
|
|
return instance
|
|
|
|
.get("/api/v1/courses/", {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${accessToken}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
// console.log(JSON.stringify(response.data));
|
2023-07-17 15:57:23 +08:00
|
|
|
return [true, response.data];
|
2023-07-06 17:19:19 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-07-17 15:57:23 +08:00
|
|
|
return [false, error_message];
|
2023-07-06 17:19:19 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function GetSemesters() {
|
|
|
|
const accessToken = await getAccessToken();
|
|
|
|
return instance
|
|
|
|
.get("/api/v1/semesters/", {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${accessToken}`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
// console.log(JSON.stringify(response.data));
|
2023-07-17 15:57:23 +08:00
|
|
|
return [true, response.data];
|
2023-07-06 17:19:19 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-07-17 15:57:23 +08:00
|
|
|
return [false, error_message];
|
2023-07-06 17:19:19 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function GetYearLevels() {
|
2023-07-17 18:45:25 +08:00
|
|
|
const config = await GetConfig();
|
2023-07-06 17:19:19 +08:00
|
|
|
return instance
|
2023-07-17 18:45:25 +08:00
|
|
|
.get("/api/v1/year_levels/", config)
|
2023-07-06 17:19:19 +08:00
|
|
|
.then((response) => {
|
2023-07-18 20:51:43 +08:00
|
|
|
// console.log(JSON.stringify(response.data));
|
2023-07-17 15:57:23 +08:00
|
|
|
return [true, response.data];
|
2023-07-06 17:19:19 +08:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-07-17 15:57:23 +08:00
|
|
|
return [false, error_message];
|
2023-07-06 17:19:19 +08:00
|
|
|
});
|
|
|
|
}
|
2023-07-06 20:29:04 +08:00
|
|
|
|
2023-07-27 00:01:44 +08:00
|
|
|
export async function GetSubjects() {
|
2023-07-17 22:44:50 +08:00
|
|
|
const config = await GetConfig();
|
2023-07-27 00:01:44 +08:00
|
|
|
return instance
|
|
|
|
.get("/api/v1/subjects/", config)
|
|
|
|
.then((response) => {
|
|
|
|
return [true, response.data];
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-07-27 00:01:44 +08:00
|
|
|
return [false, error_message];
|
|
|
|
});
|
2023-07-17 22:44:50 +08:00
|
|
|
}
|
|
|
|
|
2023-08-10 17:23:12 +08:00
|
|
|
export async function GetStudentStatus() {
|
|
|
|
const config = await GetConfig();
|
|
|
|
return instance
|
|
|
|
.get("/api/v1/student_status/self/", config)
|
|
|
|
.then((response) => {
|
|
|
|
return [true, response.data];
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-08-10 17:23:12 +08:00
|
|
|
return [false, error_message];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:29:53 +08:00
|
|
|
export async function PatchStudentStatus(info: StudentStatusType) {
|
2023-08-06 14:25:09 +08:00
|
|
|
const config = await GetConfig();
|
|
|
|
console.log(info);
|
|
|
|
return instance
|
2023-08-07 14:22:47 +08:00
|
|
|
.patch("/api/v1/student_status/self/", info, config)
|
2023-08-06 14:25:09 +08:00
|
|
|
.then((response) => {
|
|
|
|
return [true, response.data];
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-08-15 00:41:42 +08:00
|
|
|
let error_message = ParseError(error);
|
2023-08-06 14:25:09 +08:00
|
|
|
return [false, error_message];
|
|
|
|
});
|
|
|
|
}
|
2023-08-15 14:15:33 +08:00
|
|
|
|
|
|
|
export async function GetStudentStatusList() {
|
|
|
|
const config = await GetConfig();
|
|
|
|
return instance
|
|
|
|
.get("/api/v1/student_status/list/", config)
|
|
|
|
.then((response) => {
|
|
|
|
return [true, response.data];
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
let error_message = ParseError(error);
|
|
|
|
return [false, error_message];
|
|
|
|
});
|
|
|
|
}
|
2023-09-06 18:13:43 +08:00
|
|
|
|
|
|
|
export async function GetStudentStatusListFiltered() {
|
|
|
|
const config = await GetConfig();
|
|
|
|
return instance
|
|
|
|
.get("/api/v1/student_status/filter/near_student_status", config)
|
|
|
|
.then((response) => {
|
|
|
|
return [true, response.data];
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
let error_message = ParseError(error);
|
|
|
|
return [false, error_message];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// To-do
|
|
|
|
export async function GetStudentStatusListFilteredCurrentLocation() {
|
|
|
|
const config = await GetConfig();
|
|
|
|
return instance
|
|
|
|
.get("/api/v1/student_status/list/", config)
|
|
|
|
.then((response) => {
|
|
|
|
return [true, response.data];
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
let error_message = ParseError(error);
|
|
|
|
return [false, error_message];
|
|
|
|
});
|
|
|
|
}
|