Skip to content

Commit

Permalink
Merge pull request #22 from kscalelabs/second_milestone_serhii
Browse files Browse the repository at this point in the history
feature_collection_api_integration
  • Loading branch information
Serhii Ofii authored Sep 6, 2024
2 parents bf29edb + 831d895 commit 400d57e
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 271 deletions.
162 changes: 18 additions & 144 deletions frontend/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
import axios, { AxiosInstance } from "axios";

export interface Part {
description: string;
owner: string;
images: Image[];
part_id: string;
part_name: string;
}

export interface Bom {
part_id: string;
quantity: number;
}
import { AxiosInstance } from "axios";
import { Collection } from "types/model";

export interface Image {
filename: string;
s3_url: string;
}

export interface Robot {
robot_id: string;
name: string;
export interface CollectionCreateFragment {
title: string;
description: string;
owner: string;
bom: Bom[];
images: Image[];
}

export class api {
public api: AxiosInstance;

Expand All @@ -50,131 +33,22 @@ export class api {
return { s3_url: "", filename: "" };
}
}
public async getUserById(userId: string | undefined): Promise<string> {
const response = await this.api.get(`/users/${userId}`);
return response.data.email;
public async createCollection(
data: CollectionCreateFragment,
): Promise<Collection | null> {
const response = await this.api.post("/create_collection", data);
return response.data;
}
public async getRobots(): Promise<Robot[]> {
try {
const response = await this.api.get("/robots/");
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error("Error fetching robots:", error.response?.data);
throw new Error(
error.response?.data?.detail || "Error fetching robots",
);
} else {
console.error("Unexpected error:", error);
throw new Error("Unexpected error");
}
}
public async getCollection(id: string): Promise<Collection | null> {
const response = await this.api.get(`/get_collection?id=${id}`);
return response.data;
}
public async getYourRobots(): Promise<Robot[]> {
try {
const response = await this.api.get("/robots/your/");
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error("Error fetching robots:", error.response?.data);
throw new Error(
error.response?.data?.detail || "Error fetching robots",
);
} else {
console.error("Unexpected error:", error);
throw new Error("Unexpected error");
}
}
}
public async getRobotById(robotId: string | undefined): Promise<Robot> {
try {
const response = await this.api.get(`/robots/${robotId}`);
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error("Error fetching robot:", error.response?.data);
throw new Error(error.response?.data?.detail || "Error fetching robot");
} else {
console.error("Unexpected error:", error);
throw new Error("Unexpected error");
}
}
public async editCollection(data: Collection): Promise<null> {
await this.api.post(`/edit_collection`, data);
return null;
}
public async getPartById(partId: string | undefined): Promise<Part> {
try {
const response = await this.api.get(`/parts/${partId}`);
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error("Error fetching robot:", error.response?.data);
throw new Error(error.response?.data?.detail || "Error fetching robot");
} else {
console.error("Unexpected error:", error);
throw new Error("Unexpected error");
}
}
}
public async addRobot(robot: Robot): Promise<void> {
const s = robot.name;
try {
await this.api.post("/robots/add/", robot);
} catch (error) {
if (axios.isAxiosError(error)) {
console.error("Error adding robot:", error.response?.data);
throw new Error(
error.response?.data?.detail || "Error adding robot " + s,
);
} else {
console.error("Unexpected error:", error);
throw new Error("Unexpected error");
}
}
}
public async getParts(): Promise<Part[]> {
try {
const response = await this.api.get("/parts/");
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error("Error fetching parts:", error.response?.data);
throw new Error(error.response?.data?.detail || "Error fetching parts");
} else {
console.error("Unexpected error:", error);
throw new Error("Unexpected error");
}
}
}
public async getYourParts(): Promise<Part[]> {
try {
const response = await this.api.get("/parts/your/");
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error("Error fetching parts:", error.response?.data);
throw new Error(error.response?.data?.detail || "Error fetching parts");
} else {
console.error("Unexpected error:", error);
throw new Error("Unexpected error");
}
}
}
public async addPart(part: Part): Promise<void> {
const s = part.part_name;
try {
await this.api.post("/parts/add/", part);
} catch (error) {
if (axios.isAxiosError(error)) {
console.error(
"Error adding part:" + part.part_name,
error.response?.data,
);
throw new Error(
error.response?.data?.detail || "Error adding part " + s,
);
} else {
console.error("Unexpected error:", error);
throw new Error("Unexpected error");
}
}
public async getAllCollections(): Promise<Array<Collection> | null> {
const response = await this.api.get(`/get_collections`);
return response.data;
}
}
2 changes: 1 addition & 1 deletion frontend/src/components/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CardItem: React.FC<Collection> = (collectionProps) => {
};

return (
<div className="relative m-4 group">
<div className="relative m-8 group">
<div className="relative z-10 w-52 h-72 p-3 bg-rose-100 dark:bg-gray-900 border flex flex-col gap-2 opacity-95">
<h3 className="text-lg font-bold">{title}</h3>
<p className="text-sm text-gray-900 dark:text-gray-100">
Expand Down
Loading

0 comments on commit 400d57e

Please sign in to comment.