refactor: tariffs API
This commit is contained in:
parent
048d82bb23
commit
68a2142b66
@ -1,44 +1,70 @@
|
||||
import { Tariff, makeRequest } from "@frontend/kitui";
|
||||
import { makeRequest } from "@frontend/kitui";
|
||||
import { EditTariffRequestBody } from "@root/model/tariff";
|
||||
|
||||
import { parseAxiosError } from "@root/utils/parse-error";
|
||||
|
||||
const baseUrl = process.env.NODE_ENV === "production" ? "/strator" : "https://admin.pena.digital/strator";
|
||||
import type { Tariff } from "@frontend/kitui";
|
||||
|
||||
export function putTariff(tariff: Tariff) {
|
||||
return makeRequest<EditTariffRequestBody, never>({
|
||||
method: "put",
|
||||
url: baseUrl + `/tariff/${tariff._id}`,
|
||||
body: {
|
||||
name: tariff.name,
|
||||
price: tariff.price ?? 0,
|
||||
isCustom: false,
|
||||
privilegies: tariff.privilegies,
|
||||
},
|
||||
const baseUrl =
|
||||
process.env.NODE_ENV === "production"
|
||||
? "/strator"
|
||||
: "https://admin.pena.digital/strator";
|
||||
|
||||
export const putTariff = async (tariff: Tariff): Promise<[null, string?]> => {
|
||||
try {
|
||||
const putedTariffResponse = await makeRequest<EditTariffRequestBody, null>({
|
||||
method: "put",
|
||||
url: baseUrl + `/tariff/${tariff._id}`,
|
||||
body: {
|
||||
name: tariff.name,
|
||||
price: tariff.price ?? 0,
|
||||
isCustom: false,
|
||||
privilegies: tariff.privilegies,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteTariff(tariffId: string) {
|
||||
return makeRequest<{ id: string; }, never>({
|
||||
method: "delete",
|
||||
url: baseUrl + "/tariff",
|
||||
body: { id: tariffId },
|
||||
return [putedTariffResponse];
|
||||
} catch (nativeError) {
|
||||
const [error] = parseAxiosError(nativeError);
|
||||
|
||||
return [null, `Ошибка редактирования тарифа. ${error}`];
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteTariff = async (
|
||||
tariffId: string
|
||||
): Promise<[Tariff | null, string?]> => {
|
||||
try {
|
||||
const deletedTariffResponse = await makeRequest<{ id: string }, Tariff>({
|
||||
method: "delete",
|
||||
url: baseUrl + "/tariff",
|
||||
body: { id: tariffId },
|
||||
});
|
||||
}
|
||||
|
||||
return [deletedTariffResponse];
|
||||
} catch (nativeError) {
|
||||
const [error] = parseAxiosError(nativeError);
|
||||
|
||||
return [null, `Ошибка удаления тарифа. ${error}`];
|
||||
}
|
||||
};
|
||||
|
||||
export async function deleteManyTariffs(tariffIds: string[]) {
|
||||
const results = await Promise.allSettled(tariffIds.map(tariffId => deleteTariff(tariffId)));
|
||||
const results = await Promise.allSettled(
|
||||
tariffIds.map((tariffId) => deleteTariff(tariffId))
|
||||
);
|
||||
|
||||
let deletedCount = 0;
|
||||
let errorCount = 0;
|
||||
const errors: unknown[] = [];
|
||||
let deletedCount = 0;
|
||||
let errorCount = 0;
|
||||
const errors: unknown[] = [];
|
||||
|
||||
results.forEach(result => {
|
||||
if (result.status === "fulfilled") deletedCount++;
|
||||
else {
|
||||
errorCount++;
|
||||
errors.push(result.reason);
|
||||
}
|
||||
});
|
||||
results.forEach((result) => {
|
||||
if (result.status === "fulfilled") deletedCount++;
|
||||
else {
|
||||
errorCount++;
|
||||
errors.push(result.reason);
|
||||
}
|
||||
});
|
||||
|
||||
return { deletedCount, errorCount, errors };
|
||||
return { deletedCount, errorCount, errors };
|
||||
}
|
||||
|
||||
@ -10,104 +10,103 @@ import { closeEditTariffDialog, useTariffStore } from "@root/stores/tariffs";
|
||||
import { putTariff } from "@root/api/tariffs";
|
||||
import { requestTariffs } from "@root/services/tariffs.service";
|
||||
|
||||
|
||||
export default function EditModal() {
|
||||
const [nameField, setNameField] = useState("");
|
||||
const [priceField, setPriceField] = useState("");
|
||||
const tariffs = useTariffStore((state) => state.tariffs);
|
||||
const editTariffId = useTariffStore(state => state.editTariffId);
|
||||
const [nameField, setNameField] = useState("");
|
||||
const [priceField, setPriceField] = useState("");
|
||||
const tariffs = useTariffStore((state) => state.tariffs);
|
||||
const editTariffId = useTariffStore((state) => state.editTariffId);
|
||||
|
||||
const tariff = tariffs.find(tariff => tariff._id === editTariffId);
|
||||
const tariff = tariffs.find((tariff) => tariff._id === editTariffId);
|
||||
|
||||
useEffect(function setCurrentTariffFields() {
|
||||
if (!tariff) return;
|
||||
useEffect(
|
||||
function setCurrentTariffFields() {
|
||||
if (!tariff) return;
|
||||
|
||||
setNameField(tariff.name);
|
||||
setPriceField((tariff.price || 0).toString());
|
||||
}, [tariff]);
|
||||
setNameField(tariff.name);
|
||||
setPriceField((tariff.price || 0).toString());
|
||||
},
|
||||
[tariff]
|
||||
);
|
||||
|
||||
async function handleEditClick() {
|
||||
if (!tariff) return enqueueSnackbar(`Тариф ${editTariffId} не найден`);
|
||||
async function handleEditClick() {
|
||||
if (!tariff) return enqueueSnackbar(`Тариф ${editTariffId} не найден`);
|
||||
|
||||
const price = parseFloat(priceField);
|
||||
const price = parseFloat(priceField);
|
||||
|
||||
if (!isFinite(price)) return enqueueSnackbar("Поле \"Цена за единицу\" не число");
|
||||
if (!nameField) return enqueueSnackbar("Поле \"Имя\" пустое");
|
||||
if (!isFinite(price))
|
||||
return enqueueSnackbar('Поле "Цена за единицу" не число');
|
||||
if (!nameField) return enqueueSnackbar('Поле "Имя" пустое');
|
||||
|
||||
const updatedTariff = structuredClone(tariff);
|
||||
updatedTariff.name = nameField;
|
||||
updatedTariff.price = price;
|
||||
try {
|
||||
await putTariff(updatedTariff);
|
||||
closeEditTariffDialog();
|
||||
|
||||
requestTariffs();
|
||||
} catch (error) {
|
||||
devlog("Error updating tariff", error);
|
||||
const message = getMessageFromFetchError(error);
|
||||
if (message) enqueueSnackbar(message);
|
||||
}
|
||||
const updatedTariff = structuredClone(tariff);
|
||||
updatedTariff.name = nameField;
|
||||
updatedTariff.price = price;
|
||||
|
||||
const [_, putedTariffError] = await putTariff(updatedTariff);
|
||||
|
||||
if (putedTariffError) {
|
||||
devlog("Error updating tariff", putedTariffError);
|
||||
|
||||
return enqueueSnackbar(putedTariffError);
|
||||
}
|
||||
closeEditTariffDialog();
|
||||
requestTariffs();
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal
|
||||
open={editTariffId !== null}
|
||||
onClose={closeEditTariffDialog}
|
||||
aria-labelledby="modal-modal-title"
|
||||
aria-describedby="modal-modal-description"
|
||||
return (
|
||||
<Modal
|
||||
open={editTariffId !== null}
|
||||
onClose={closeEditTariffDialog}
|
||||
aria-labelledby="modal-modal-title"
|
||||
aria-describedby="modal-modal-description"
|
||||
>
|
||||
<Box
|
||||
sx={{
|
||||
position: "absolute",
|
||||
top: "50%",
|
||||
left: "50%",
|
||||
transform: "translate(-50%, -50%)",
|
||||
width: 400,
|
||||
bgcolor: "background.paper",
|
||||
border: "2px solid gray",
|
||||
borderRadius: "6px",
|
||||
boxShadow: 24,
|
||||
p: 4,
|
||||
}}
|
||||
>
|
||||
<Typography
|
||||
id="modal-modal-title"
|
||||
variant="h6"
|
||||
component="h2"
|
||||
sx={{ whiteSpace: "nowrap" }}
|
||||
>
|
||||
<Box
|
||||
sx={{
|
||||
position: "absolute",
|
||||
top: "50%",
|
||||
left: "50%",
|
||||
transform: "translate(-50%, -50%)",
|
||||
width: 400,
|
||||
bgcolor: "background.paper",
|
||||
border: "2px solid gray",
|
||||
borderRadius: "6px",
|
||||
boxShadow: 24,
|
||||
p: 4,
|
||||
}}
|
||||
>
|
||||
<Typography
|
||||
id="modal-modal-title"
|
||||
variant="h6"
|
||||
component="h2"
|
||||
sx={{ whiteSpace: "nowrap" }}
|
||||
>
|
||||
Редактирование тариффа
|
||||
</Typography>
|
||||
Редактирование тариффа
|
||||
</Typography>
|
||||
|
||||
{tariff && (
|
||||
<Box sx={{ mt: "20px", display: "flex", flexDirection: "column" }}>
|
||||
<Typography>Название Тарифа: {tariff.name}</Typography>
|
||||
<TextField
|
||||
onChange={(event) => setNameField(event.target.value)}
|
||||
label="Имя"
|
||||
name="name"
|
||||
value={nameField}
|
||||
sx={{ marginBottom: "10px" }}
|
||||
/>
|
||||
<Typography>
|
||||
Цена за единицу: {tariff.privilegies[0].price}
|
||||
</Typography>
|
||||
<TextField
|
||||
type="number"
|
||||
onChange={(event) => setPriceField(event.target.value)}
|
||||
label="Цена за единицу"
|
||||
name="price"
|
||||
value={priceField}
|
||||
sx={{ marginBottom: "10px" }}
|
||||
/>
|
||||
<Button
|
||||
onClick={handleEditClick}
|
||||
>
|
||||
Редактировать
|
||||
</Button>
|
||||
</Box>
|
||||
)}
|
||||
</Box>
|
||||
</Modal>
|
||||
);
|
||||
{tariff && (
|
||||
<Box sx={{ mt: "20px", display: "flex", flexDirection: "column" }}>
|
||||
<Typography>Название Тарифа: {tariff.name}</Typography>
|
||||
<TextField
|
||||
onChange={(event) => setNameField(event.target.value)}
|
||||
label="Имя"
|
||||
name="name"
|
||||
value={nameField}
|
||||
sx={{ marginBottom: "10px" }}
|
||||
/>
|
||||
<Typography>
|
||||
Цена за единицу: {tariff.privilegies[0].price}
|
||||
</Typography>
|
||||
<TextField
|
||||
type="number"
|
||||
onChange={(event) => setPriceField(event.target.value)}
|
||||
label="Цена за единицу"
|
||||
name="price"
|
||||
value={priceField}
|
||||
sx={{ marginBottom: "10px" }}
|
||||
/>
|
||||
<Button onClick={handleEditClick}>Редактировать</Button>
|
||||
</Box>
|
||||
)}
|
||||
</Box>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user