Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Sort columns properly in downloaded files #870

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ You can also check the [release page](https://github.com/visualize-admin/visuali
- Enhancements:
- Cube Checker:
- Introduced a Temporal dimensions scaleType & timeFormat checks
- Data download:
- Order of columns in downloaded files now matches the order visible in the dataset preview (based on shacl:order)
- Bug fixes:
- Correctly retrieve min & max values for Temporal dimensions when scaleType is not equal to Interval

Expand Down
4 changes: 3 additions & 1 deletion app/components/data-download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { OperationResult, useClient } from "urql";

import { QueryFilters } from "@/charts/shared/chart-helpers";
import { DataSource } from "@/configurator";
import { getSortedColumns } from "@/configurator/components/datatable";
import { useLocale } from "@/src";

import { Observation } from "../domain/data";
Expand Down Expand Up @@ -84,6 +85,7 @@ export type FileFormat = typeof FILE_FORMATS[number];
const makeColumnLabel = (dim: DimensionMetadataFragment) => {
return `${dim.label}${dim.unit ? ` (${dim.unit})` : ""}`;
};

const prepareData = ({
dimensions,
measures,
Expand All @@ -93,7 +95,7 @@ const prepareData = ({
measures: DimensionMetadataFragment[];
observations: Observation[];
}) => {
const columns = keyBy([...dimensions, ...measures], (d) => d.iri);
const columns = keyBy(getSortedColumns(dimensions, measures), (d) => d.iri);
const data = observations.map((obs) =>
Object.keys(obs).reduce((acc, key) => {
const col = columns[key];
Expand Down
11 changes: 6 additions & 5 deletions app/configurator/components/datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const DataSetPreviewTable = ({
observations: Observation[];
}) => {
const headers = useMemo(() => {
return getSortedHeaders(dimensions, measures);
return getSortedColumns(dimensions, measures);
}, [dimensions, measures]);

if (observations) {
Expand Down Expand Up @@ -231,7 +231,8 @@ export const DataSetTable = ({
if (!data?.dataCubeByIri) {
return [];
}
return getSortedHeaders(

return getSortedColumns(
data.dataCubeByIri.dimensions,
data.dataCubeByIri.measures
);
Expand All @@ -252,13 +253,13 @@ export const DataSetTable = ({
}
};

function getSortedHeaders(
export const getSortedColumns = (
dimensions: DimensionMetadataFragment[],
measures: DimensionMetadataFragment[]
) {
) => {
const allDimensions = [...dimensions, ...measures];
allDimensions.sort((a, b) =>
ascending(a.order ?? Infinity, b.order ?? Infinity)
);
return allDimensions;
}
};