mirror of
https://github.com/lemeow125/EquipmentTracker-Frontend.git
synced 2024-11-16 21:59:24 +08:00
Update API endpoint
This commit is contained in:
parent
4389b21a39
commit
2c4ec9a8d6
1 changed files with 342 additions and 342 deletions
|
@ -1,342 +1,342 @@
|
||||||
/* eslint-disable react-refresh/only-export-components */
|
/* eslint-disable react-refresh/only-export-components */
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import {
|
import {
|
||||||
ActivationType,
|
ActivationType,
|
||||||
EquipmentListType,
|
EquipmentListType,
|
||||||
LoginType,
|
LoginType,
|
||||||
RegisterType,
|
RegisterType,
|
||||||
ResetPasswordConfirmType,
|
ResetPasswordConfirmType,
|
||||||
EquipmentInstanceListType,
|
EquipmentInstanceListType,
|
||||||
EquipmentType,
|
EquipmentType,
|
||||||
AddEquipmentType,
|
AddEquipmentType,
|
||||||
AddEquipmentInstanceType,
|
AddEquipmentInstanceType,
|
||||||
EquipmentInstanceType,
|
EquipmentInstanceType,
|
||||||
PatchEquipmentInstanceType,
|
PatchEquipmentInstanceType,
|
||||||
PatchEquipmentType,
|
PatchEquipmentType,
|
||||||
EquipmentLogListType,
|
EquipmentLogListType,
|
||||||
EquipmentInstanceLogListType,
|
EquipmentInstanceLogListType,
|
||||||
} from "../Types/Types";
|
} from "../Types/Types";
|
||||||
|
|
||||||
const debug = false;
|
const debug = false;
|
||||||
let backendURL;
|
let backendURL;
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
backendURL = "http://localhost:8000/";
|
backendURL = "http://localhost:8000/";
|
||||||
} else {
|
} else {
|
||||||
backendURL = "https://equipment-tracker-backend.keannu1.duckdns.org/";
|
backendURL = "https://api.equipment-tracker.06222001.xyz/";
|
||||||
}
|
}
|
||||||
|
|
||||||
const instance = axios.create({
|
const instance = axios.create({
|
||||||
baseURL: backendURL,
|
baseURL: backendURL,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Token Handling
|
// Token Handling
|
||||||
export async function getAccessToken() {
|
export async function getAccessToken() {
|
||||||
const accessToken = await localStorage.getItem("access_token");
|
const accessToken = await localStorage.getItem("access_token");
|
||||||
return accessToken;
|
return accessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRefreshToken() {
|
export async function getRefreshToken() {
|
||||||
const refreshToken = await localStorage.getItem("refresh_token");
|
const refreshToken = await localStorage.getItem("refresh_token");
|
||||||
return refreshToken;
|
return refreshToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setAccessToken(access: string) {
|
export async function setAccessToken(access: string) {
|
||||||
await localStorage.setItem("access_token", access);
|
await localStorage.setItem("access_token", access);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setRefreshToken(refresh: string) {
|
export async function setRefreshToken(refresh: string) {
|
||||||
await localStorage.setItem("refresh_token", refresh);
|
await localStorage.setItem("refresh_token", refresh);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Header Config Template for REST
|
// Header Config Template for REST
|
||||||
export async function GetConfig() {
|
export async function GetConfig() {
|
||||||
const accessToken = await getAccessToken();
|
const accessToken = await getAccessToken();
|
||||||
return {
|
return {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${accessToken}`,
|
Authorization: `Bearer ${accessToken}`,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ParseError(error: { response: { data: string } }) {
|
export function ParseError(error: { response: { data: string } }) {
|
||||||
if (error.response && error.response.data) {
|
if (error.response && error.response.data) {
|
||||||
if (error.response.data.length > 50) {
|
if (error.response.data.length > 50) {
|
||||||
return "Error truncated (too long)";
|
return "Error truncated (too long)";
|
||||||
}
|
}
|
||||||
return JSON.stringify(error.response.data)
|
return JSON.stringify(error.response.data)
|
||||||
.replace(/[{}]/g, " ")
|
.replace(/[{}]/g, " ")
|
||||||
.replace(/\(/g, " ")
|
.replace(/\(/g, " ")
|
||||||
.replace(/\)/g, " ")
|
.replace(/\)/g, " ")
|
||||||
.replace(/"/g, " ")
|
.replace(/"/g, " ")
|
||||||
.replace(/,/g, ",")
|
.replace(/,/g, ",")
|
||||||
.replace(/\[/g, "")
|
.replace(/\[/g, "")
|
||||||
.replace(/\]/g, "")
|
.replace(/\]/g, "")
|
||||||
.replace(/\./g, "")
|
.replace(/\./g, "")
|
||||||
.replace(/non_field_errors/g, "")
|
.replace(/non_field_errors/g, "")
|
||||||
.trim();
|
.trim();
|
||||||
}
|
}
|
||||||
return "Unable to reach server";
|
return "Unable to reach server";
|
||||||
}
|
}
|
||||||
// User APIs
|
// User APIs
|
||||||
|
|
||||||
export function RegisterAPI(info: RegisterType) {
|
export function RegisterAPI(info: RegisterType) {
|
||||||
return instance
|
return instance
|
||||||
.post("api/v1/accounts/users/", info)
|
.post("api/v1/accounts/users/", info)
|
||||||
.then(async (response) => {
|
.then(async (response) => {
|
||||||
console.log(response.data);
|
console.log(response.data);
|
||||||
return [true, 0];
|
return [true, 0];
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Registration failed");
|
console.log("Registration failed");
|
||||||
return [false, ParseError(error)];
|
return [false, ParseError(error)];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function LoginAPI(user: LoginType, remember_session: boolean) {
|
export function LoginAPI(user: LoginType, remember_session: boolean) {
|
||||||
return instance
|
return instance
|
||||||
.post("api/v1/accounts/jwt/create/", user)
|
.post("api/v1/accounts/jwt/create/", user)
|
||||||
.then(async (response) => {
|
.then(async (response) => {
|
||||||
console.log(response.data);
|
console.log(response.data);
|
||||||
setAccessToken(response.data.access);
|
setAccessToken(response.data.access);
|
||||||
if (remember_session) {
|
if (remember_session) {
|
||||||
setRefreshToken(response.data.refresh);
|
setRefreshToken(response.data.refresh);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Login Success");
|
console.log("Login Success");
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Login Failed", error.response.data);
|
console.log("Login Failed", error.response.data);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function JWTRefreshAPI() {
|
export async function JWTRefreshAPI() {
|
||||||
const refresh = await getRefreshToken();
|
const refresh = await getRefreshToken();
|
||||||
return instance
|
return instance
|
||||||
.post("api/v1/accounts/jwt/refresh/", {
|
.post("api/v1/accounts/jwt/refresh/", {
|
||||||
refresh: refresh,
|
refresh: refresh,
|
||||||
})
|
})
|
||||||
.then(async (response) => {
|
.then(async (response) => {
|
||||||
setAccessToken(response.data.access);
|
setAccessToken(response.data.access);
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Error refreshing token");
|
console.log("Error refreshing token");
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function UserAPI() {
|
export async function UserAPI() {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.get("api/v1/accounts/users/me/", config)
|
.get("api/v1/accounts/users/me/", config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return response.data;
|
return response.data;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Error retrieving user data");
|
console.log("Error retrieving user data");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ActivationAPI(activation: ActivationType) {
|
export function ActivationAPI(activation: ActivationType) {
|
||||||
return instance
|
return instance
|
||||||
.post("api/v1/accounts/users/activation/", activation)
|
.post("api/v1/accounts/users/activation/", activation)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log("Activation Success");
|
console.log("Activation Success");
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Activation failed");
|
console.log("Activation failed");
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
export function ResetPasswordAPI(email: string) {
|
export function ResetPasswordAPI(email: string) {
|
||||||
return instance
|
return instance
|
||||||
.post("api/v1/accounts/users/reset_password/", { email: email })
|
.post("api/v1/accounts/users/reset_password/", { email: email })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log("Activation Success");
|
console.log("Activation Success");
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Activation failed");
|
console.log("Activation failed");
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ResetPasswordConfirmAPI(info: ResetPasswordConfirmType) {
|
export function ResetPasswordConfirmAPI(info: ResetPasswordConfirmType) {
|
||||||
return instance
|
return instance
|
||||||
.post("api/v1/accounts/users/reset_password_confirm/", info)
|
.post("api/v1/accounts/users/reset_password_confirm/", info)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log("Reset Success");
|
console.log("Reset Success");
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Reset failed");
|
console.log("Reset failed");
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equipment APIs
|
// Equipment APIs
|
||||||
|
|
||||||
export async function EquipmentAPI(id: number) {
|
export async function EquipmentAPI(id: number) {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.get(`api/v1/equipments/equipments/${id}/`, config)
|
.get(`api/v1/equipments/equipments/${id}/`, config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return response.data as EquipmentType;
|
return response.data as EquipmentType;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Error retrieving equipment");
|
console.log("Error retrieving equipment");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function EquipmentUpdateAPI(
|
export async function EquipmentUpdateAPI(
|
||||||
equipment: PatchEquipmentType,
|
equipment: PatchEquipmentType,
|
||||||
id: number
|
id: number
|
||||||
) {
|
) {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.patch(`api/v1/equipments/equipments/${id}/`, equipment, config)
|
.patch(`api/v1/equipments/equipments/${id}/`, equipment, config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return [true, response.data as EquipmentType];
|
return [true, response.data as EquipmentType];
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Error updating equipment instance");
|
console.log("Error updating equipment instance");
|
||||||
return [false, ParseError(error)];
|
return [false, ParseError(error)];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function EquipmentRemoveAPI(id: number) {
|
export async function EquipmentRemoveAPI(id: number) {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.delete(`api/v1/equipments/equipments/${id}/`, config)
|
.delete(`api/v1/equipments/equipments/${id}/`, config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return [true, response.data as EquipmentType];
|
return [true, response.data as EquipmentType];
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Error deleting equipment instance");
|
console.log("Error deleting equipment instance");
|
||||||
return [false, ParseError(error)];
|
return [false, ParseError(error)];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function EquipmentsAPI() {
|
export async function EquipmentsAPI() {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.get("api/v1/equipments/equipments/", config)
|
.get("api/v1/equipments/equipments/", config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return response.data as EquipmentListType;
|
return response.data as EquipmentListType;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Error retrieving equipments");
|
console.log("Error retrieving equipments");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function EquipmentCreateAPI(equipment: AddEquipmentType) {
|
export async function EquipmentCreateAPI(equipment: AddEquipmentType) {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.post("api/v1/equipments/equipments/", equipment, config)
|
.post("api/v1/equipments/equipments/", equipment, config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return [true, response.data as EquipmentType];
|
return [true, response.data as EquipmentType];
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Error creating equipment");
|
console.log("Error creating equipment");
|
||||||
return [false, ParseError(error)];
|
return [false, ParseError(error)];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function EquipmentLogsAPI() {
|
export async function EquipmentLogsAPI() {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.get("api/v1/equipments/equipments/logs", config)
|
.get("api/v1/equipments/equipments/logs", config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return response.data as EquipmentLogListType;
|
return response.data as EquipmentLogListType;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Error retrieving equipment logs");
|
console.log("Error retrieving equipment logs");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equipment Instances APIs
|
// Equipment Instances APIs
|
||||||
|
|
||||||
export async function EquipmentInstanceLogsAPI() {
|
export async function EquipmentInstanceLogsAPI() {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.get("api/v1/equipments/equipment_instances/logs", config)
|
.get("api/v1/equipments/equipment_instances/logs", config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return response.data as EquipmentInstanceLogListType;
|
return response.data as EquipmentInstanceLogListType;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Error retrieving equipment logs");
|
console.log("Error retrieving equipment logs");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
export async function EquipmentInstanceAPI(id: number) {
|
export async function EquipmentInstanceAPI(id: number) {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.get(`api/v1/equipments/equipment_instances/${id}/`, config)
|
.get(`api/v1/equipments/equipment_instances/${id}/`, config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return response.data as EquipmentInstanceType;
|
return response.data as EquipmentInstanceType;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Error retrieving equipment");
|
console.log("Error retrieving equipment");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function EquipmentInstanceUpdateAPI(
|
export async function EquipmentInstanceUpdateAPI(
|
||||||
item: PatchEquipmentInstanceType,
|
item: PatchEquipmentInstanceType,
|
||||||
id: number
|
id: number
|
||||||
) {
|
) {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.patch(`api/v1/equipments/equipment_instances/${id}/`, item, config)
|
.patch(`api/v1/equipments/equipment_instances/${id}/`, item, config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return [true, response.data as EquipmentInstanceType];
|
return [true, response.data as EquipmentInstanceType];
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Error updating equipment instance");
|
console.log("Error updating equipment instance");
|
||||||
return [false, ParseError(error)];
|
return [false, ParseError(error)];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function EquipmentInstanceRemoveAPI(id: number) {
|
export async function EquipmentInstanceRemoveAPI(id: number) {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.delete(`api/v1/equipments/equipment_instances/${id}/`, config)
|
.delete(`api/v1/equipments/equipment_instances/${id}/`, config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return [true, response.data];
|
return [true, response.data];
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Error deleting equipment instance");
|
console.log("Error deleting equipment instance");
|
||||||
return [false, ParseError(error)];
|
return [false, ParseError(error)];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function EquipmentInstancesAPI() {
|
export async function EquipmentInstancesAPI() {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.get("api/v1/equipments/equipment_instances/", config)
|
.get("api/v1/equipments/equipment_instances/", config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return response.data as EquipmentInstanceListType;
|
return response.data as EquipmentInstanceListType;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
console.log("Error retrieving equipments");
|
console.log("Error retrieving equipments");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function EquipmentInstanceCreateAPI(
|
export async function EquipmentInstanceCreateAPI(
|
||||||
equipment_instance: AddEquipmentInstanceType
|
equipment_instance: AddEquipmentInstanceType
|
||||||
) {
|
) {
|
||||||
const config = await GetConfig();
|
const config = await GetConfig();
|
||||||
return instance
|
return instance
|
||||||
.post("api/v1/equipments/equipment_instances/", equipment_instance, config)
|
.post("api/v1/equipments/equipment_instances/", equipment_instance, config)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
return [true, response.data as EquipmentInstanceType];
|
return [true, response.data as EquipmentInstanceType];
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Error creating equipment instance");
|
console.log("Error creating equipment instance");
|
||||||
return [false, ParseError(error)];
|
return [false, ParseError(error)];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue