Skip to content

Commit

Permalink
- fixed reference access error when both chart & table are disabled
Browse files Browse the repository at this point in the history
- fixed `preview-tabular-data-settings` not fetched for dist
- code refactoring
  • Loading branch information
t83714 committed Jul 31, 2024
1 parent 1dd1221 commit 1a0f48d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 50 deletions.
59 changes: 56 additions & 3 deletions magda-web-client/src/Components/Common/DataPreviewVis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,39 @@ type StateType = {
fileSizeCheckResult: FileSizeCheckResult | null;
};

export function mergeDatasetAndDistributionPreviewSettings(
dist: ParsedDistribution | null,
dataset: ParsedDataset
) {
if (!dist) {
return dist;
}
const datasetPreviewTabularDataSettings =
dataset?.rawData?.aspects?.["preview-tabular-data-settings"];
if (datasetPreviewTabularDataSettings) {
// merge the dataset preview-tabular-data-settings with the distribution preview-tabular-data-settings
const rawDistDataAspect = {
...dist.rawData.aspects,
"preview-tabular-data-settings": {
...datasetPreviewTabularDataSettings,
...(dist?.rawData?.aspects?.["preview-tabular-data-settings"]
? dist.rawData.aspects["preview-tabular-data-settings"]
: {})
}
};
const rawDistData = {
...dist.rawData,
aspects: rawDistDataAspect
};
return {
...dist,
rawData: rawDistData
};
} else {
return { ...dist };
}
}

class DataPreviewVis extends Component<
{
distribution: ParsedDistribution | null;
Expand Down Expand Up @@ -175,10 +208,13 @@ class DataPreviewVis extends Component<
* @param {Array} tabs - Array of tab items
*/
renderTabs(tabs) {
const activeTab = tabs.find(
let activeTab = tabs.find(
(item, i) =>
item.value.toLowerCase() === this.state.visType.toLowerCase()
);
if (!activeTab && tabs.length > 0) {
activeTab = tabs[0];
}

return (
<nav className="tab-navigation">
Expand All @@ -187,7 +223,7 @@ class DataPreviewVis extends Component<
<li key={t.value}>
<button
className={`${t.value.toLowerCase()} au-link ${
t.value.toLowerCase() === activeTab.value
t.value.toLowerCase() === activeTab?.value
? "tab-active"
: null
}`}
Expand All @@ -202,12 +238,29 @@ class DataPreviewVis extends Component<
</li>
))}
</ul>
{activeTab.action}
{activeTab?.action ? activeTab.action : null}
</nav>
);
}

shouldNotShow() {
const distribution = this.props.distribution;
return (
(distribution?.compatiblePreviews?.chart === false ||
distribution?.rawData?.aspects?.[
"preview-tabular-data-settings"
]?.enableChart === false) &&
(distribution?.compatiblePreviews?.table === false ||
distribution?.rawData?.aspects?.[
"preview-tabular-data-settings"
]?.enableTable === false)
);
}

renderByState() {
if (this.shouldNotShow()) {
return null;
}
if (
this.state.fileSizeCheckResult &&
this.state.fileSizeCheckResult.fileSizeCheckStatus !==
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { Component } from "react";
import { Location } from "history";
import DataPreviewVis from "Components/Common/DataPreviewVis";
import DataPreviewVis, {
mergeDatasetAndDistributionPreviewSettings
} from "Components/Common/DataPreviewVis";
import DataPreviewMap from "Components/Common/DataPreviewMap";
import { getPluginExtraVisualisationSections } from "../../../externalPluginComponents";
import { ParsedDistribution, ParsedDataset } from "../../../helpers/record";
Expand All @@ -21,7 +23,7 @@ export default class DatasetPagePreview extends Component<PropsTypes> {
if (!distributions || distributions?.length === 0) {
return null;
}
let dist = distributions.find((d) => {
const dist = distributions.find((d) => {
// Checking if the distribution has a url
if (!(typeof d === "string") && !d.downloadURL && !d.accessURL) {
return null;
Expand All @@ -33,36 +35,7 @@ export default class DatasetPagePreview extends Component<PropsTypes> {
return null;
}

if (dist) {
const datasetPreviewTabularDataSettings =
dataset?.rawData?.aspects?.["preview-tabular-data-settings"];
if (datasetPreviewTabularDataSettings) {
// merge the dataset preview-tabular-data-settings with the distribution preview-tabular-data-settings
const rawDistDataAspect = {
...dist.rawData.aspects,
"preview-tabular-data-settings": {
...datasetPreviewTabularDataSettings,
...(dist?.rawData?.aspects?.[
"preview-tabular-data-settings"
]
? dist.rawData.aspects[
"preview-tabular-data-settings"
]
: {})
}
};
const rawDistData = {
...dist.rawData,
aspects: rawDistDataAspect
};
dist = {
...dist,
rawData: rawDistData
};
}
}

return dist;
return mergeDatasetAndDistributionPreviewSettings(dist, dataset);
}

render() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { Component } from "react";
import DataPreviewVis from "Components/Common/DataPreviewVis";
import DataPreviewVis, {
mergeDatasetAndDistributionPreviewSettings
} from "Components/Common/DataPreviewVis";
import DataPreviewMap from "Components/Common/DataPreviewMap";
import MagdaNamespacesConsumer from "Components/i18n/MagdaNamespacesConsumer";
import ContactPoint from "Components/Common/ContactPoint";
Expand Down Expand Up @@ -115,7 +117,10 @@ class DistributionDetails extends Component<{

render() {
const dataset = this.props.dataset;
const distribution = this.props.distribution;
const distribution = mergeDatasetAndDistributionPreviewSettings(
this.props.distribution,
dataset
) as ParsedDistribution;
const sourceText = this.renderLinkText(distribution);

return (
Expand All @@ -135,7 +140,8 @@ class DistributionDetails extends Component<{
{(distribution?.downloadURL || distribution?.accessURL) && (
<div className="distribution-preview">
<DataPreviewVis
distribution={this.props.distribution}
distribution={distribution}
dataset={dataset}
/>
<DataPreviewMap distributions={[distribution]} />
{extraVisualisationSections?.length
Expand Down
15 changes: 3 additions & 12 deletions magda-web-client/src/actions/recordActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { FetchError } from "../types";
import {
ensureAspectExists,
fetchRecord,
Record
Record,
DEFAULT_OPTIONAL_DISTRIBUTION_FETCH_ASPECT_LIST
} from "api-clients/RegistryApis";
import request from "helpers/request";

Expand Down Expand Up @@ -124,17 +125,7 @@ export function fetchDistributionFromRegistry(id: string): any {
try {
const data = await fetchRecord<RawDistribution>(
id,
[
"source-link-status",
"source",
"visualization-info",
"access",
"usage",
"dataset-format",
"ckan-resource",
"publishing",
"version"
],
DEFAULT_OPTIONAL_DISTRIBUTION_FETCH_ASPECT_LIST,
["dcat-distribution-strings"]
);
return dispatch(receiveDistribution(data));
Expand Down
13 changes: 13 additions & 0 deletions magda-web-client/src/api-clients/RegistryApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ export const DEFAULT_OPTIONAL_FETCH_ASPECT_LIST = [
"preview-tabular-data-settings"
];

export const DEFAULT_OPTIONAL_DISTRIBUTION_FETCH_ASPECT_LIST = [
"source-link-status",
"source",
"visualization-info",
"access",
"usage",
"dataset-format",
"ckan-resource",
"publishing",
"version",
"preview-tabular-data-settings"
];

export const DEFAULT_COMPULSORY_FETCH_ASPECT_LIST = ["dcat-dataset-strings"];

export async function fetchRecord<T = RawDataset>(
Expand Down

0 comments on commit 1a0f48d

Please sign in to comment.