feat🔫: new labels

This commit is contained in:
车厘子 2022-11-26 01:17:48 +08:00
parent 53ac383f5b
commit 1d928b8a23
3 changed files with 38 additions and 26 deletions

View File

@ -1,13 +1,13 @@
import {Button, DatePicker, Input, InputNumber, message, Radio, Select, Space, Table, Tag} from "antd"; import { Button, DatePicker, Input, InputNumber, message, Radio, Select, Space, Table, Tag } from "antd";
import {ArrowDownOutlined, CloudUploadOutlined, DeleteOutlined,} from "@ant-design/icons"; import { ArrowDownOutlined, CloudUploadOutlined, DeleteOutlined, } from "@ant-design/icons";
import {useContext, useEffect, useRef, useState} from "react"; import { useContext, useEffect, useRef, useState } from "react";
import {BillType, EmptyBill, IBill} from "../../model"; import { BillType, EmptyBill, IBill } from "../../model";
import moment from "moment/moment"; import moment from "moment/moment";
import {BillContext} from "../../store"; import { BillContext } from "../../store";
import {observer} from "mobx-react-lite"; import { observer } from "mobx-react-lite";
import styles from "./Record.module.scss" import styles from "./Record.module.scss"
import {BaseSelectRef} from "rc-select/lib/BaseSelect"; import { BaseSelectRef } from "rc-select/lib/BaseSelect";
import {createBill} from "../../api/bills"; import { createBill } from "../../api/bills";
function Record() { function Record() {
@ -69,7 +69,7 @@ function Record() {
<Button <Button
type="primary" type="primary"
danger danger
icon={<DeleteOutlined/>} icon={<DeleteOutlined />}
onClick={() => setDataSource(datasource.filter(bill => bill !== record))} onClick={() => setDataSource(datasource.filter(bill => bill !== record))}
/> />
</Space> </Space>
@ -79,8 +79,8 @@ function Record() {
const [datasource, setDataSource] = useState<IBill[]>([]) const [datasource, setDataSource] = useState<IBill[]>([])
const typeOpt = [ const typeOpt = [
{label: '支出', value: BillType.CONSUME}, { label: '支出', value: BillType.CONSUME },
{label: '收入', value: BillType.INCOME}, { label: '收入', value: BillType.INCOME },
]; ];
// 提交到表格 // 提交到表格
@ -120,7 +120,7 @@ function Record() {
const failures = [] const failures = []
for (let bill of datasource) { for (let bill of datasource) {
try { try {
const {id} = await createBill(bill) const { id } = await createBill(bill)
if (!id) failures.push(bill) if (!id) failures.push(bill)
} catch (e) { } catch (e) {
failures.push(bill) failures.push(bill)
@ -133,10 +133,10 @@ function Record() {
let classData: string[] = [] let classData: string[] = []
switch (billType) { switch (billType) {
case BillType.CONSUME: case BillType.CONSUME:
classData = Object.keys(cls2label.consume) classData = cls2label.consume.map(it => it.name)
break break
case BillType.INCOME: case BillType.INCOME:
classData = cls2label.income classData = cls2label.income.map(it => it.name)
break break
} }
@ -145,7 +145,7 @@ function Record() {
<div className={styles.new}> <div className={styles.new}>
<Space align="start"> <Space align="start">
<Radio.Group <Radio.Group
style={{width: 120}} style={{ width: 120 }}
options={typeOpt} options={typeOpt}
optionType="button" optionType="button"
buttonStyle="solid" buttonStyle="solid"
@ -153,14 +153,14 @@ function Record() {
onChange={e => setBillType(e.target.value)} onChange={e => setBillType(e.target.value)}
/> />
<DatePicker <DatePicker
style={{width: 120}} style={{ width: 120 }}
allowClear={false} allowClear={false}
value={moment(date, 'YYYY-MM-DD')} value={moment(date, 'YYYY-MM-DD')}
onChange={(_, dateStr) => setDate(dateStr)} onChange={(_, dateStr) => setDate(dateStr)}
/> />
<Select <Select
ref={clsRef} ref={clsRef}
style={{width: 120}} style={{ width: 120 }}
// showSearch // showSearch
placeholder="类别" placeholder="类别"
optionFilterProp="children" optionFilterProp="children"
@ -170,7 +170,7 @@ function Record() {
value={cls === "" ? null : cls} value={cls === "" ? null : cls}
onChange={c => { onChange={c => {
setCls(c) setCls(c)
if (billType === BillType.CONSUME) setLabel(cls2label.consume[c][0]) // if (billType === BillType.CONSUME) setLabel(cls2label.consume[c][0])
}} }}
> >
{ {
@ -179,7 +179,7 @@ function Record() {
</Select> </Select>
{billType === BillType.CONSUME && ( {billType === BillType.CONSUME && (
<Select <Select
style={{width: 120}} style={{ width: 120 }}
// showSearch // showSearch
placeholder="标签" placeholder="标签"
optionFilterProp="children" optionFilterProp="children"
@ -189,14 +189,15 @@ function Record() {
value={label === "" ? null : label} value={label === "" ? null : label}
onChange={setLabel}> onChange={setLabel}>
{cls !== "" && {cls !== "" &&
cls2label.consume[cls] cls2label.consume
.find(it => it.name === cls)?.labels
.map(la => <Select.Option key={la} value={la}>{la}</Select.Option>) .map(la => <Select.Option key={la} value={la}>{la}</Select.Option>)
} }
<Select.Option key={"other"} value={"其他"}>{"其他"}</Select.Option>) <Select.Option key={"other"} value={"其他"}>{"其他"}</Select.Option>)
</Select> </Select>
)} )}
<InputNumber <InputNumber
style={{width: 120}} style={{ width: 120 }}
placeholder="money" placeholder="money"
prefix="¥" prefix="¥"
value={money} value={money}
@ -204,7 +205,7 @@ function Record() {
onKeyDown={e => e.key === "Enter" && submit()} onKeyDown={e => e.key === "Enter" && submit()}
/> />
<Input.TextArea <Input.TextArea
style={{width: 180}} style={{ width: 180 }}
rows={1} rows={1}
placeholder="备注" placeholder="备注"
value={options} value={options}
@ -213,7 +214,7 @@ function Record() {
/> />
<Button <Button
type="primary" type="primary"
icon={<ArrowDownOutlined/>} icon={<ArrowDownOutlined />}
onKeyUp={e => e.key === "Tab" && clsRef.current!.focus()} onKeyUp={e => e.key === "Tab" && clsRef.current!.focus()}
onClick={submit} onClick={submit}
> >
@ -228,7 +229,7 @@ function Record() {
size="small" size="small"
/> />
<Button <Button
icon={<CloudUploadOutlined/>} icon={<CloudUploadOutlined />}
type="primary" type="primary"
loading={uploadLoading} loading={uploadLoading}
onClick={upload} onClick={upload}

View File

@ -3,6 +3,8 @@ import { createContext } from "react";
import { createBill, getBills, getLabels } from "../api/bills"; import { createBill, getBills, getLabels } from "../api/bills";
import { BillType, IBill } from "../model"; import { BillType, IBill } from "../model";
import * as R from "ramda" import * as R from "ramda"
import { BillLabel } from "./types";
/** /**
* *
@ -10,7 +12,7 @@ import * as R from "ramda"
export class Bill { export class Bill {
private _bills: IBill[] = []; private _bills: IBill[] = [];
// _cls2label: IClass = {consume: new Map<string, string[]>(), income: []} // _cls2label: IClass = {consume: new Map<string, string[]>(), income: []}
private _cls2label: { consume: [], income: [] } = { consume: [], income: [] } private _cls2label: BillLabel = { consume: [], income: [] }
constructor() { constructor() {
makeAutoObservable(this) makeAutoObservable(this)
@ -135,7 +137,7 @@ export class Bill {
async fetchLabels() { async fetchLabels() {
const cls2label = await getLabels() const cls2label = await getLabels()
runInAction(() => { runInAction(() => {
this._cls2label == cls2label this._cls2label = cls2label
}) })
} }
} }

9
src/store/types.ts Normal file
View File

@ -0,0 +1,9 @@
export type BillLabel = {
consume: Array<BillLabelOption>,
income: Array<BillLabelOption>,
}
type BillLabelOption = {
name: string,
labels: Array<string>,
}