import { Button, FormContainer, Input, Notification, Select, toast } from '@/components/ui' import { Field, FieldArray, Form, Formik, FieldProps } from 'formik' import { FaMinus, FaPlus, FaTimes, FaSave } from 'react-icons/fa' import { SelectBoxOption } from '@/types/shared' import { columnSummaryTypeListOptions } from '../admin/listForm/edit/options' import { ChartSeriesDto } from '@/proxy/admin/charts/models' import { useLocalization } from '@/utils/hooks/useLocalization' import { useStoreState } from '@/store/store' import { object, array, string } from 'yup' import { SummaryTypeEnum } from '@/proxy/form/models' import { useState, useEffect } from 'react' interface ChartDrawerProps { open: boolean onClose: () => void initialSeries: ChartSeriesDto[] fieldList: SelectBoxOption[] onSave: (series: ChartSeriesDto[]) => void onPreviewChange: (series: ChartSeriesDto[]) => void } const schema = object().shape({ series: array().of( object().shape({ name: string().required('Name Required'), argumentField: string().required('Argument Field Required'), valueField: string().required('Value Field Required'), summaryType: string().required('Summary Type Required'), }), ), }) const ChartDrawer = ({ open, onClose, initialSeries, fieldList, onSave, onPreviewChange, }: ChartDrawerProps) => { const { translate } = useLocalization() const { userName } = useStoreState((s) => s.auth.user) const [selectedSeriesIndex, setSelectedSeriesIndex] = useState(-1) // Chart type icons const chartTypeIcons = [ { type: 'line', label: 'Line', icon: '📈' }, { type: 'bar', label: 'Bar', icon: '📊' }, { type: 'area', label: 'Area', icon: '🏔️' }, { type: 'scatter', label: 'Scatter', icon: '🔵' }, { type: 'spline', label: 'Spline', icon: '〰️' }, { type: 'stackedbar', label: 'Stacked Bar', icon: '📚' }, { type: 'stackedarea', label: 'Stacked Area', icon: '⛰️' }, ] const newSeriesValue = () => { return { index: -1, type: 'line', name: '', argumentField: '', valueField: '', summaryType: SummaryTypeEnum.Sum, axis: '', barOverlapGroup: '', barPadding: 0, barWidth: 0, color: '', cornerRadius: 0, dashStyle: 'solid', ignoreEmptyPoints: false, pane: '', rangeValue1Field: '', rangeValue2Field: '', selectionMode: 'none', showInLegend: true, visible: true, width: 2, label: { visible: true, backgroundColor: '#f05b41', customizeText: '', format: 'decimal', font: { color: '#FFFFFF', family: '"Segoe UI", "Helvetica Neue", "Trebuchet MS", Verdana, sans-serif', size: 12, weight: 400, }, }, userId: userName ?? '', } } if (!open) return null return ( <> {/* Drawer */}
0 ? initialSeries : [newSeriesValue()], }} validationSchema={schema} onSubmit={async (values, { setSubmitting }) => { try { await onSave(values.series) toast.push(Chart kaydedildi, { placement: 'top-end', }) onClose() } catch (error: any) { toast.push( Hata {error} , { placement: 'top-end' }, ) } finally { setSubmitting(false) } }} > {({ setFieldValue, values, isSubmitting }) => { // Preview'ı her değişiklikte otomatik güncelle useEffect(() => { const validSeries = values.series.filter( (s) => s.argumentField && s.valueField && s.summaryType, ) if (validSeries.length > 0) { onPreviewChange(validSeries) } }, [values.series]) return (
{/* Header */}

📊 Chart Series

{/* Add Series Button */}
{/* Scrollable Content */}
{({ remove }) => (
{values.series.map((series, index) => (
{/* Header */}
Seri #{index + 1}
{/* Chart Type Selector */}
{({ field, form }: FieldProps) => (
{selectedSeriesIndex === index && (
{chartTypeIcons.map((chartType) => ( ))}
)}
)}
{/* Name */}
{/* Argument Field */}
{({ field, form }: FieldProps) => ( option.value === field.value, )} onChange={(option) => { form.setFieldValue(field.name, option?.value) }} placeholder="Select field..." /> )}
{/* Summary Type */}
{({ field, form }: FieldProps) => (