Skip to content

Commit 480777a

Browse files
committed
feat: add excel parser for file upload component
1 parent 4247b0c commit 480777a

28 files changed

+491
-381
lines changed

client/packages/openblocks/src/api/datasourceApi.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ import { JSONArray } from "../util/jsonTypes";
77
import { AuthType, HttpOAuthGrantType } from "../pages/datasource/form/httpDatasourceForm";
88
import { Datasource } from "@openblocks-ee/constants/datasourceConstants";
99

10-
// FIXME(zhangqinyao): extract a common config
11-
export interface MysqlConfig {
10+
export interface PreparedStatementConfig {
11+
enableTurnOffPreparedStatement: boolean;
12+
}
13+
14+
export interface SQLConfig extends PreparedStatementConfig {
1215
host: string;
1316
port: string;
1417
database: string;
1518
username: string;
1619
password: string;
1720
usingSsl: boolean;
18-
enableTurnOffPreparedStatement: boolean;
1921
}
2022

21-
export interface MongoConfig extends MysqlConfig {
23+
export interface MongoConfig extends SQLConfig {
2224
uri: string;
2325
usingUri: boolean;
2426
}
2527

26-
export interface OracleConfig extends MysqlConfig {
28+
export interface OracleConfig extends SQLConfig {
2729
serviceName: string;
2830
sid: string;
2931
usingSid: boolean;
@@ -90,7 +92,7 @@ export interface HttpConfig {
9092
}
9193

9294
export type DatasourceConfigType =
93-
| MysqlConfig
95+
| SQLConfig
9496
| HttpConfig
9597
| MongoConfig
9698
| OAuthBasicConfig

client/packages/openblocks/src/comps/comps/dateComp.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DatePicker } from "antd";
22
import _ from "lodash";
33
import moment from "moment";
4-
import { Section, sectionNames } from "openblocks-design";
4+
import { isDarkColor, lightenColor, Section, sectionNames } from "openblocks-design";
55
import { RecordConstructorToComp, RecordConstructorToView } from "openblocks-core";
66
import {
77
BoolCodeControl,
@@ -24,29 +24,31 @@ import { formDataChildren, FormDataPropertyView } from "./formComp/formDataConst
2424
import { styleControl } from "comps/controls/styleControl";
2525
import { DateTimeStyle, DateTimeStyleType } from "comps/controls/styleControlConstants";
2626
import styled, { css } from "styled-components";
27-
import { isDarkColor, lightenColor } from "openblocks-design";
2827
import { withMethodExposing } from "../generators/withMethodExposing";
2928
import {
3029
disabledPropertyView,
30+
formatPropertyView,
3131
hiddenPropertyView,
3232
hourStepPropertyView,
33-
minuteStepPropertyView,
34-
SecondStepPropertyView,
35-
requiredPropertyView,
3633
maxDatePropertyView,
34+
maxTimePropertyView,
3735
minDatePropertyView,
3836
minTimePropertyView,
39-
maxTimePropertyView,
40-
formatPropertyView,
37+
minuteStepPropertyView,
38+
requiredPropertyView,
39+
SecondStepPropertyView,
4140
} from "comps/utils/propertyUtils";
4241
import { trans } from "i18n";
4342
import {
4443
DATE_FORMAT,
45-
DateParser,
4644
DATE_TIME_FORMAT,
47-
TIME_FORMAT,
45+
DateParser,
4846
PickerMode,
47+
TIME_FORMAT,
4948
} from "util/dateTimeUtils";
49+
import { checkIsMobile } from "util/commonUtils";
50+
import { useContext } from "react";
51+
import { EditorContext } from "comps/editorState";
5052

5153
const EventOptions = [changeEvent, focusEvent, blurEvent] as const;
5254

@@ -262,6 +264,7 @@ export const datePickerControl = (function () {
262264
};
263265

264266
return new UICompBuilder(childrenMap, (props) => {
267+
const editorState = useContext(EditorContext);
265268
const children = (
266269
<>
267270
<DatePickerStyled
@@ -283,6 +286,7 @@ export const datePickerControl = (function () {
283286
}}
284287
onFocus={() => props.onEvent("focus")}
285288
onBlur={() => props.onEvent("blur")}
289+
inputReadOnly={checkIsMobile(editorState.getAppSettings().maxWidth)}
286290
/>
287291
</>
288292
);
@@ -342,6 +346,7 @@ export const dateRangeControl = (function () {
342346
};
343347

344348
return new UICompBuilder(childrenMap, (props) => {
349+
const editorState = useContext(EditorContext);
345350
const children = (
346351
<>
347352
<RangePickerStyled
@@ -370,6 +375,7 @@ export const dateRangeControl = (function () {
370375
}}
371376
onFocus={() => props.onEvent("focus")}
372377
onBlur={() => props.onEvent("blur")}
378+
inputReadOnly={checkIsMobile(editorState.getAppSettings().maxWidth)}
373379
/>
374380
</>
375381
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id,name,date,department
2+
1,Reagen Gilberthorpe,7/5/2022,Marketing
3+
2,Haroun Lortzing,11/6/2022,Human Resources
4+
3,Garret Kilmaster,11/14/2021,Research and Development
5+
4,Israel Harrowsmith,4/3/2022,Training
6+
5,Loren O'Lagen,9/10/2022,Services
7+
6,Wallis Hothersall,4/18/2022,Accounting
8+
7,Kaia Biskup,3/4/2022,Sales
9+
8,Travers Saterweyte,1/9/2022,Human Resources
10+
9,Mikey Niemetz,1/4/2022,Marketing
11+
10,Mano Meckiff,2/19/2022,Research and Development
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{ "id": 1, "name": "Reagen Gilberthorpe", "date": "7/5/2022", "department": "Marketing" },
3+
{ "id": 2, "name": "Haroun Lortzing", "date": "11/6/2022", "department": "Human Resources" },
4+
{
5+
"id": 3,
6+
"name": "Garret Kilmaster",
7+
"date": "11/14/2021",
8+
"department": "Research and Development"
9+
},
10+
{ "id": 4, "name": "Israel Harrowsmith", "date": "4/3/2022", "department": "Training" },
11+
{ "id": 5, "name": "Loren O'Lagen", "date": "9/10/2022", "department": "Services" },
12+
{ "id": 6, "name": "Wallis Hothersall", "date": "4/18/2022", "department": "Accounting" },
13+
{ "id": 7, "name": "Kaia Biskup", "date": "3/4/2022", "department": "Sales" },
14+
{ "id": 8, "name": "Travers Saterweyte", "date": "1/9/2022", "department": "Human Resources" },
15+
{ "id": 9, "name": "Mikey Niemetz", "date": "1/4/2022", "department": "Marketing" },
16+
{
17+
"id": 10,
18+
"name": "Mano Meckiff",
19+
"date": "2/19/2022",
20+
"department": "Research and Development"
21+
}
22+
]
Loading
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { readFile } from "fs/promises";
2+
import { resolveParsedValue } from "./fileComp";
3+
import mime from "mime";
4+
5+
global.TextDecoder = require("util").TextDecoder;
6+
7+
const expectParseValue = [
8+
{ id: 1, name: "Reagen Gilberthorpe", date: "7/5/2022", department: "Marketing" },
9+
{
10+
id: 2,
11+
name: "Haroun Lortzing",
12+
date: "11/6/2022",
13+
department: "Human Resources",
14+
},
15+
{ id: 3, name: "Garret Kilmaster", date: "11/14/2021", department: "Research and Development" },
16+
{
17+
id: 4,
18+
name: "Israel Harrowsmith",
19+
date: "4/3/2022",
20+
department: "Training",
21+
},
22+
{ id: 5, name: "Loren O'Lagen", date: "9/10/2022", department: "Services" },
23+
{
24+
id: 6,
25+
name: "Wallis Hothersall",
26+
date: "4/18/2022",
27+
department: "Accounting",
28+
},
29+
{ id: 7, name: "Kaia Biskup", date: "3/4/2022", department: "Sales" },
30+
{
31+
id: 8,
32+
name: "Travers Saterweyte",
33+
date: "1/9/2022",
34+
department: "Human Resources",
35+
},
36+
{ id: 9, name: "Mikey Niemetz", date: "1/4/2022", department: "Marketing" },
37+
{
38+
id: 10,
39+
name: "Mano Meckiff",
40+
date: "2/19/2022",
41+
department: "Research and Development",
42+
},
43+
];
44+
45+
// csv use raw data to avoid number format
46+
const expectCSVParseValue = [
47+
{ id: "1", name: "Reagen Gilberthorpe", date: "7/5/2022", department: "Marketing" },
48+
{
49+
id: "2",
50+
name: "Haroun Lortzing",
51+
date: "11/6/2022",
52+
department: "Human Resources",
53+
},
54+
{ id: "3", name: "Garret Kilmaster", date: "11/14/2021", department: "Research and Development" },
55+
{
56+
id: "4",
57+
name: "Israel Harrowsmith",
58+
date: "4/3/2022",
59+
department: "Training",
60+
},
61+
{ id: "5", name: "Loren O'Lagen", date: "9/10/2022", department: "Services" },
62+
{
63+
id: "6",
64+
name: "Wallis Hothersall",
65+
date: "4/18/2022",
66+
department: "Accounting",
67+
},
68+
{ id: "7", name: "Kaia Biskup", date: "3/4/2022", department: "Sales" },
69+
{
70+
id: "8",
71+
name: "Travers Saterweyte",
72+
date: "1/9/2022",
73+
department: "Human Resources",
74+
},
75+
{ id: "9", name: "Mikey Niemetz", date: "1/4/2022", department: "Marketing" },
76+
{
77+
id: "10",
78+
name: "Mano Meckiff",
79+
date: "2/19/2022",
80+
department: "Research and Development",
81+
},
82+
];
83+
84+
function toArrayBuffer(buf: Buffer) {
85+
const ab = new ArrayBuffer(buf.length);
86+
const view = new Uint8Array(ab);
87+
for (let i = 0; i < buf.length; ++i) {
88+
view[i] = buf[i];
89+
}
90+
return ab;
91+
}
92+
93+
function getFile(path: string) {
94+
return readFile(path).then((b) => ({
95+
originFileObj: {
96+
arrayBuffer: () => new Promise((resolve) => resolve(toArrayBuffer(b))),
97+
type: mime.getType(path.substring(path.lastIndexOf("."))),
98+
},
99+
}));
100+
}
101+
102+
test("test resolveParsedValue", async () => {
103+
const files = await Promise.all([
104+
getFile("packages/openblocks/src/comps/comps/fileComp.test.csv"),
105+
getFile("packages/openblocks/src/comps/comps/fileComp.test.json"),
106+
getFile("packages/openblocks/src/comps/comps/fileComp.test.png"),
107+
getFile("packages/openblocks/src/comps/comps/fileComp.test.txt"),
108+
getFile("packages/openblocks/src/comps/comps/fileComp.test.xlsx"),
109+
]);
110+
const parsedValue = await resolveParsedValue(files as any);
111+
expect(parsedValue[0]).toMatchObject(expectCSVParseValue);
112+
expect(parsedValue[1]).toMatchObject(expectParseValue);
113+
expect(parsedValue[2]).toBeNull();
114+
expect(parsedValue[3]).toMatchObject(expectParseValue);
115+
expect(parsedValue[4]).toMatchObject(expectParseValue);
116+
});
Binary file not shown.

0 commit comments

Comments
 (0)