Skip to content

Commit f1e3606

Browse files
Merge pull request #114 from devgateway/task/TCDICORE-475/fix-manual-csv-colors
2 parents 26427c2 + a6ba56f commit f1e3606

9 files changed

Lines changed: 42 additions & 15 deletions

File tree

.changeset/odd-pants-admire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devgateway/dvz-wp-commons": patch
3+
---
4+
5+
Fix manual CSV colors

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ WORKDIR /app
1212
FROM base AS builder
1313
ENV NODE_ENV=production
1414

15+
1516
COPY pnpm-lock.yaml /app/pnpm-lock.yaml
1617
COPY pnpm-workspace.yaml /app/pnpm-workspace.yaml
1718
COPY package.json /app/package.json

packages/commons/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"start": "tsc -w"
5959
},
6060
"dependencies": {
61+
"@devgateway/dvz-wp-commons": "workspace:@devgateway/dvz-wp-commons@*",
6162
"@wordpress/api-fetch": "^7.32.0",
6263
"@wordpress/block-editor": "^15.5.0",
6364
"@wordpress/components": "^30.5.0",

packages/commons/src/ChartColors.jsx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export const ChartColors = (props) => {
157157

158158
useEffect(() => {
159159
let nextUseColors = useColors;
160-
if (app != "csv") {
160+
if (app !== "csv") {
161161
// All conditions for coloring by measures
162162
if ((dimension2 == "none" && colorBy === "index" && swap) ||
163163
(dimension1 == "none" && dimension2 == "none")) { //Multi measure colored by first dimension but swapped (Colored by Measure) or dimensionless bar charts
@@ -198,13 +198,26 @@ export const ChartColors = (props) => {
198198

199199
const updateColor = (value, color, colorByMode = null) => {
200200

201-
const newColors = Object.assign({}, manualColors)
201+
const newColors = {
202+
...manualColors,
203+
[app]: {
204+
...manualColors[app],
205+
}
206+
}
202207
if (colorByMode) {
203-
// For CSV colors, nest by colorBy mode
204-
if (!newColors[app][colorByMode]) {
205-
newColors[app][colorByMode] = {}
208+
// For CSV colors, nest by colorBy mode.
209+
// If the key exists as a flat (legacy) entry, remove it to avoid duplicates.
210+
const appColors = newColors[app];
211+
const baseApp = typeof appColors[value] === "string"
212+
? (({ [value]: _removed, ...rest }) => rest)(appColors)
213+
: appColors;
214+
newColors[app] = {
215+
...baseApp,
216+
[colorByMode]: {
217+
...(baseApp[colorByMode] || {}),
218+
[value]: color
219+
}
206220
}
207-
newColors[app][colorByMode][value] = color
208221
} else {
209222
// For non-CSV colors, use flat structure
210223
newColors[app][value] = color
@@ -399,7 +412,7 @@ export const ChartColors = (props) => {
399412
return null
400413
}
401414

402-
const csvColors = () => {
415+
const csvColors = () => {
403416
const data = Papa.parse(csv, {header: true, dynamicTyping: true});
404417
const values = [];
405418

@@ -416,11 +429,12 @@ export const ChartColors = (props) => {
416429

417430
if (manualColors[app] && values) {
418431
return values.map(v => {
419-
// Get the current colorBy mode's color storage, creating it if needed
432+
// Get the current colorBy mode's color storage, falling back to flat structure for legacy data
420433
const colorByColors = manualColors[app][colorBy] || {};
434+
const color = colorByColors[v] !== undefined ? colorByColors[v] : manualColors[app][v];
421435
return <PanelColorSettings
422436
colorSettings={[{
423-
value: colorByColors[v],
437+
value: color,
424438

425439
onChange: (color) => {
426440
if (color) {
@@ -517,7 +531,7 @@ export const ChartColors = (props) => {
517531
{/* CSV CHART*/}
518532

519533
{app == "csv" && <PanelBody initialOpen={false} title={__("Set Colors")}>
520-
{csvColors(colorBy)}
534+
{csvColors()}
521535
</PanelBody>}
522536

523537

plugins/wp-react-blocks-plugin/blocks/charts/Bar.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ const BarOptions = (props) => {
5555
enableGridY,
5656
sort,
5757
sortReverse,
58+
manualColors
5859
}
5960
} = props;
60-
61+
6162
const getSeries = () => {
6263
if (allCategories) {
6364

@@ -127,10 +128,12 @@ const BarOptions = (props) => {
127128
}
128129
const series = app == 'csv' ? getCSVSeries() : getSeries();
129130

131+
132+
130133
return [
131134
<PanelBody initialOpen={false} title={__("Bar Options")}>
132135
<PanelBody initialOpen={false} title={__("Colors")}>
133-
<ChartColors {...props}></ChartColors>
136+
<ChartColors {...props} manualColors={manualColors}></ChartColors>
134137
</PanelBody>
135138
<PanelBody initialOpen={false} title={"Layout"}>
136139
<PanelRow>

plugins/wp-react-blocks-plugin/blocks/charts/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ registerBlockType(BLOCKS_NS + '/chart',
368368
},
369369

370370
manualColors: {
371-
type: 'Object',
371+
type: 'object',
372372
default: {}
373373
},
374374
barPadding: {

plugins/wp-react-blocks-plugin/blocks/format-bar/text-color.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { RichTextToolbarButton, ColorPalette } from '@wordpress/block-editor';
44
import { Popover, Panel, PanelBody, Button } from '@wordpress/components';
55
import { useState } from '@wordpress/element';
66
import { tag as tagIcon } from '@wordpress/icons';
7+
import { BLOCKS_NS } from '@devgateway/dvz-wp-commons'
78

8-
const name = process.env.BLOCKS_NS + '/text-color';
9+
const name = BLOCKS_NS + '/text-color';
910

1011
function Edit(props) {
1112
const { isActive, value, onChange, contentRef } = props;

plugins/wp-react-blocks-plugin/blocks/webpack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module.exports = {
1717
...defaultConfig.plugins,
1818
new Dotenv({
1919
systemvars: true,
20-
safe: true
2120
}),
2221
]
2322
};

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)