|
1 | 1 | import React, { useEffect } from 'react'; |
2 | 2 | import { ChartTypes } from '@/gql/generated/graphql'; |
3 | 3 | import { Button, Checkbox, Select, Text, TextField } from 'opub-ui'; |
4 | | - |
5 | 4 | import { ChartData, ResourceData, ResourceSchema } from '../types'; |
6 | 5 |
|
7 | 6 | interface ChartFormProps { |
@@ -45,6 +44,20 @@ const ChartForm: React.FC<ChartFormProps> = ({ |
45 | 44 | } |
46 | 45 | }, [chartData.options.yAxisColumn]); |
47 | 46 |
|
| 47 | + |
| 48 | + |
| 49 | + // useEffect(() => { |
| 50 | + // if ( |
| 51 | + // !chartData.filters || |
| 52 | + // chartData.filters.length === 0 |
| 53 | + // ) { |
| 54 | + // handleChange('filters', { |
| 55 | + // ...chartData, |
| 56 | + // // filters: [{ column: '', operator: '==', value: '' }], |
| 57 | + // }); |
| 58 | + // } |
| 59 | + // }, [chartData.filters]); |
| 60 | + |
48 | 61 | const handleYAxisColumnChange = ( |
49 | 62 | index: number, |
50 | 63 | field: string, |
@@ -82,6 +95,33 @@ const ChartForm: React.FC<ChartFormProps> = ({ |
82 | 95 | }); |
83 | 96 | }; |
84 | 97 |
|
| 98 | + // const handlefilterColumnChange = ( |
| 99 | + // index: number, |
| 100 | + // field: string, |
| 101 | + // value: any |
| 102 | + // ) => { |
| 103 | + // const currentFilters = Array.isArray(chartData.filters) ? chartData.filters : []; |
| 104 | + // const newFiltersColumns = [...currentFilters]; |
| 105 | + // newFiltersColumns[index] = { |
| 106 | + // ...newFiltersColumns[index], |
| 107 | + // [field]: value, |
| 108 | + // }; |
| 109 | + // handleChange('filters', newFiltersColumns); // Changed this line |
| 110 | + // }; |
| 111 | + // const addFilterColumn = () => { |
| 112 | + // const currentFilters = Array.isArray(chartData.filters) ? chartData.filters : []; |
| 113 | + // const newFiltersColumns = [ |
| 114 | + // ...currentFilters, |
| 115 | + // { column: '', operator: '==', value: '' }, |
| 116 | + // ]; |
| 117 | + // handleChange('filters', newFiltersColumns); |
| 118 | + // }; |
| 119 | + |
| 120 | + // const removeFilterColumn = (index: number) => { |
| 121 | + // const currentFilters = Array.isArray(chartData.filters) ? chartData.filters : []; |
| 122 | + // const newFiltersColumns = currentFilters.filter((_, i) => i !== index); |
| 123 | + // handleChange('filters', newFiltersColumns); |
| 124 | + // }; |
85 | 125 | const updateChartData = (field: string, value: any) => { |
86 | 126 | if (field === 'type') { |
87 | 127 | const newData = { |
@@ -233,7 +273,6 @@ const ChartForm: React.FC<ChartFormProps> = ({ |
233 | 273 |
|
234 | 274 | {(isBarOrLineChart || isGroupedChart) && ( |
235 | 275 | <div className="flex flex-row flex-wrap justify-between gap-4"> |
236 | | - |
237 | 276 | <div className="flex flex-col gap-4 "> |
238 | 277 | {chartData?.options?.yAxisColumn?.map((column, index) => ( |
239 | 278 | <div |
@@ -293,7 +332,7 @@ const ChartForm: React.FC<ChartFormProps> = ({ |
293 | 332 | ))} |
294 | 333 | </div> |
295 | 334 | {isGroupedChart && ( |
296 | | - <Button className="h-fit w-fit mt-4" onClick={addYAxisColumn}> |
| 335 | + <Button className="mt-4 h-fit w-fit" onClick={addYAxisColumn}> |
297 | 336 | Add Y-axis Column |
298 | 337 | </Button> |
299 | 338 | )} |
@@ -340,6 +379,71 @@ const ChartForm: React.FC<ChartFormProps> = ({ |
340 | 379 | /> |
341 | 380 | </> |
342 | 381 | )} |
| 382 | + |
| 383 | + {/* <div className="flex flex-row flex-wrap justify-between gap-4"> |
| 384 | + <div className="flex flex-col gap-4 "> |
| 385 | + {Array.isArray(chartData?.filters) && chartData?.filters?.map((column, index) => ( |
| 386 | + <div |
| 387 | + key={index} |
| 388 | + className="flex flex-wrap items-end gap-4 lg:flex-nowrap" |
| 389 | + > |
| 390 | + <Select |
| 391 | + name={`Column-${index}`} |
| 392 | + options={resourceSchema.map((field) => ({ |
| 393 | + label: field.fieldName, |
| 394 | + value: field.id, |
| 395 | + }))} |
| 396 | + label="Column" |
| 397 | + className={`w-36`} |
| 398 | + value={column.fieldName} |
| 399 | + onChange={(e) => handlefilterColumnChange(index, 'column', e)} |
| 400 | + onBlur={() => handleSave(chartData)} |
| 401 | + placeholder="Select" |
| 402 | + /> |
| 403 | + <Select |
| 404 | + name={`operator-${index}`} |
| 405 | + className={`w-36`} |
| 406 | + options={[ |
| 407 | + { label: 'Equal to', value: '==' }, |
| 408 | + { label: 'Not Equal to', value: '!=' }, |
| 409 | + { label: 'Less than', value: '<' }, |
| 410 | + { label: 'Greater than', value: '>' }, |
| 411 | + { label: 'In', value: 'in' }, |
| 412 | + { label: 'Not In', value: 'not in' }, |
| 413 | + { label: 'Less than or Equal to', value: '<=' }, |
| 414 | + { label: 'Greater than or Equal to', value: '>=' }, |
| 415 | +
|
| 416 | + ]} |
| 417 | + label="Operator" |
| 418 | + value={column.operator} |
| 419 | + defaultValue="Equal to" |
| 420 | + onChange={(e) => handlefilterColumnChange(index, 'operator', e)} |
| 421 | + onBlur={() => handleSave(chartData)} |
| 422 | + /> |
| 423 | +
|
| 424 | + <TextField |
| 425 | + name={`Value-${index}`} |
| 426 | + value={column.value} |
| 427 | + label="Value" |
| 428 | + onChange={(e: any) => { |
| 429 | + handlefilterColumnChange(index, 'value', e); |
| 430 | + }} |
| 431 | + onBlur={() => handleSave(chartData)} |
| 432 | + /> |
| 433 | + { index > 0 && ( |
| 434 | + <Button onClick={() => removeFilterColumn(index)}> |
| 435 | + Remove |
| 436 | + </Button> |
| 437 | + )} |
| 438 | + </div> |
| 439 | + ))} |
| 440 | + </div> |
| 441 | + { ( |
| 442 | + <Button className="mt-4 h-fit w-fit" onClick={addFilterColumn}> |
| 443 | + Add Filter Column |
| 444 | + </Button> |
| 445 | + )} |
| 446 | + </div> */} |
343 | 447 | </div> |
344 | 448 | ); |
345 | 449 | }; |
|
0 commit comments