Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { Loader, Select, SelectProps } from "@mantine/core";
|
||||
import {
|
||||
MarketplaceAuthData,
|
||||
} from "../../../pages/MarketplacesPage/modals/MarketplaceFormModal/MarketplaceFormModal.tsx";
|
||||
import { omit } from "lodash";
|
||||
import { useEffect, useState } from "react";
|
||||
import { MarketplaceService, YandexMarketCampaignSchema } from "../../../client";
|
||||
|
||||
type RestProps = {
|
||||
authData: MarketplaceAuthData;
|
||||
onChange(value: YandexMarketCampaignSchema): void;
|
||||
value?: string;
|
||||
|
||||
}
|
||||
type Props = Omit<SelectProps, "data" | "value" | "onChange"> & RestProps;
|
||||
|
||||
const YandexMarketCampaignSelect = (props: Props) => {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
const [data, setData] = useState<{ label: string, value: string }[]>([]);
|
||||
const restProps = omit(props, ["authData", "onChange", "value"]);
|
||||
const { authData } = props;
|
||||
const fetchData = async () => {
|
||||
const apiKey = authData["Api-Key"];
|
||||
if (!apiKey || !apiKey.startsWith("ACMA")) return;
|
||||
setIsLoading(true);
|
||||
MarketplaceService.getYandexMarketCampaigns({ requestBody: { apiKey: apiKey } }).then(response => {
|
||||
const campaigns = response.campaigns;
|
||||
const data = campaigns.map((campaign) => ({
|
||||
value: campaign.id.toString(),
|
||||
label: campaign.name,
|
||||
}));
|
||||
setData(data);
|
||||
setIsLoading(false);
|
||||
});
|
||||
};
|
||||
const onChange = (value: string | null) => {
|
||||
const selected = data.find(item => item.value === value);
|
||||
if (selected) {
|
||||
props.onChange({ id: parseInt(selected.value), name: selected.label });
|
||||
}
|
||||
};
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [authData["Api-Key"]]);
|
||||
return (
|
||||
<Select
|
||||
{...restProps}
|
||||
disabled={isLoading}
|
||||
rightSection={isLoading && <Loader size={16} />}
|
||||
value={props.value?.toString()}
|
||||
onChange={onChange}
|
||||
data={data}
|
||||
label="Магазин"
|
||||
placeholder="Выберите магазин"
|
||||
searchable
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default YandexMarketCampaignSelect;
|
||||
Reference in New Issue
Block a user