Skip to content

Commit 8ca9bda

Browse files
committed
enhancement: refactor download URL controls into a separate component
1 parent 932b0df commit 8ca9bda

File tree

3 files changed

+68
-52
lines changed

3 files changed

+68
-52
lines changed

packages/core/components/EditorPanel/DataTableEditor.tsx

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { CheckBox, TextField, Select } from './Inputs'
55
import MultiSelect from '../MultiSelect'
66
import { UpdateFieldFunc } from '../../types/UpdateFieldFunc'
77
import { Visualization } from '../../types/Visualization'
8-
import _ from 'lodash'
9-
import { Column } from '../../types/Column'
10-
import CustomSortOrder from './CustomSortOrder'
8+
import _ from 'lodash'
9+
import { Column } from '../../types/Column'
10+
import CustomSortOrder from './CustomSortOrder'
11+
import DownloadUrlControls from './DownloadUrlControls'
1112

1213
interface DataTableProps {
1314
config: Partial<Visualization>
@@ -377,29 +378,12 @@ const DataTableEditor: React.FC<DataTableProps> = ({ config, updateField, isDash
377378
updateField={updateField}
378379
/>
379380
)}
380-
{hasUrlBackedDataSource && (
381-
<>
382-
<CheckBox
383-
value={config.table.showDownloadUrl}
384-
fieldName='showDownloadUrl'
385-
label='Show URL to Automatically Updated Data'
386-
section='table'
387-
updateField={updateField}
388-
/>
389-
{config.table.showDownloadUrl && (
390-
<div className='ms-4 mt-2' style={{ maxWidth: 'calc(100% - 1.5rem)' }}>
391-
<TextField
392-
value={config.table.downloadUrlLabel}
393-
section='table'
394-
fieldName='downloadUrlLabel'
395-
label='Dataset Link Text'
396-
placeholder='Link to Dataset'
397-
updateField={updateField}
398-
/>
399-
</div>
400-
)}
401-
</>
402-
)}
381+
<DownloadUrlControls
382+
hasUrlBackedDataSource={hasUrlBackedDataSource}
383+
showDownloadUrl={config.table.showDownloadUrl}
384+
downloadUrlLabel={config.table.downloadUrlLabel}
385+
updateField={updateField}
386+
/>
403387
{config.type !== 'table' && (
404388
<CheckBox
405389
value={config.table.showDownloadImgButton}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react'
2+
import { CheckBox, TextField } from './Inputs'
3+
import { UpdateFieldFunc } from '../../types/UpdateFieldFunc'
4+
import { Column } from '../../types/Column'
5+
6+
type DownloadUrlControlsProps = {
7+
hasUrlBackedDataSource: boolean
8+
showDownloadUrl?: boolean
9+
downloadUrlLabel?: string
10+
updateField: UpdateFieldFunc<string | boolean | string[] | number | Record<string, Partial<Column>>>
11+
}
12+
13+
const DownloadUrlControls: React.FC<DownloadUrlControlsProps> = ({
14+
hasUrlBackedDataSource,
15+
showDownloadUrl,
16+
downloadUrlLabel,
17+
updateField
18+
}) => {
19+
if (!hasUrlBackedDataSource) return null
20+
21+
return (
22+
<>
23+
<CheckBox
24+
value={showDownloadUrl}
25+
fieldName='showDownloadUrl'
26+
label='Show URL to Automatically Updated Data'
27+
section='table'
28+
updateField={updateField}
29+
/>
30+
{showDownloadUrl && (
31+
<div className='ms-4 mt-2' style={{ maxWidth: 'calc(100% - 1.5rem)' }}>
32+
<TextField
33+
value={downloadUrlLabel}
34+
section='table'
35+
fieldName='downloadUrlLabel'
36+
label='Dataset Link Text'
37+
placeholder='Link to Dataset'
38+
updateField={updateField}
39+
/>
40+
</div>
41+
)}
42+
</>
43+
)
44+
}
45+
46+
export default DownloadUrlControls

packages/map/src/components/EditorPanel/components/EditorPanel.tsx

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { MapContext } from '../../../types/MapContext.js'
5151
import Alert from '@cdc/core/components/Alert'
5252
import { updateFieldFactory } from '@cdc/core/helpers/updateFieldFactory'
5353
import { CheckBox, Select, TextField } from '@cdc/core/components/EditorPanel/Inputs'
54+
import DownloadUrlControls from '@cdc/core/components/EditorPanel/DownloadUrlControls'
5455
import Button from '@cdc/core/components/elements/Button'
5556
import StyleTreatmentSection from '@cdc/core/components/EditorPanel/sections/StyleTreatmentSection'
5657
import { HeaderThemeSelector } from '@cdc/core/components/HeaderThemeSelector'
@@ -1207,7 +1208,11 @@ const EditorPanel: React.FC<MapEditorPanelProps> = ({ datasets }) => {
12071208
))
12081209
}
12091210

1210-
const isLoadedFromUrl = config?.dataKey?.includes('http://') || config?.dataKey?.includes('https://')
1211+
const isLoadedFromUrl =
1212+
config?.dataFileSourceType === 'url' ||
1213+
Boolean(config?.runtimeDataUrl || config?.dataUrl || config?.dataFileName) ||
1214+
config?.dataKey?.includes('http://') ||
1215+
config?.dataKey?.includes('https://')
12111216

12121217
// Custom convertStateToConfig for map with map-specific logic
12131218
const customConvertStateToConfig = () => {
@@ -3198,31 +3203,12 @@ const EditorPanel: React.FC<MapEditorPanelProps> = ({ datasets }) => {
31983203
updateField={updateField}
31993204
/>
32003205
)}
3201-
{isLoadedFromUrl && (
3202-
<>
3203-
<CheckBox
3204-
value={config.table.showDownloadUrl}
3205-
section='table'
3206-
subsection={null}
3207-
fieldName='showDownloadUrl'
3208-
label='Show URL to Automatically Updated Data'
3209-
updateField={updateField}
3210-
/>
3211-
{config.table.showDownloadUrl && (
3212-
<div className='ms-4 mt-2' style={{ maxWidth: 'calc(100% - 1.5rem)' }}>
3213-
<TextField
3214-
value={config.table.downloadUrlLabel}
3215-
section='table'
3216-
subsection={null}
3217-
fieldName='downloadUrlLabel'
3218-
label='Dataset Link Text'
3219-
placeholder='Link to Dataset'
3220-
updateField={updateField}
3221-
/>
3222-
</div>
3223-
)}
3224-
</>
3225-
)}
3206+
<DownloadUrlControls
3207+
hasUrlBackedDataSource={Boolean(isLoadedFromUrl)}
3208+
showDownloadUrl={config.table.showDownloadUrl}
3209+
downloadUrlLabel={config.table.downloadUrlLabel}
3210+
updateField={updateField}
3211+
/>
32263212
<CheckBox
32273213
value={config.table.showFullGeoNameInCSV}
32283214
section='table'

0 commit comments

Comments
 (0)