-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathExport.tsx
164 lines (153 loc) · 5.15 KB
/
Export.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React from 'react';
import { commonText } from '../../localization/common';
import { queryText } from '../../localization/query';
import { ping } from '../../utils/ajax/ping';
import { f } from '../../utils/functools';
import type { RA } from '../../utils/types';
import { keysToLowerCase } from '../../utils/utils';
import type { SerializedResource } from '../DataModel/helperTypes';
import type { SpecifyResource } from '../DataModel/legacyTypes';
import { schema } from '../DataModel/schema';
import type { SpQuery, SpQueryField, Tables } from '../DataModel/types';
import { Dialog } from '../Molecules/Dialog';
import { downloadFile } from '../Molecules/FilePicker';
import { hasPermission } from '../Permissions/helpers';
import { userPreferences } from '../Preferences/userPreferences';
import { mappingPathIsComplete } from '../WbPlanView/helpers';
import { generateMappingPathPreview } from '../WbPlanView/mappingPreview';
import { QueryButton } from './Components';
import type { QueryField } from './helpers';
import { hasLocalityColumns } from './helpers';
import type { QueryResultRow } from './Results';
export function QueryExportButtons({
baseTableName,
fields,
queryResource,
getQueryFieldRecords,
recordSetId,
results,
selectedRows,
}: {
readonly baseTableName: keyof Tables;
readonly fields: RA<QueryField>;
readonly queryResource: SpecifyResource<SpQuery>;
readonly getQueryFieldRecords:
| (() => RA<SerializedResource<SpQueryField>>)
| undefined;
readonly recordSetId: number | undefined;
readonly results: React.MutableRefObject<
RA<QueryResultRow | undefined> | undefined
>;
readonly selectedRows: ReadonlySet<number>;
}): JSX.Element {
const showConfirmation = (): boolean =>
fields.some(({ mappingPath }) => !mappingPathIsComplete(mappingPath));
const [state, setState] = React.useState<'creating' | 'warning' | undefined>(
undefined
);
function doQueryExport(url: string, delimiter: string | undefined): void {
if (typeof getQueryFieldRecords === 'function')
queryResource.set('fields', getQueryFieldRecords());
const serialized = queryResource.toJSON();
setState('creating');
void ping(url, {
method: 'POST',
body: keysToLowerCase({
...serialized,
captions: fields
.filter(({ isDisplay }) => isDisplay)
.map(({ mappingPath }) =>
generateMappingPathPreview(baseTableName, mappingPath)
),
recordSetId,
delimiter,
}),
});
}
const [separator] = userPreferences.use(
'queryBuilder',
'behavior',
'exportFileDelimiter'
);
/*
*Will be only called if query is not distinct,
*selection not enabled when distinct selected
*/
function handleSelectedResults(): string {
const selectedResults = results?.current?.filter((item) =>
f.has(selectedRows, item?.[0])
);
const joinedSelected = selectedResults?.map((subArray) =>
subArray?.slice(1).join(separator)
);
const resultToExport = [
fields
.map((field) =>
generateMappingPathPreview(baseTableName, field.mappingPath)
)
.join(separator),
...(joinedSelected ?? []),
];
return resultToExport.join('\n');
}
const canUseKml =
(baseTableName === 'Locality' ||
fields.some(({ mappingPath }) => mappingPath.includes('locality'))) &&
hasPermission('/querybuilder/query', 'export_kml');
return (
<>
{state === 'creating' ? (
<Dialog
buttons={commonText.close()}
header={queryText.queryExportStarted()}
onClose={(): void => setState(undefined)}
>
{queryText.queryExportStartedDescription()}
</Dialog>
) : state === 'warning' ? (
<Dialog
buttons={commonText.close()}
header={queryText.missingCoordinatesForKml()}
onClose={(): void => setState(undefined)}
>
{queryText.missingCoordinatesForKmlDescription()}
</Dialog>
) : undefined}
{hasPermission('/querybuilder/query', 'export_csv') && (
<QueryButton
disabled={fields.length === 0}
showConfirmation={showConfirmation}
onClick={(): void => {
selectedRows.size === 0
? doQueryExport('/stored_query/exportcsv/', separator)
: downloadFile(
`${
queryResource.isNew()
? `${queryText.newQueryName()} ${
schema.models[baseTableName].label
}`
: queryResource.get('name')
} - ${new Date().toDateString()}.csv`,
handleSelectedResults()
);
}}
>
{queryText.createCsv()}
</QueryButton>
)}
{canUseKml && (
<QueryButton
disabled={fields.length === 0}
showConfirmation={showConfirmation}
onClick={(): void =>
hasLocalityColumns(fields)
? doQueryExport('/stored_query/exportkml/', undefined)
: setState('warning')
}
>
{queryText.createKml()}
</QueryButton>
)}
</>
);
}