import { useState } from 'react'; import { useMutation } from '@tanstack/react-query'; import config from '../config'; export function Import() { const [file, setFile] = useState(null); const importMutation = useMutation({ mutationFn: async (formData: FormData) => { const response = await fetch(`${config.apiUrl}/products/import`, { method: 'POST', body: formData, }); if (!response.ok) { throw new Error('Import failed'); } return response.json(); }, }); const handleFileChange = (e: React.ChangeEvent) => { if (e.target.files?.[0]) { setFile(e.target.files[0]); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!file) return; const formData = new FormData(); formData.append('file', file); importMutation.mutate(formData); }; return (

Import Products

The CSV file should contain the following columns: sku, name, description (optional), category (optional)

{importMutation.isSuccess && (

Successfully imported {importMutation.data.imported} products

)} {importMutation.isError && (

Error importing products: {importMutation.error.message}

)}
); }