208 lines
6.8 KiB
JavaScript
208 lines
6.8 KiB
JavaScript
import { Button, Col, Form, Input, Row, Select } from "antd";
|
|
import React, { useEffect, useState } from "react";
|
|
import { fetchFilterData } from "../../utils/RenderTemplate";
|
|
import { DeleteFilled } from "@ant-design/icons";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
export default function Test({ form }) {
|
|
return (
|
|
<Form.Item style={{ margin: 0, padding: 0 }} dependencies={["key"]}>
|
|
{() => {
|
|
const key = form.getFieldValue("key");
|
|
return <RenderFilters templateId={key} />;
|
|
}}
|
|
</Form.Item>
|
|
);
|
|
}
|
|
|
|
function RenderFilters({ templateId }) {
|
|
const [state, setState] = useState(null);
|
|
console.log("state", state);
|
|
const { t } = useTranslation();
|
|
useEffect(() => {
|
|
const fetch = async () => {
|
|
const data = await fetchFilterData({ name: templateId });
|
|
console.log("🚀 ~ fetch ~ data:", data);
|
|
setState(JSON.parse(data));
|
|
};
|
|
|
|
console.log("🚀 ~ useEffect ~ templateId:", templateId);
|
|
if (templateId) {
|
|
fetch();
|
|
}
|
|
}, [templateId]);
|
|
|
|
if (!templateId || !state) return null;
|
|
return (
|
|
<div>
|
|
<Form.List name={["filters"]}>
|
|
{(fields, { add, remove, move }) => {
|
|
return (
|
|
<div>
|
|
Filters
|
|
{fields.map((field, index) => (
|
|
<Form.Item key={field.key}>
|
|
<Row gutter={[16, 16]}>
|
|
<Col span={10}>
|
|
<Form.Item
|
|
key={`${index}field`}
|
|
label="field"
|
|
name={[field.name, "field"]}
|
|
rules={[
|
|
{
|
|
required: true,
|
|
//message: t("general.validation.required"),
|
|
},
|
|
]}
|
|
>
|
|
<Select
|
|
options={
|
|
state.filters
|
|
? state.filters.map((f) => ({
|
|
value: f.name,
|
|
label: t(f.translation),
|
|
}))
|
|
: []
|
|
}
|
|
/>
|
|
</Form.Item>
|
|
</Col>
|
|
<Col span={6}>
|
|
<Form.Item
|
|
key={`${index}operator`}
|
|
label="operator"
|
|
name={[field.name, "operator"]}
|
|
rules={[
|
|
{
|
|
required: true,
|
|
//message: t("general.validation.required"),
|
|
},
|
|
]}
|
|
>
|
|
<Select />
|
|
</Form.Item>
|
|
</Col>
|
|
<Col span={6}>
|
|
<Form.Item
|
|
key={`${index}value`}
|
|
label="value"
|
|
name={[field.name, "value"]}
|
|
rules={[
|
|
{
|
|
required: true,
|
|
//message: t("general.validation.required"),
|
|
},
|
|
]}
|
|
>
|
|
<Input />
|
|
</Form.Item>
|
|
</Col>
|
|
<Col span={2}>
|
|
<DeleteFilled
|
|
style={{ margin: "1rem" }}
|
|
onClick={() => {
|
|
remove(field.name);
|
|
}}
|
|
/>
|
|
</Col>
|
|
</Row>
|
|
</Form.Item>
|
|
))}
|
|
<Form.Item>
|
|
<Button
|
|
type="dashed"
|
|
onClick={() => {
|
|
add();
|
|
}}
|
|
style={{ width: "100%" }}
|
|
>
|
|
{t("general.actions.add")}
|
|
</Button>
|
|
</Form.Item>
|
|
</div>
|
|
);
|
|
}}
|
|
</Form.List>
|
|
<Form.List name={["sorters"]}>
|
|
{(fields, { add, remove, move }) => {
|
|
return (
|
|
<div>
|
|
Sorters
|
|
{fields.map((field, index) => (
|
|
<Form.Item key={field.key}>
|
|
<Row gutter={[16, 16]}>
|
|
<Col span={11}>
|
|
<Form.Item
|
|
key={`${index}field`}
|
|
label="field"
|
|
name={[field.name, "field"]}
|
|
rules={[
|
|
{
|
|
required: true,
|
|
//message: t("general.validation.required"),
|
|
},
|
|
]}
|
|
>
|
|
<Select
|
|
options={
|
|
state.sorters
|
|
? state.sorters.map((f) => ({
|
|
value: f.name,
|
|
label: t(f.name),
|
|
}))
|
|
: []
|
|
}
|
|
/>
|
|
</Form.Item>
|
|
</Col>
|
|
<Col span={11}>
|
|
<Form.Item
|
|
key={`${index}direction`}
|
|
label="direction"
|
|
name={[field.name, "direction"]}
|
|
rules={[
|
|
{
|
|
required: true,
|
|
//message: t("general.validation.required"),
|
|
},
|
|
]}
|
|
>
|
|
<Select
|
|
options={[
|
|
{ value: "desc", label: "Descending" },
|
|
{ value: "asc", label: "Ascending" },
|
|
]}
|
|
/>
|
|
</Form.Item>
|
|
</Col>
|
|
|
|
<Col span={2}>
|
|
<DeleteFilled
|
|
style={{ margin: "1rem" }}
|
|
onClick={() => {
|
|
remove(field.name);
|
|
}}
|
|
/>
|
|
</Col>
|
|
</Row>
|
|
</Form.Item>
|
|
))}
|
|
<Form.Item>
|
|
<Button
|
|
type="dashed"
|
|
onClick={() => {
|
|
add();
|
|
}}
|
|
style={{ width: "100%" }}
|
|
>
|
|
{t("general.actions.add")}
|
|
</Button>
|
|
</Form.Item>
|
|
</div>
|
|
);
|
|
}}
|
|
</Form.List>
|
|
</div>
|
|
);
|
|
}
|