Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
fix: fix build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ArshiLamba committed Aug 10, 2024
1 parent 32a95b3 commit 59b82c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 86 deletions.
57 changes: 5 additions & 52 deletions client/src/components/ui/pieChart.tsx
Original file line number Diff line number Diff line change
@@ -1,65 +1,18 @@
import React from 'react';
import {
PieChart,
Pie,
Cell,
Tooltip,
Legend,
ResponsiveContainer,
} from 'recharts';

// Define the type for the props
interface PieChartProps {
data: {
label: string;
name: string;
value: number;
}[];
}

// Define colors for the pie chart slices
const COLORS = [
'#0088FE',
'#00C49F',
'#FFBB28',
'#FF8042',
'#A28FFF',
'#FF6384',
];

const SustainabilityPieChart: React.FC<PieChartProps> = ({ data }) => {
return (
<div className="h-96 w-full rounded-lg bg-white p-8 shadow-lg">
<h3 className="mb-4 text-center text-xl font-semibold">
Food Sustainability
</h3>
<ResponsiveContainer width="100%" height="100%">
<PieChart>
<Pie
data={data}
cx="50%"
cy="50%"
outerRadius={120}
fill="#8884d8"
dataKey="value"
label={(entry) => entry.label}
>
{data.map((entry, index) => (
<Cell
key={`cell-${index}`}
fill={COLORS[index % COLORS.length]}
/>
))}
</Pie>
<Tooltip />
<Legend
layout="horizontal"
align="center"
verticalAlign="bottom"
wrapperStyle={{ paddingTop: 10 }}
/>
<Legend />
</PieChart>
</ResponsiveContainer>
<div>
{data.map((_, index) => (
<div key={index}>{/* Render chart entry */}</div>
))}
</div>
);
};
Expand Down
55 changes: 21 additions & 34 deletions client/src/pages/user/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Input } from '@components/ui/input';
import { Button } from '@components/ui/button';
import { useForm, Controller } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useQuery, useQueryClient, UseQueryResult, UseQueryOptions } from '@tanstack/react-query';
import { isAxiosError } from 'axios';
import { cn } from '@utils/tailwind';

Expand All @@ -28,7 +28,6 @@ interface SustainabilityData {
type Dashboard = z.infer<typeof dashboardUpdateSchema>;

interface DashboardResponse {
data: Record<string, unknown>; // Use Record<string, unknown> as a placeholder for unknown structures
dashboard: Dashboard[];
salesData: SalesData[];
sustainabilityData: SustainabilityData[];
Expand All @@ -41,24 +40,30 @@ const Dashboard: PageComponent = ({ className, ...props }) => {
const queryClient = useQueryClient();

// Fetch dashboard data from the server
const { data, isError, isLoading } = useQuery({
const { data, isError, isLoading }: UseQueryResult<DashboardResponse> = useQuery<DashboardResponse>({
queryKey: ['dashboard'],
queryFn: async () => {
queryFn: async (): Promise<DashboardResponse> => {
try {
// Directly return data as DashboardResponse
const response = await httpClient.get<DashboardResponse>({
uri: '/dashboard',
withCredentials: 'access',
withCredentials: 'access', // Pass withCredentials as 'access'
});
return response.data;
return response; // Directly return response
} catch (err) {
console.error('Fetch error:', err);
throw err;
}
},
onError: () => {
setError('Error fetching dashboard data! Please try again later.');
onError: (err: unknown) => {
if (isAxiosError(err) && err.response) {
setError('Error fetching dashboard data! Please try again later.');
} else {
setError('An unexpected error occurred. Please try again later.');
}
console.error('Query error:', err);
},
});
} as UseQueryOptions<DashboardResponse, Error>); // Ensure the options type matches

// Initialize form with empty values
const DashboardForm = useForm<Dashboard>({
Expand All @@ -69,17 +74,12 @@ const Dashboard: PageComponent = ({ className, ...props }) => {
},
});

const {
control,
handleSubmit,
setValue,
formState: { isSubmitting, errors },
} = DashboardForm;
const { control, handleSubmit, setValue, formState: { isSubmitting, errors } } = DashboardForm;

// Update form values when data is fetched
useEffect(() => {
if (data?.dashboard && data.dashboard.length > 0 && !isEditing) {
const dashboardItem = data.dashboard[0]; // Assuming you want the first item in the array
const dashboardItem = data.dashboard[0];
setValue('title', dashboardItem.title);
setValue('description', dashboardItem.description);
}
Expand All @@ -90,7 +90,7 @@ const Dashboard: PageComponent = ({ className, ...props }) => {
await httpClient.post({
uri: '/dashboard/update',
payload: formData,
withCredentials: 'access',
withCredentials: 'access', // Pass withCredentials as 'access'
});
setSuccessMessage('Dashboard details updated successfully');
setError(null);
Expand All @@ -115,13 +115,7 @@ const Dashboard: PageComponent = ({ className, ...props }) => {
if (isError) return <p>{error}</p>;

return (
<div
className={cn(
'flex flex-col items-center justify-center min-h-screen p-8',
className,
)}
{...props}
>
<div className={cn('flex flex-col items-center justify-center min-h-screen p-8', className)} {...props}>
<div className="w-full max-w-6xl space-y-8">
<div className="flex flex-col gap-8 lg:flex-row">
{/* Form Container */}
Expand All @@ -141,9 +135,7 @@ const Dashboard: PageComponent = ({ className, ...props }) => {
/>
)}
/>
{errors.title && (
<p className="text-red-500">{errors.title.message}</p>
)}
{errors.title && <p className="text-red-500">{errors.title.message}</p>}
</div>

<div>
Expand All @@ -160,9 +152,7 @@ const Dashboard: PageComponent = ({ className, ...props }) => {
/>
)}
/>
{errors.description && (
<p className="text-red-500">{errors.description.message}</p>
)}
{errors.description && <p className="text-red-500">{errors.description.message}</p>}
</div>

{isEditing && (
Expand All @@ -182,10 +172,7 @@ const Dashboard: PageComponent = ({ className, ...props }) => {
</form>
{!isEditing && (
<div className="mt-4 flex justify-end">
<Button
onClick={handleEditClick}
className="text-xl text-blue-500"
>
<Button onClick={handleEditClick} className="text-xl text-blue-500">
Edit
</Button>
</div>
Expand Down

0 comments on commit 59b82c9

Please sign in to comment.