Refactor frontend components and API interactions

Removed redundant API endpoint and added reusable hooks. Implemented various UI component updates including loading spinner, file upload form, and machine selector. Improved state management in page and layout components and introduced new request handling functionalities.
This commit is contained in:
2024-10-24 16:14:20 +02:00
parent ee127f431c
commit bfe49f65ec
22 changed files with 1676 additions and 163 deletions

View File

@@ -0,0 +1,154 @@
"use client"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { z } from "zod"
import { Button } from "../ui/button"
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "../ui/form"
import { Input } from "../ui/input"
import { FilesApi } from 'apps/frontend/src/requests/files';
import {
MultipleMachinesSelector
} from 'apps/frontend/src/components/forms/machines-selector';
import MultipleSelector, { Option } from 'apps/frontend/src/components/ui/multiple-selector';
import { MachinesApi } from 'apps/frontend/src/requests/machines';
import { Loader } from 'lucide-react';
import React from 'react';
async function getMachines(value: string): Promise<Option[]> {
try {
const machines = await MachinesApi.get.all();
console.log(machines.length);
const filtered = machines.filter((machine) => machine.machineName && machine.machineName.toLowerCase().includes(value.toLowerCase()));
console.log(filtered.length);
const mapped = filtered.map((machine) => ({
label: machine.machineName,
value: machine.id,
}));
return mapped;
} catch (error) {
console.error('Erreur lors de la récupération des machines:', error);
return [];
}
}
const machinesSchema = z.object({
label: z.string(),
value: z.string(),
disable: z.boolean().optional(),
});
const fileUploadSchema = z.object({
fileName: z.string().min(2, {
message: "Le nom du fichier doit faire au moins faire 2 carractères.",
}).max(128, {
message: "Le nom du fichier ne peux pas faire plus de 128 carractères."
}),
author: z.string().min(2, {
message: "Votre pseudonyme doit faire au moins faire 2 carractères."
}).max(64, {
message: "Votre pseudonyme ne peux pas faire plus de 64 carractères."
}),
machinesId: z.array(machinesSchema).min(1, {
message: "Vous devez indiqué au moins une machine."
})
})
export function FileUploadForm() {
const executeUpload = FilesApi.post.upload;
const form = useForm<z.infer<typeof fileUploadSchema>>({
resolver: zodResolver(fileUploadSchema),
})
function onSubmit(data: z.infer<typeof fileUploadSchema>) {
console.log(data)
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="w-2/3 space-y-6">
<FormField
control={form.control}
name="fileName"
render={({ field }) => (
<FormItem>
<FormLabel>Nom du fichier</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormDescription>
Le nom qui sera affiché lors d'une recherche.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="machinesId"
render={({ field }) => (
<FormItem>
<FormLabel>Associer à des machines</FormLabel>
<FormControl>
<MultipleSelector
{...field}
onSearch={async (value) => {
const res = await getMachines(value);
return res;
}}
triggerSearchOnFocus
placeholder="Cliquez pour chercher."
loadingIndicator={
<div>
<Loader className={"animate-spin h-4 w-4 mr-2"} />
<p className="py-2 text-center text-lg leading-10 text-muted-foreground">Chargement...</p>
</div>
}
emptyIndicator={
<p
className="w-full text-center text-lg leading-10 text-muted-foreground">
Auccun résultats
</p>
}
/>
</FormControl>
<FormDescription>
Machine(s) qui seront associé(s) à ce fichier.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="author"
render={({ field }) => (
<FormItem>
<FormLabel>Votre pseudonyme ou nom.</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormDescription>
Votre nom d'affichage qui sera lié au fichier.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Submit</Button>
</form>
</Form>
)
}