mirror of
https://github.com/codex-storage/codex-marketplace-ui.git
synced 2025-02-24 13:48:14 +00:00
Improve error management
This commit is contained in:
parent
ca61077fca
commit
3605aa3d4f
@ -1,24 +1,22 @@
|
|||||||
import {
|
import {
|
||||||
Input,
|
Input,
|
||||||
InputGroup,
|
InputGroup,
|
||||||
SpaceAllocation,
|
|
||||||
StepperAction,
|
StepperAction,
|
||||||
StepperState,
|
StepperState,
|
||||||
} from "@codex-storage/marketplace-ui-components";
|
} from "@codex-storage/marketplace-ui-components";
|
||||||
import { ChangeEvent, Dispatch, useState } from "react";
|
import { ChangeEvent, Dispatch, useCallback, useEffect, useState } from "react";
|
||||||
import "./AvailabilityForm.css";
|
import "./AvailabilityForm.css";
|
||||||
import { CodexNodeSpace } from "@codex-storage/sdk-js";
|
import { CodexNodeSpace } from "@codex-storage/sdk-js";
|
||||||
import { UIAvailability } from "./types";
|
import { UIAvailability } from "./types";
|
||||||
import { GB, TB } from "../../utils/constants";
|
import { GB, TB } from "../../utils/constants";
|
||||||
import { classnames } from "../../utils/classnames";
|
import { classnames } from "../../utils/classnames";
|
||||||
import { AvailabilityConfirm } from "./AvailabilityConfirmation";
|
|
||||||
import { AvailabilitySpaceAllocation } from "./AvailabilitySpaceAllocation";
|
import { AvailabilitySpaceAllocation } from "./AvailabilitySpaceAllocation";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
dispatch: Dispatch<StepperAction>;
|
dispatch: Dispatch<StepperAction>;
|
||||||
state: StepperState;
|
state: StepperState;
|
||||||
space: CodexNodeSpace;
|
space: CodexNodeSpace;
|
||||||
onAvailabilityChange: (data: Partial<UIAvailability>, valid: boolean) => void;
|
onAvailabilityChange: (data: Partial<UIAvailability>) => void;
|
||||||
availability: UIAvailability;
|
availability: UIAvailability;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -30,6 +28,22 @@ export function AvailabilityForm({
|
|||||||
}: Props) {
|
}: Props) {
|
||||||
const [totalSizeError, setTotalSizeError] = useState("");
|
const [totalSizeError, setTotalSizeError] = useState("");
|
||||||
|
|
||||||
|
const isAvailabilityInvalid = useCallback(
|
||||||
|
(value: number) => {
|
||||||
|
const unit = availability.totalSizeUnit === "gb" ? GB : TB;
|
||||||
|
return value * unit > space.quotaMaxBytes - space.quotaReservedBytes;
|
||||||
|
},
|
||||||
|
[space]
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isAvailabilityInvalid(availability.totalSize)) {
|
||||||
|
setTotalSizeError(
|
||||||
|
"You cannot allocate more space than the remaining space."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}, [availability]);
|
||||||
|
|
||||||
const onTotalSizeUnitChange = async (e: ChangeEvent<HTMLSelectElement>) => {
|
const onTotalSizeUnitChange = async (e: ChangeEvent<HTMLSelectElement>) => {
|
||||||
const element = e.currentTarget;
|
const element = e.currentTarget;
|
||||||
const valid = element.value === "tb" || element.value === "gb";
|
const valid = element.value === "tb" || element.value === "gb";
|
||||||
@ -39,75 +53,63 @@ export function AvailabilityForm({
|
|||||||
isNextEnable: false,
|
isNextEnable: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
onAvailabilityChange(
|
onAvailabilityChange({
|
||||||
{
|
totalSize: 0,
|
||||||
totalSize: 0,
|
totalSizeUnit: element.value as "tb" | "gb",
|
||||||
totalSizeUnit: element.value as "tb" | "gb",
|
});
|
||||||
},
|
|
||||||
valid
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onDurationUnitChange = async (e: ChangeEvent<HTMLSelectElement>) => {
|
const onDurationUnitChange = async (e: ChangeEvent<HTMLSelectElement>) => {
|
||||||
const element = e.currentTarget;
|
const element = e.currentTarget;
|
||||||
const valid =
|
|
||||||
element.value === "hours" ||
|
|
||||||
element.value === "days" ||
|
|
||||||
element.value === "months";
|
|
||||||
|
|
||||||
onAvailabilityChange(
|
onAvailabilityChange({
|
||||||
{
|
duration: 1,
|
||||||
duration: 1,
|
durationUnit: element.value as "hours" | "days" | "months",
|
||||||
durationUnit: element.value as "hours" | "days" | "months",
|
});
|
||||||
},
|
|
||||||
valid
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onAvailablityChange = async (e: ChangeEvent<HTMLInputElement>) => {
|
const onAvailablityChange = async (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
const element = e.currentTarget;
|
const element = e.currentTarget;
|
||||||
const valid = element.checkValidity();
|
const v = element.value;
|
||||||
const val = parseFloat(element.value);
|
const value = parseFloat(v);
|
||||||
const unit = availability.totalSizeUnit === "gb" ? GB : TB;
|
const valid = element.checkValidity() && !isAvailabilityInvalid(value);
|
||||||
|
|
||||||
if (val * unit > space.quotaMaxBytes - space.quotaReservedBytes) {
|
onAvailabilityChange({
|
||||||
|
[element.name]: v,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (valid) {
|
||||||
|
setTotalSizeError("");
|
||||||
|
dispatch({
|
||||||
|
type: "toggle-next",
|
||||||
|
isNextEnable: true,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
setTotalSizeError(
|
setTotalSizeError(
|
||||||
"You cannot allocate more space than the remaining space."
|
element.validationMessage ||
|
||||||
|
"You cannot allocate more space than the remaining space."
|
||||||
);
|
);
|
||||||
dispatch({
|
dispatch({
|
||||||
type: "toggle-next",
|
type: "toggle-next",
|
||||||
isNextEnable: false,
|
isNextEnable: false,
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onAvailabilityChange(
|
|
||||||
{
|
|
||||||
[element.name]: parseFloat(element.value),
|
|
||||||
},
|
|
||||||
valid
|
|
||||||
);
|
|
||||||
setTotalSizeError(valid ? "" : element.validationMessage);
|
|
||||||
dispatch({
|
|
||||||
type: "toggle-next",
|
|
||||||
isNextEnable: valid && parseFloat(e.target.value) > 0,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onInputChange = async (e: ChangeEvent<HTMLInputElement>) => {
|
const onInputChange = async (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
const element = e.currentTarget;
|
const element = e.currentTarget;
|
||||||
const valid = element.checkValidity();
|
const valid = element.checkValidity();
|
||||||
|
|
||||||
onAvailabilityChange(
|
onAvailabilityChange({
|
||||||
{
|
[element.name]: parseFloat(element.value),
|
||||||
[element.name]: parseFloat(element.value),
|
});
|
||||||
},
|
|
||||||
valid
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const unit = availability.totalSizeUnit === "gb" ? GB : TB;
|
const unit = availability.totalSizeUnit === "gb" ? GB : TB;
|
||||||
const max = space.quotaMaxBytes / unit - space.quotaReservedBytes / unit;
|
const max = (
|
||||||
|
space.quotaMaxBytes / unit -
|
||||||
|
space.quotaReservedBytes / unit
|
||||||
|
).toFixed(2);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user