feat: handler modal dynamic

This commit is contained in:
caturbgs 2021-12-22 14:43:04 +07:00
parent 7edf7650ec
commit 7cf403f19d
3 changed files with 55 additions and 47 deletions

View File

@ -15,9 +15,8 @@ export const ProductComponent = observer((props) => {
const {Option} = Select; const {Option} = Select;
const history = useHistory(); const history = useHistory();
const [idData, setIdData] = useState(""); const [idData, setIdData] = useState("");
const [filterSupplier, setFilterSupplier] = useState(null); const [filterSupplier, setFilterSupplier] = useState([]);
const [filterCategories, setFilterCategories] = useState(null); const [filterSubCategories, setFilterSubCategories] = useState([]);
const [filterSubCategories, setFilterSubCategories] = useState(null);
const modalLoader = useContext(ModalLoaderContext); const modalLoader = useContext(ModalLoaderContext);
const handleEditButton = (data) => { const handleEditButton = (data) => {
@ -163,17 +162,17 @@ export const ProductComponent = observer((props) => {
const handleRemoveFilter = async () => { const handleRemoveFilter = async () => {
store.product.filterSupplier = null; store.product.filterSupplier = null;
store.product.filterSubCategory = null; store.product.filterSubCategory = null;
setFilterSupplier(null); setFilterSupplier([]);
setFilterCategories(null); store.product.filterCategory = null;
setFilterSubCategories(null); setFilterSubCategories([]);
await store.product.getData(); await store.product.getData();
store.product.visibleModalFilterProduct = false; store.product.visibleModalFilterProduct = false;
}; };
const handleCancelFilter = () => { const handleCancelFilter = () => {
setFilterSupplier(null); setFilterSupplier([]);
setFilterCategories(null); store.product.filterCategory = null;
setFilterSubCategories(null); setFilterSubCategories([]);
store.product.visibleModalFilterProduct = false; store.product.visibleModalFilterProduct = false;
}; };
@ -186,14 +185,24 @@ export const ProductComponent = observer((props) => {
store.product.visibleModalFilterProduct = false; store.product.visibleModalFilterProduct = false;
}; };
const handleFilterCategory = async (value) => {
if (value) {
store.product.filterCategory = value;
await store.product.getDataSubCategories();
} else {
store.product.filterCategory = null;
await store.product.getDataSubCategories();
}
};
const footerLayoutFilter = [ const footerLayoutFilter = [
<Button <Button
key={"remove"} key={"remove"}
onClick={handleRemoveFilter} onClick={handleRemoveFilter}
style={{ style={{
backgroundColor: "#e74e5e", backgroundColor: "#e74e5e",
color: "#fff", color: "#fff",
}} }}
> >
Remove Filter Remove Filter
</Button>, </Button>,
@ -403,17 +412,18 @@ export const ProductComponent = observer((props) => {
Filter Supplier Filter Supplier
</Title> </Title>
<Select <Select
mode={"multiple"} mode={"multiple"}
placeholder="Choose Supplier" placeholder="Choose Supplier"
onChange={(val) => { onChange={(val) => {
setFilterSupplier(val); setFilterSupplier(val);
}} }}
style={{ marginBottom: "20px", width: "100%" }} style={{marginBottom: "20px", width: "100%"}}
value={filterSupplier}
> >
{store.supplier.data.map((item) => ( {store.supplier.data.map((item) => (
<Option value={item.id} key={item.id}> <Option value={item.id} key={item.id}>
{item.name} {item.name}
</Option> </Option>
))} ))}
</Select> </Select>
</Col> </Col>
@ -422,19 +432,16 @@ export const ProductComponent = observer((props) => {
Filter Categories Filter Categories
</Title> </Title>
<Select <Select
mode={"multiple"} mode={"multiple"}
placeholder="Choose Category" placeholder="Choose Category"
onChange={async (val) => { onChange={async (val) => handleFilterCategory(val)}
setFilterCategories(val); style={{marginBottom: "20px", width: "100%"}}
store.product.filterByCategory = val; value={store.product.filterCategory || []}
await store.product.getDataSubCategories();
}}
style={{ marginBottom: "20px", width: "100%" }}
> >
{store.category.data.map((item) => ( {store.category.data.map((item) => (
<Option value={item.id} key={item.id}> <Option value={item.id} key={item.id}>
{item.name} {item.name}
</Option> </Option>
))} ))}
</Select> </Select>
</Col> </Col>
@ -443,17 +450,18 @@ export const ProductComponent = observer((props) => {
Filter Sub-Categories Filter Sub-Categories
</Title> </Title>
<Select <Select
mode={"multiple"} mode={"multiple"}
placeholder="Choose Sub-Category" placeholder="Choose Sub-Category"
onChange={(val) => { onChange={(val) => {
setFilterSubCategories(val); setFilterSubCategories(val);
}} }}
style={{ marginBottom: "20px", width: "100%" }} style={{marginBottom: "20px", width: "100%"}}
value={filterSubCategories}
> >
{store.product.dataSubCategories.map((item) => ( {store.product.dataSubCategories.map((item) => (
<Option value={item.id} key={item.id}> <Option value={item.id} key={item.id}>
{item.name} {item.name}
</Option> </Option>
))} ))}
</Select> </Select>
</Col> </Col>

View File

@ -21,7 +21,7 @@ export const Product = observer(() => {
await Promise.allSettled([ await Promise.allSettled([
store.supplier.getData(), store.supplier.getData(),
store.category.getData(), store.category.getData(),
store.category.getDataSubCategories(), store.product.getDataSubCategories(),
]); ]);
await store.product.getData(); await store.product.getData();
modalLoader.setLoading(false); modalLoader.setLoading(false);

View File

@ -21,7 +21,7 @@ export class Product {
pageSizeSubCategories = 10 pageSizeSubCategories = 10
dataSubCategories = []; dataSubCategories = [];
total_dataSubCategories = 0; total_dataSubCategories = 0;
filterByCategory = null; filterCategory = null;
constructor(ctx) { constructor(ctx) {
this.ctx = ctx; this.ctx = ctx;
@ -44,7 +44,7 @@ export class Product {
async getDataSubCategories() { async getDataSubCategories() {
try { try {
const response = await http.get(`/product/sub-categories?category=${this.filterByCategory}&page=${this.pageSubCategories}&pageSize=${this.pageSizeSubCategories}`); const response = await http.get(`/product/sub-categories?category=${this.filterCategory}&page=${this.pageSubCategories}&pageSize=${this.pageSizeSubCategories}`);
this.dataSubCategories = response.body.data.map((item, idx) => { this.dataSubCategories = response.body.data.map((item, idx) => {
item.key = idx; item.key = idx;
return item return item