From 535686ff4e2413e67fd1efbf6e3deb2e1591aa4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yhoan=20Alejandro=20Guzm=C3=A1n=20Garc=C3=ADa?= <41337901+yaguzmang@users.noreply.github.com> Date: Fri, 3 Jan 2025 01:11:42 -0500 Subject: [PATCH] 4001 - Datatable use Datagrid - ButtonCopyValues (#4194) * 4001 - Copy values from grid * 4001 - Move and refactor Copy logic to useCopyValues hook * 4001 - Minor rtl fix for disabled input number --- .../ButtonCopyValues/ButtonCopyValues.tsx | 77 ++-------------- .../ButtonCopyValues/hooks/useCopyValues.ts | 92 +++++++++++++++++++ .../DataTable/Table/ButtonCopyValues/types.ts | 8 ++ .../Table/RowData/Cell/Number/Number.scss | 12 +++ .../pages/Section/DataTable/Table/Table.tsx | 2 +- 5 files changed, 119 insertions(+), 72 deletions(-) create mode 100644 src/client/pages/Section/DataTable/Table/ButtonCopyValues/hooks/useCopyValues.ts create mode 100644 src/client/pages/Section/DataTable/Table/ButtonCopyValues/types.ts diff --git a/src/client/pages/Section/DataTable/Table/ButtonCopyValues/ButtonCopyValues.tsx b/src/client/pages/Section/DataTable/Table/ButtonCopyValues/ButtonCopyValues.tsx index 70c5f11cb6..30957c09c7 100644 --- a/src/client/pages/Section/DataTable/Table/ButtonCopyValues/ButtonCopyValues.tsx +++ b/src/client/pages/Section/DataTable/Table/ButtonCopyValues/ButtonCopyValues.tsx @@ -1,83 +1,18 @@ -import React, { MutableRefObject, useCallback } from 'react' +import React from 'react' import { useTranslation } from 'react-i18next' -import { Table, TableNames } from 'meta/assessment' - -import { useCycle } from 'client/store/assessment' -import { useUser } from 'client/store/user' import Button from 'client/components/Buttons/Button' -import { getData } from 'client/components/ButtonTableExport/utils' - -type CopyValuesProps = { - tableRef: MutableRefObject - table: Table -} -// tableMappings is a mapping of table names to the variables that should be copied to clipboard -const tableMappings2020: Record> = { - [TableNames.forestCharacteristics]: [ - 'forestCharacteristics.naturalForestArea', - 'forestCharacteristics.plantationForestArea', - 'forestCharacteristics.plantationForestIntroducedArea', - 'forestCharacteristics.otherPlantedForestArea', - ], - [TableNames.growingStockAvg]: [ - 'growingStock.naturallyRegeneratingForest', - 'growingStock.plantedForest', - 'growingStock.plantationForest', - 'growingStock.otherPlantedForest', - ], -} - -const tableMappings2025: Record> = { - [TableNames.forestCharacteristics]: [ - 'fra.forestCharacteristics.naturalForestArea2025', - 'fra.growingStock.plantationForest2025', - 'fra.forestCharacteristics.plantationForestIntroducedArea2025', - 'fra.forestCharacteristics.ofWhichOtherPlantedForest', - ], - [TableNames.growingStockAvg]: [ - 'fra.growingStock.naturallyRegeneratingForest2025', - 'fra.growingStock.plantedForest2025', - 'fra.growingStock.plantationForest2025', - 'fra.growingStock.otherPlantedForest2025', - ], -} - -const colMapping = [1990, 2000, 2010, 2015, 2020, 2025] +import { useCopyValues } from './hooks/useCopyValues' +import { CopyValuesProps } from './types' const ButtonCopyValues: React.FC = (props: CopyValuesProps) => { - const { tableRef, table } = props - const cycle = useCycle() - const user = useUser() const { t } = useTranslation() + const { onClick, showButton } = useCopyValues(props) - const tableMappings = cycle.name === '2025' ? tableMappings2025 : tableMappings2020 - - const showButton = Object.keys(tableMappings).includes(table.props.name) - - const _onClick = useCallback(() => { - const _table = tableRef.current - if (!_table) return - const csv = getData(_table) - const include = tableMappings[table.props.name].map((variableLabel) => t(variableLabel)) - // A list of indexes of the table columns that should be copied to clipboard - const correctIndexes = colMapping.map((year) => csv[1].indexOf(year.toString())) - const z = csv - .filter((row) => { - return include.some((translatedVariable) => row[0].includes(translatedVariable)) - }) - .map((row: string[]) => { - return row.filter((_, i) => correctIndexes.includes(i)) - }) - - navigator.clipboard.writeText(z.map((row: Array) => row.join('\t')).join('\n')) - }, [t, table.props.name, tableMappings, tableRef]) - - // Hide button if incorrect table or user is not logged in - if (!user || !showButton) return null + if (!showButton) return null - return