Export function in client component
Unanswered
Savannah posted this in #help-forum
SavannahOP
Hi, i would like to know if there is a way to have an export function in a client component. I need to put "use client" inside the folder cause i did a fetch and i also need to have the export function...
my code :
my code :
"use client";
import eyeVariants from "../../data/avatar/eyeVariants.json";
import mouthVariants from "../../data/avatar/mouthVariants.json"
import interfaceAvatar from "../../interfaces/Avatar";
export const generateRandomAvatar = (): {eyes: string; mouth: string } => {
const randomEyes = eyeVariants[Math.floor(Math.random() * eyeVariants.length)];
const randomMouth = mouthVariants[Math.floor(Math.random() * mouthVariants.length)];
return {
eyes: randomEyes,
mouth: randomMouth
}
}
/*Changer type avatar par l'interface*/
export async function updateAvatar(email: string, avatar: any) {
try {
const response = await fetch(`/api/user/avatar/${email}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email, avatar })
});
if (!response.ok) {
throw new Error(`Erreur ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch(error) {
console.error("aaaaaaaaa Erreur lors de la récupération des données de l'avatar :", error);
}
}