feat: a lot of a lot

This commit is contained in:
2024-09-01 01:05:20 +03:00
parent b93bfe39d5
commit 45d80b7c86
38 changed files with 1000 additions and 6 deletions

View File

@@ -0,0 +1,115 @@
import {useEffect, useState} from "react";
import {ClientSchema, MarketplaceSchema, MarketplaceService, TaskService} from "../../../client";
import {notifications} from "../../../shared/lib/notifications.ts";
import {RootState, useAppDispatch} from "../../../redux/store.ts";
import {addTask} from "../../../features/tasksSlice.tsx";
import {useSelector} from "react-redux";
const useMarketplacesPageState = () => {
const dispatch = useAppDispatch();
const tasks = useSelector((state: RootState) => state.tasks.tasks);
const [client, setClient] = useState<ClientSchema | undefined>();
const [items, setItems] = useState<MarketplaceSchema[]>([]);
const fetchMarketplaces = async () => {
if (!client) return;
MarketplaceService.getClientMarketplaces({
requestBody: {
clientId: client.id
}
}).then((response) => {
setItems(response.marketplaces);
})
}
const onCreate = (marketplace: MarketplaceSchema) => {
MarketplaceService.createMarketplace({
requestBody: {
marketplace: {
...marketplace,
clientId: marketplace.client.id,
baseMarketplaceKey: marketplace.baseMarketplace.key
}
}
}).then(async ({ok, message}) => {
notifications.guess(ok, {message});
if (!ok) return;
await fetchMarketplaces();
})
}
const onDelete = (marketplace: MarketplaceSchema) => {
MarketplaceService.deleteMarketplace({
requestBody: {
marketplaceId: marketplace.id
}
}).then(async ({ok, message}) => {
notifications.guess(ok, {message});
if (!ok) return;
await fetchMarketplaces();
})
}
const onChange = (marketplace: MarketplaceSchema) => {
MarketplaceService.updateMarketplace({
requestBody: {
marketplace: marketplace
}
}).then(async ({ok, message}) => {
notifications.guess(ok, {message});
if (!ok) return;
await fetchMarketplaces();
})
}
const onSynchronize = (marketplace: MarketplaceSchema) => {
// If there is already synchronization task for this marketplace show notifications.error()
const task = tasks.find(task => task.info.marketplaceId === marketplace.id);
if (task) {
notifications.error({
title: 'Ошибка',
message: `Синхронизация маркетплейса ${marketplace.name} уже запущена`
});
return;
}
TaskService.createSynchronizeMarketplaceTask({
requestBody: {
marketplaceId:
marketplace.id
}
}).then(({taskId}) => {
dispatch(addTask({
id: taskId,
config: {
onErrorData: {
title: 'Ошибка',
message: `Ошибка синхронизации маркетплейса: ${marketplace.name}`
},
onLoadingData: {
title: 'Синхронизация',
message: `Синхронизация маркетплейса: ${marketplace.name}`
},
onSuccessData: {
title: 'Успех',
message: `Маркетплейс ${marketplace.name} успешно синхронизирован`
}
},
info: {
marketplaceId: marketplace.id
}
}));
})
}
useEffect(() => {
fetchMarketplaces();
}, [client]);
return {
client,
setClient,
items,
onDelete,
onChange,
onCreate,
onSynchronize
}
}
export default useMarketplacesPageState;