Skip to content

Commit

Permalink
[#399] Create ExploreDataFromOtherStudies table
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Jan 24, 2025
1 parent 3994b6e commit 68d32c8
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 15 deletions.
32 changes: 21 additions & 11 deletions frontend/src/pages/cases/components/VisualCardWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const VisualCardWrapper = ({
setShowLabel,
exportElementRef,
exportFilename = "Undefined",
extraButtons = [],
}) => {
const [exportimg, setExporting] = useState(false);

Expand All @@ -77,9 +78,13 @@ const VisualCardWrapper = ({
<Col span={16}>
<Space align="center">
<div className="title">{title}</div>
<Tooltip className="info-tooltip" title={tooltipText}>
<InfoCircleOutlined />
</Tooltip>
{tooltipText ? (
<Tooltip className="info-tooltip" title={tooltipText}>
<InfoCircleOutlined />
</Tooltip>
) : (
""
)}
</Space>
</Col>
<Col span={8} align="end">
Expand All @@ -94,14 +99,19 @@ const VisualCardWrapper = ({
) : (
""
)}
<Button
size="small"
className="button-export"
onClick={handleOnClickSaveAsImage}
loading={exportimg}
>
Export
</Button>
{exportElementRef ? (
<Button
size="small"
className="button-export"
onClick={handleOnClickSaveAsImage}
loading={exportimg}
>
Export
</Button>
) : (
""
)}
{extraButtons?.length ? extraButtons.map((button) => button) : ""}
</Col>
</Row>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React, { useMemo } from "react";
import { Card, Row, Col, Space, Tag } from "antd";
import { CaseUIState, CurrentCaseState } from "../store";
import { thousandFormatter } from "../../../components/chart/options/common";
import { VisualCardWrapper } from "../components";
import ChartCalculatedHouseholdIncome from "./ChartCalculatedHouseholdIncome";
import ExploreDataFromOtherStudiesTable from "./ExploreDataFromOtherStudiesTable";

const EnterIncomeDataVisual = () => {
const { activeSegmentId } = CaseUIState.useState((s) => s.general);
Expand Down Expand Up @@ -39,9 +39,7 @@ const EnterIncomeDataVisual = () => {
<ChartCalculatedHouseholdIncome />
</Col>
<Col span={24}>
<VisualCardWrapper title="Household Income">
Household Income Table
</VisualCardWrapper>
<ExploreDataFromOtherStudiesTable />
</Col>
</Row>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import React, { useState, useEffect } from "react";
import { VisualCardWrapper } from "../components";
import { Button, Col, Row, Select, Table } from "antd";
import { driverOptions } from "../../explore-studies";
import { thousandFormatter } from "../../../components/chart/options/common";
import { api } from "../../../lib";
import { CurrentCaseState } from "../store";
import { isEmpty, upperFirst } from "lodash";

const ExploreDataFromOtherStudiesTable = () => {
const currentCase = CurrentCaseState.useState((s) => s);

const [selectedDriver, setSelectedDriver] = useState("area");
const [loadingRefData, setLoadingRefData] = useState(false);
const [referenceData, setReferenceData] = useState([]);
const [exploreButtonLink, setExploreButtonLink] = useState(null);

useEffect(() => {
const country = currentCase?.country;
const commodity = currentCase?.case_commodities?.find(
(x) => x.commodity_type === "focus"
)?.commodity;
if (!isEmpty(currentCase) && selectedDriver) {
setLoadingRefData(true);
setExploreButtonLink(
`/explore-studies/${country}/${commodity}/${selectedDriver}`
);
api
.get(
`reference_data/reference_value?country=${country}&commodity=${commodity}&driver=${selectedDriver}`
)
.then((res) => {
setReferenceData(res.data);
})
.catch(() => {
setReferenceData([]);
})
.finally(() => {
setLoadingRefData(false);
});
}
}, [currentCase, selectedDriver]);

return (
<VisualCardWrapper
title="Explore data from other studies"
extraButtons={[
<a
href={exploreButtonLink}
key="explore-studies-button"
target="_blank"
rel="noreferrer noopener"
>
<Button className="button-export" disabled={!exploreButtonLink}>
Explore studies
</Button>
</a>,
]}
>
<Row gutter={[20, 20]} align="middle">
<Col span={24}>
<Select
placeholder="Select driver"
options={driverOptions}
onChange={setSelectedDriver}
value={selectedDriver}
/>
</Col>
<Col span={24}>
<Table
bordered
size="small"
rowKey="id"
loading={loadingRefData}
columns={[
{
key: "value",
title: "Value",
dataIndex: "value",
render: (value) => {
if (value && Number(value)) {
return thousandFormatter(value);
}
return value || "-";
},
},
{
key: "unit",
title: "Unit",
dataIndex: "unit",
render: (value) => value || "-",
},
{
key: "type",
title: "Type",
dataIndex: "type",
render: (value) => value || "-",
},
{
key: "source",
title: "Source",
width: "35%",
render: (value, row) => {
if (!row?.link) {
return value;
}
const url =
row.link?.includes("https://") ||
row.link?.includes("http://")
? row.link
: `https://${row.link}`;
return (
<a href={url} target="_blank" rel="noreferrer noopener">
{row.source}
</a>
);
},
},
]}
dataSource={referenceData}
expandable={{
expandedRowRender: (record) => (
<div style={{ padding: 0 }}>
<Table
bordered
showHeader={false}
size="small"
rowKey="id"
columns={[
{
key: "label",
title: "Label",
dataIndex: "label",
width: "45%",
},
{
key: "value",
title: "Value",
dataIndex: "value",
},
]}
dataSource={Object.keys(record)
.map((key) => {
if (
["id", "unit", "value", "source", "link"].includes(
key
)
) {
return false;
}
const label = key
.split("_")
?.map((x) => upperFirst(x))
?.join(" ");
let value = record[key];
if (value && typeof value === "number") {
value = thousandFormatter(value);
}
if (value && typeof value !== "number") {
value = value
.split(" ")
.map((x) => upperFirst(x))
.join(" ");
}
return {
label: label,
value: value || "-",
};
})
.filter((x) => x)}
pagination={false}
/>
</div>
),
}}
/>
</Col>
</Row>
</VisualCardWrapper>
);
};

export default ExploreDataFromOtherStudiesTable;

0 comments on commit 68d32c8

Please # to comment.