Skip to content

Commit 634d6f8

Browse files
Areg HakobyanAreg Hakobyan
authored andcommitted
fix(ProgressBar): simplify logic of the component
1 parent 7394aaa commit 634d6f8

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

src/components/molecules/ProgressBar/ProgressBar.tsx

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { FC, useEffect, useMemo, useState } from "react";
1+
import React, { FC, useMemo } from "react";
22
import classNames from "classnames";
33

44
// Components
@@ -81,47 +81,42 @@ const ProgressBar: FC<IProgressBarProps> = ({
8181
infoText,
8282
label
8383
}) => {
84-
const [progressBarStatus, setProgressBarStatus] = useState<IProgressBarProps["status"] | "success">(status);
85-
8684
const isDeterminate = type === "determinate";
87-
const isTypeRest = progressBarStatus === "rest";
85+
const isTypeRest = status === "rest";
8886
const isPercentLowerThanMax = percent !== undefined && percent < 100;
89-
const error = status === "error";
87+
const isError = status === "error";
88+
const isSuccess = percent === 100;
89+
const effectiveType = isError ? "determinate" : type;
90+
const isRestOrWarning = status === "rest" || status === "warning";
9091

9192
const processedPercent = useMemo(() => {
9293
let result = percent || 0;
9394

94-
if (result < 0 && !error) result = 0;
95-
if (result >= 100 || error) result = 100;
95+
if (result < 0 && !isError) result = 0;
96+
if (result >= 100 || isError) result = 100;
9697

9798
return `${result}%`;
98-
}, [percent, error]);
99-
100-
useEffect(() => {
101-
if (error) {
102-
setProgressBarStatus("error");
103-
return;
104-
}
105-
106-
if (percent !== undefined && !error) {
107-
if (percent >= 100 && progressBarStatus !== "success" && isDeterminate) {
108-
setProgressBarStatus("success");
109-
} else if ((!isTypeRest && isPercentLowerThanMax && isDeterminate) || !isDeterminate) {
110-
setProgressBarStatus("rest");
111-
}
112-
}
113-
}, [error, isTypeRest, status, percent, isDeterminate]);
99+
}, [percent, isError]);
114100

115101
return (
116102
<div
117103
className={classNames(
118-
`progressBar progressBar_type_${error ? "determinate" : type} progressBar_size_${size} progressBar_status_${progressBarStatus}`,
119-
className
104+
"progressBar",
105+
`progressBar_type_${effectiveType}`,
106+
`progressBar_size_${size}`,
107+
className,
108+
{
109+
progressBar_status_error: isError,
110+
progressBar_status_success: isSuccess,
111+
progressBar_status_rest: isRestOrWarning
112+
}
120113
)}
121114
>
122115
<Label text={label} size={helperTextAndLabelSizeMap[size]} infoText={infoText} />
123116
<div className="progressBar__track">
124-
{(isDeterminate || error) && <div className="progressBar__fill" style={{ width: processedPercent }} />}
117+
{(isDeterminate || isError) && (
118+
<div className="progressBar__fill" style={{ width: processedPercent }} />
119+
)}
125120
<div className="progressBar__loadingBar" />
126121
</div>
127122
<div className="progressBar__info">

0 commit comments

Comments
 (0)