mirror of
https://github.com/suitenumerique/drive.git
synced 2026-09-23 01:55:11 +02:00
✨(front) add methods to drivers
In order to prepare the form to create folder we need to add methods.
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user