Skip to content

Commit

Permalink
Refactor: manage statusCode handling within the child component
Browse files Browse the repository at this point in the history
  • Loading branch information
rog404 committed Nov 18, 2024
1 parent fd88df5 commit 6d31c94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions Client/src/Components/HttpStatusLabel/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ import { BaseLabel } from "../Label";
/**
* @component
* @param {Object} props
* @param {number | 'N/A'} props.status - The http status for the label
* @param {number} props.status - The http status for the label
* @param {Styles} props.customStyles - CSS Styles passed from parent component
* @returns {JSX.Element}
* @example
* // Render a http status label
* <HttpStatusLabel status={404} />
*/

const DEFAULT_CODE = 9999; // Default code for unknown status

const handleStatusCode = (status) => {
if (status >= 100 && status < 600) {
return status;
}
return DEFAULT_CODE;
};

const getRoundedStatusCode = (status) => {
if (typeof status === "number" && status >= 100 && status < 600) {
return Math.floor(status / 100) * 100;
}
return status;
};

const HttpStatusLabel = ({ status, customStyles }) => {
Expand All @@ -33,19 +39,21 @@ const HttpStatusLabel = ({ status, customStyles }) => {
bgColor: theme.palette.error.bg,
borderColor: theme.palette.error.light,
},
"N/A": {
default: {
dotColor: theme.palette.unresolved.main,
bgColor: theme.palette.unresolved.bg,
borderColor: theme.palette.unresolved.light,
},
};

const statusCode = handleStatusCode(status)

const { borderColor, bgColor, dotColor } =
colors[getRoundedStatusCode(status)] || colors["N/A"];
colors[getRoundedStatusCode(statusCode)] || colors.default;

return (
<BaseLabel
label={String(status)}
label={String(statusCode)}
styles={{
color: dotColor,
backgroundColor: bgColor,
Expand All @@ -56,9 +64,8 @@ const HttpStatusLabel = ({ status, customStyles }) => {
);
};

// TODO: Restrict status type to accept only numeric values after "N/A" status in IncidentTable is resolved
HttpStatusLabel.propTypes = {
status: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf(["N/A"])]).isRequired,
status:PropTypes.number,
customStyles: PropTypes.object,
};

Expand Down
2 changes: 1 addition & 1 deletion Client/src/Pages/Incidents/IncidentTable/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const IncidentTable = ({ monitors, selectedMonitor, filter }) => {
</TableCell>
<TableCell>{formattedDate}</TableCell>
<TableCell>
<HttpStatusLabel status={check.statusCode || "N/A"} />
<HttpStatusLabel status={check.statusCode} />
</TableCell>
<TableCell>{check.message}</TableCell>
</TableRow>
Expand Down

0 comments on commit 6d31c94

Please sign in to comment.