|
1 | 1 | import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons'
|
2 |
| -import { useContext } from 'react' |
3 |
| -import { Button, Form, Input, Select, Space } from 'antd' |
| 2 | +import { useContext, useState, useEffect } from 'react' |
| 3 | +import { Button, Form, Input, Select, Space, Modal, message } from 'antd' |
4 | 4 | import { Ctx } from '@/pages'
|
5 | 5 |
|
6 | 6 | const { ipcRenderer } = window.require('electron')
|
7 | 7 |
|
8 | 8 | export default (props: { nextStep: () => void }) => {
|
9 | 9 | const [form] = Form.useForm()
|
10 |
| - const {setFahuoFile, setLaihuoFile, setColorMap} = useContext(Ctx) |
| 10 | + const { setFahuoFile, setLaihuoFile, setColorMap } = useContext(Ctx) |
11 | 11 | function handleLaihuoClick() {
|
12 | 12 | ipcRenderer.send('read-laihuo-excel')
|
13 | 13 | ipcRenderer.on('read-laihuo-success', (evt: any, fileName: string, file: ArrayBuffer) => {
|
14 |
| - form.setFields([{name: 'laihuo', value: fileName}]) |
| 14 | + form.setFields([{ name: 'laihuo', value: fileName }]) |
15 | 15 | setLaihuoFile(file)
|
16 | 16 | })
|
17 | 17 | }
|
18 | 18 | function handleFahuoClick() {
|
19 | 19 | ipcRenderer.send('read-fahuo-excel')
|
20 | 20 | ipcRenderer.on('read-fahuo-success', (evt: any, fileName: string, file: ArrayBuffer) => {
|
21 |
| - form.setFields([{name: 'fahuo', value: fileName}]) |
| 21 | + form.setFields([{ name: 'fahuo', value: fileName }]) |
22 | 22 | setFahuoFile(file)
|
23 | 23 | })
|
24 | 24 | }
|
25 | 25 | async function handleNextStep() {
|
26 | 26 | await form.validateFields()
|
27 | 27 | const values = form.getFieldsValue()
|
28 |
| - setColorMap(values.colorMaps.reduce((map: any, item: any) => ({...map, [item.cn]: item.en}), {})) |
| 28 | + setColorMap(values.colorMaps.reduce((map: any, item: any) => ({ ...map, [item.cn]: item.en }), {})) |
29 | 29 | props.nextStep()
|
30 | 30 | }
|
31 | 31 |
|
32 |
| - const defaultValue = { |
33 |
| - colorMaps: [{ |
34 |
| - cn: '白底红花', |
35 |
| - en: 'White with red flowers' |
36 |
| - }, { |
37 |
| - cn: '红橙点', |
38 |
| - en: 'Red orange point' |
39 |
| - }, { |
40 |
| - cn: '酒红', |
41 |
| - en: 'wine red' |
42 |
| - }, { |
43 |
| - cn: '白色', |
44 |
| - en: 'white' |
45 |
| - }, { |
46 |
| - cn: '白底红花', |
47 |
| - en: 'White printing' |
48 |
| - }, { |
49 |
| - cn: '黑色波点', |
50 |
| - en: 'Black wave point' |
51 |
| - }, { |
52 |
| - cn: '黑色', |
53 |
| - en: 'black' |
54 |
| - }, { |
55 |
| - cn: '深蓝花', |
56 |
| - en: 'Deep blue flowers' |
57 |
| - }, { |
58 |
| - cn: '深绿', |
59 |
| - en: 'dark green' |
60 |
| - }] |
| 32 | + const [saveVisible, setSaveVisible] = useState(false) |
| 33 | + const [saveForm] = Form.useForm() |
| 34 | + const [colorMapTpl, setColorMapTpl] = useState<Record<string, { cn: string, en: string }[]>>({}) |
| 35 | + const [selectedTpl, setSelectedTpl] = useState<string>() |
| 36 | + useEffect(() => { |
| 37 | + const tpl = JSON.parse(localStorage.getItem('colorMapTpls') ?? '{}') |
| 38 | + setColorMapTpl(tpl) |
| 39 | + }, []) |
| 40 | + async function handleOpenSave() { |
| 41 | + const value = form.getFieldsValue() |
| 42 | + if (!value.colorMaps?.length) return |
| 43 | + const validatePaths = value.colorMaps.reduce((res: any, item: any, i: number) => [...res, ['colorMaps', i, 'cn'], ['colorMaps', i, 'en']], []) |
| 44 | + await form.validateFields(validatePaths) |
| 45 | + setSaveVisible(true) |
| 46 | + saveForm.resetFields() |
61 | 47 | }
|
| 48 | + async function handleConfirmSave() { |
| 49 | + await saveForm.validateFields() |
| 50 | + const value = saveForm.getFieldsValue() |
| 51 | + const colorMaps = form.getFieldsValue().colorMaps ?? [] |
| 52 | + colorMapTpl[value.name] = colorMaps |
| 53 | + localStorage.setItem('colorMapTpls', JSON.stringify(colorMapTpl)) |
| 54 | + |
| 55 | + message.success('保存模版成功!') |
| 56 | + setSaveVisible(false) |
| 57 | + } |
| 58 | + function handleCloseSave() { |
| 59 | + setSaveVisible(false) |
| 60 | + } |
| 61 | + function handleSelectTpl(value: string) { |
| 62 | + setSelectedTpl(value) |
| 63 | + form.setFields([{ name: 'colorMaps', value: colorMapTpl[value] }]) |
| 64 | + } |
| 65 | + |
62 | 66 | return (
|
63 |
| - <Form form={form} className="!mt-4 w-4/6" labelCol={{ span: 5 }} initialValues={defaultValue}> |
| 67 | + <Form form={form} className="!mt-4 w-4/6" labelCol={{ span: 5 }}> |
64 | 68 | <Form.Item label="来货表" name="laihuo" rules={[{ required: true, message: '请上传来货表!' }]}>
|
65 | 69 | <Input onClick={handleLaihuoClick} placeholder="点击上传" />
|
66 | 70 | </Form.Item>
|
67 | 71 | <Form.Item label="发货表" name="fahuo" rules={[{ required: true, message: '请上传发货表!' }]}>
|
68 |
| - <Input onClick={handleFahuoClick} placeholder="点击上传"/> |
| 72 | + <Input onClick={handleFahuoClick} placeholder="点击上传" /> |
69 | 73 | </Form.Item>
|
70 | 74 | <Form.Item label="颜色映射" required>
|
71 | 75 | <div className="inline-block w-[295px] h-[250px] p-2 rounded bg-gray-8 align-top overflow-auto">
|
@@ -106,19 +110,37 @@ export default (props: { nextStep: () => void }) => {
|
106 | 110 | <Button className="!w-7/12" type="primary" onClick={() => add()} block icon={<PlusOutlined />}>
|
107 | 111 | 添加
|
108 | 112 | </Button>
|
109 |
| - <Button className="!w-4/12">存为模版</Button> |
| 113 | + <Button className="!w-4/12" onClick={handleOpenSave}>存为模版</Button> |
110 | 114 | </div>
|
111 | 115 | </Form.Item>
|
112 | 116 | <Form.ErrorList errors={errors} />
|
113 | 117 | </>
|
114 | 118 | )}
|
115 | 119 | </Form.List>
|
116 | 120 | </div>
|
117 |
| - <Select className="inline-block !w-[80px] !ml-4 align-top" placeholder="模版"></Select> |
| 121 | + <Select className="inline-block !w-[80px] !ml-4 align-top" placeholder="模版" value={selectedTpl} onChange={handleSelectTpl} showSearch> |
| 122 | + { |
| 123 | + Object.keys(colorMapTpl).map(key => <Select.Option value={key} key={key}>{key}</Select.Option>) |
| 124 | + } |
| 125 | + </Select> |
118 | 126 | </Form.Item>
|
119 | 127 | <Form.Item className="!text-right">
|
120 | 128 | <Button type="primary" onClick={handleNextStep}>下一步</Button>
|
121 | 129 | </Form.Item>
|
| 130 | + |
| 131 | + <Modal title="保存颜色映射模版" |
| 132 | + onOk={handleConfirmSave} |
| 133 | + visible={saveVisible} |
| 134 | + onCancel={handleCloseSave} |
| 135 | + okText="确认" |
| 136 | + cancelText="取消" |
| 137 | + > |
| 138 | + <Form form={saveForm}> |
| 139 | + <Form.Item label="名称" name="name" rules={[{ required: true, message: '请输入名称!' }]}> |
| 140 | + <Input placeholder='输入模版名称,可覆盖之前的'></Input> |
| 141 | + </Form.Item> |
| 142 | + </Form> |
| 143 | + </Modal> |
122 | 144 | </Form>
|
123 | 145 | )
|
124 | 146 | }
|
0 commit comments