Polished APIs in preparation for online deployment

This commit is contained in:
keannu125 2023-03-10 12:55:54 +08:00
parent e5f048e14f
commit 91db76cb50
2 changed files with 130 additions and 121 deletions

View file

@ -17,88 +17,85 @@ import Register from "./Routes/Register/Register";
const queryClient = new QueryClient(); const queryClient = new QueryClient();
const router = createBrowserRouter( const router = createBrowserRouter([
[ {
{ path: "/",
path: "/", element: (
element: ( <Container>
<Container> <Dashboard />
<Dashboard /> </Container>
</Container> ),
), errorElement: (
errorElement: ( <Container>
<Container> <Error />
<Error /> </Container>
</Container> ),
), },
}, {
{ path: "/Products",
path: "/Products", element: (
element: ( <Container>
<Container> <Products />
<Products /> </Container>
</Container> ),
), },
}, {
{ path: "/Inventory",
path: "/Inventory", element: (
element: ( <Container>
<Container> <Inventory />
<Inventory /> </Container>
</Container> ),
), },
}, {
{ path: "/Logs",
path: "/Logs", element: (
element: ( <Container>
<Container> <Logs />
<Logs /> </Container>
</Container> ),
), },
}, {
{ path: "/Login",
path: "/Login", element: (
element: ( <Container>
<Container> <Login />
<Login /> </Container>
</Container> ),
), },
}, {
{ path: "/Product/:id",
path: "/Product/:id", element: (
element: ( <Container>
<Container> <Product />
<Product /> </Container>
</Container> ),
), },
}, {
{ path: "/Activation/:uid/:token",
path: "/Activation/:uid/:token", element: (
element: ( <Container>
<Container> <Activation />
<Activation /> </Container>
</Container> ),
), },
}, {
{ path: "/NewProduct",
path: "/NewProduct", element: (
element: ( <Container>
<Container> <NewProduct />
<NewProduct /> </Container>
</Container> ),
), },
}, {
{ path: "/Register",
path: "/Register", element: (
element: ( <Container>
<Container> <Register />
<Register /> </Container>
</Container> ),
), },
}, ]);
],
{ basename: "Ivy" }
);
export default function App() { export default function App() {
return ( return (

View file

@ -10,105 +10,122 @@ import {
// Product APIs // Product APIs
// Local Testing "http://localhost:8000" // Local Testing "http://localhost:8000"
// Remote Deploy "https://keannu125.pythonanywhere.com" // Remote Deploy "https://keannu125.pythonanywhere.com"
export const baseurl = "https://keannu125.pythonanywhere.com"; axios.defaults.baseURL = "https://keannu125.pythonanywhere.com";
const token = JSON.parse(localStorage.getItem("token") || "{}");
export function GetProducts() { export function GetProducts() {
const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.get(baseurl + "/api/v1/products/", { .get("/api/v1/products/", {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.then((response) => { .then((response) => {
console.log("Queried products successfully");
return response.data; return response.data;
})
.catch((error) => {
console.log("Error querying products");
return error;
}); });
} }
export function GetProduct(id: number) { export function GetProduct(id: number) {
const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.get(baseurl + "/api/v1/products/" + id + "/", { .get("/api/v1/products/" + id + "/", {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.then((response) => { .then((response) => {
console.log("Queried product successfully");
return response.data; return response.data;
})
.catch((error) => {
console.log("Error querying product");
return error;
}); });
} }
export function UpdateProduct(product: UpdateProductParams) { export function UpdateProduct(product: UpdateProductParams) {
const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.patch(baseurl + "/api/v1/products/" + product.id + "/", product, { .patch("/api/v1/products/" + product.id + "/", product, {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.then((response) => { .then((response) => {
console.log("Product update successful", response.data); console.log("Product update successful");
return response.data; return response.data;
}) })
.catch((error) => { .catch((error) => {
console.log("Error updating product", error.response); console.log("Error updating product");
return error; return error;
}); });
} }
export function GetLowestStockedProduct() { export function GetLowestStockedProduct() {
const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.get(baseurl + "/api/v1/lowest_stock_product/", { .get("/api/v1/lowest_stock_product/", {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.then((response) => { .then((response) => {
console.log("Queried lowest stocekd product successfully");
return response.data; return response.data;
})
.catch((error) => {
console.log("Error querying lowest stocked product");
}); });
} }
export function GetLogs() { export function GetLogs() {
const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.get(baseurl + "/api/v1/logs/", { .get("/api/v1/logs/", {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.then((response) => { .then((response) => {
console.log("Queried logs successfully");
return response.data; return response.data;
})
.catch((error) => {
console.log("Error querying logs");
return error;
}); });
} }
export function AddProduct(note: AddProductParams) { export function AddProduct(note: AddProductParams) {
const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.post(baseurl + "/api/v1/products/", note, { .post("/api/v1/products/", note, {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.then((response) => { .then((response) => {
console.log("Added product successfully");
return response.data; return response.data;
}) })
.catch((error) => { .catch((error) => {
console.log("Error adding product", error.response); console.log("Error adding product");
return error; return error;
}); });
} }
export function DeleteProduct(id: number) { export function DeleteProduct(id: number) {
const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.delete(baseurl + "/api/v1/products/" + id + "/", { .delete("/api/v1/products/" + id + "/", {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.then((response) => {
console.log("Deleted product successfully");
return response;
})
.catch((error) => { .catch((error) => {
console.log("Error deleting product", error.response); console.log("Error deleting product");
return error; return error;
}); });
} }
@ -117,79 +134,74 @@ export function DeleteProduct(id: number) {
export function UserRegister(register: RegistrationParams) { export function UserRegister(register: RegistrationParams) {
return axios return axios
.post(baseurl + "/api/v1/accounts/users/", register) .post("/api/v1/accounts/users/", register)
.then(async (response) => { .then(async (response) => {
console.log(response.data); console.log("Registration success");
return true; return true;
}) })
.catch((error) => { .catch((error) => {
console.log("Registration failed: " + error.response); console.log("Registration failed");
return false; return false;
}); });
} }
export function UserLogin(user: LoginParams) { export function UserLogin(user: LoginParams) {
return axios return axios
.post(baseurl + "/api/v1/accounts/token/login/", user) .post("/api/v1/accounts/token/login/", user)
.then(async (response) => { .then(async (response) => {
localStorage.setItem("token", JSON.stringify(response.data.auth_token)); localStorage.setItem("token", JSON.stringify(response.data.auth_token));
console.log( console.log("Login Success!");
"Login Success! Stored Token: ",
JSON.parse(localStorage.getItem("token") || "{}")
);
return true; return true;
}) })
.catch((error) => { .catch((error) => {
console.log("Login Failed: " + error.response); console.log("Login Failed");
return false; return false;
}); });
} }
export function UserInfo() { export function UserInfo() {
const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.get(baseurl + "/api/v1/accounts/users/me/", { .get("/api/v1/accounts/users/me/", {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.then((response) => { .then((response) => {
console.log(response.data); console.log("Success querying self info");
return response.data; return response.data;
}) })
.catch((error) => { .catch((error) => {
console.log("Error retrieving user data", error.response); console.log("Error querying self info");
return false; return false;
}); });
} }
export function QueryUser(id: number) { export function QueryUser(id: number) {
const token = JSON.parse(localStorage.getItem("token") || "{}");
return axios return axios
.get(baseurl + "/api/v1/user_list/" + id, { .get("/api/v1/user_list/" + id, {
headers: { headers: {
Authorization: "Token " + token, Authorization: "Token " + token,
}, },
}) })
.then((response) => { .then((response) => {
console.log("Querying one user...", response.data); console.log("Success querying user");
return response.data; return response.data;
}) })
.catch((error) => { .catch((error) => {
console.log("Error retrieving single user data", error.response); console.log("Error querying user");
return false; return false;
}); });
} }
export function UserActivate(activation: ActivationParams) { export function UserActivate(activation: ActivationParams) {
return axios return axios
.post(baseurl + "/api/v1/accounts/users/activation/", activation) .post("/api/v1/accounts/users/activation/", activation)
.then(async (response) => { .then(async (response) => {
console.log("Activation Success"); console.log("Activation Success");
return true; return true;
}) })
.catch((error) => { .catch((error) => {
console.log("Activation failed: " + error.response); console.log("Activation failed");
return false; return false;
}); });
} }