(front) add methods to drivers

In order to prepare the form to create folder we need to add methods.
This commit is contained in:
Nathan Vasse
2025-03-11 16:07:49 +01:00
committed by NathanVss
parent 5eab16f77b
commit 446c034580
4 changed files with 52 additions and 7 deletions
@@ -1,5 +1,7 @@
import { Item } from "./types";
import { Item, ItemType } from "./types";
export abstract class Driver {
abstract getItems(path: string): Promise<Item[]>;
abstract getItems(filters?: { type?: ItemType }): Promise<Item[]>;
abstract getItem(id: string): Promise<Item>;
abstract createFolder(data: { title: string }): Promise<Item>;
}
@@ -6,9 +6,28 @@ export class DummyDriver extends Driver {
return [
{
id: "1",
name: "Mon Espace",
title: "Mon Espace",
type: ItemType.FOLDER,
lastUpdate: new Date().toISOString(),
},
];
}
async getItem(id: string): Promise<Item> {
return {
id: "1",
title: "Mon Espace",
type: ItemType.FOLDER,
lastUpdate: new Date().toISOString(),
};
}
async createFolder(data: { title: string }): Promise<Item> {
return {
id: "1",
title: data.title,
type: ItemType.FOLDER,
lastUpdate: new Date().toISOString(),
};
}
}
@@ -1,11 +1,35 @@
import { fetchAPI } from "@/features/api/fetchApi";
import { Driver } from "../Driver";
import { Item } from "../types";
import { Item, ItemType } from "../types";
export class StandardDriver extends Driver {
async getItems(): Promise<Item[]> {
const response = await fetchAPI(`items/`);
async getItems(filters = {}): Promise<Item[]> {
const response = await fetchAPI(`items/`, {
params: filters,
});
const data = await response.json();
return data.results;
}
async getItem(id: string): Promise<Item> {
const response = await fetchAPI(`items/${id}/`);
const data = await response.json();
return data;
}
async createFolder(data: {
title: string;
parentId?: string;
}): Promise<Item> {
const { parentId, ...rest } = data;
const response = await fetchAPI(`items/${parentId}/children/`, {
method: "POST",
body: JSON.stringify({
...rest,
type: ItemType.FOLDER,
}),
});
const item = await response.json();
return item;
}
}
@@ -5,7 +5,7 @@ export enum ItemType {
export type Item = {
id: string;
name: string;
title: string;
type: ItemType;
lastUpdate: string;
};