Skip to content

Commit

Permalink
refactor(web): helper util funcs for empty/null/undefined vars
Browse files Browse the repository at this point in the history
* firstNonDefault
* firstNonEmpty
* isEmptyArray
* isEmptyOrNull
  • Loading branch information
JosephKav committed Apr 3, 2024
1 parent 0e0e476 commit 75c4dad
Show file tree
Hide file tree
Showing 48 changed files with 211 additions and 123 deletions.
70 changes: 35 additions & 35 deletions web/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions web/ui/react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"private": true,
"type": "module",
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.5.1",
"@fortawesome/free-brands-svg-icons": "^6.5.1",
"@fortawesome/free-solid-svg-icons": "^6.5.1",
"@fortawesome/fontawesome-svg-core": "^6.5.2",
"@fortawesome/free-brands-svg-icons": "^6.5.2",
"@fortawesome/free-solid-svg-icons": "^6.5.2",
"@fortawesome/react-fontawesome": "^0.2.0",
"@tanstack/react-query": "^5.28.12",
"@tanstack/react-query": "^5.28.14",
"@types/bootstrap": "^5.2.10",
"@types/jest": "^29.5.12",
"@types/react": "^18.2.74",
Expand Down Expand Up @@ -56,8 +56,8 @@
]
},
"devDependencies": {
"@tanstack/react-query-devtools": "^5.28.12",
"@types/node": "^20.12.2",
"@tanstack/react-query-devtools": "^5.28.14",
"@types/node": "^20.12.3",
"@types/styled-components": "^5.1.34"
}
}
3 changes: 2 additions & 1 deletion web/ui/react-app/src/components/approvals/service-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { ModalContext } from "contexts/modal";
import { formatRelative } from "date-fns";
import { isEmptyOrNull } from "utils";

interface Props {
service: ServiceSummaryType;
Expand Down Expand Up @@ -57,7 +58,7 @@ export const ServiceInfo: FC<Props> = ({
() => ({
// If version hasn't been found or a new version has been found (and not skipped)
warning:
(service?.status?.deployed_version ?? "") === "" ||
isEmptyOrNull(service?.status?.deployed_version) ||
(updateAvailable && !updateSkipped),
// If the latest version is the same as the approved version
updateApproved:
Expand Down
9 changes: 4 additions & 5 deletions web/ui/react-app/src/components/generic/form-item-colour.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const FormItemColour: FC<FormItemColourProps> = ({
positionXS = position,
}) => {
const { register, setValue } = useFormContext();
const hexColour = useWatch({ name: name });
const hexColour: string = useWatch({ name: name });
const trimmedHex = hexColour?.replace("#", "");
const error = useError(name, true);
const padding = formPadding({ col_xs, col_sm, position, positionXS });
Expand All @@ -66,6 +66,7 @@ const FormItemColour: FC<FormItemColourProps> = ({
type="text"
defaultValue={trimmedHex}
placeholder={defaultVal}
maxLength={6}
autoFocus={false}
{...register(name, {
pattern: {
Expand All @@ -80,10 +81,8 @@ const FormItemColour: FC<FormItemColourProps> = ({
style={{ width: "30%" }}
type="color"
title="Choose your color"
value={hexColour || defaultVal}
onChange={(event) => {
setColour(event.target.value);
}}
value={`#${trimmedHex || defaultVal?.replace("#", "")}`}
onChange={(event) => setColour(event.target.value)}
autoFocus={false}
/>
</InputGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FC, useMemo } from "react";
import { useFormContext, useWatch } from "react-hook-form";

import FormLabel from "./form-label";
import { isEmptyOrNull } from "utils";
import { useError } from "hooks/errors";

interface Props {
Expand Down Expand Up @@ -70,7 +71,7 @@ const FormItemWithPreview: FC<Props> = ({
{...register(name, {
validate: (value: string | undefined) => {
// Allow empty values
if ((value ?? "") === "") return true;
if (isEmptyOrNull(value)) return true;

// Validate that it's a URL (with prefix)
try {
Expand Down
5 changes: 4 additions & 1 deletion web/ui/react-app/src/components/generic/form-key-val-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import FormKeyVal from "./form-key-val";
import { FormLabel } from "components/generic/form";
import { HeaderType } from "types/config";
import { diffObjects } from "utils/diff-objects";
import { isEmptyArray } from "utils";

interface Props {
name: string;
Expand Down Expand Up @@ -59,7 +60,9 @@ const FormKeyValMap: FC<Props> = ({
// useDefaults when the fieldValues are undefined or the same as the defaults
const useDefaults = useMemo(
() =>
(defaults && diffObjects(fieldValues ?? fields ?? [], defaults)) ?? false,
(!isEmptyArray(defaults) &&
diffObjects(fieldValues ?? fields ?? [], defaults)) ??
false,
[fieldValues, defaults]
);
// trigger validation on change of defaults being used/not
Expand Down
8 changes: 6 additions & 2 deletions web/ui/react-app/src/components/generic/form-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useFieldArray, useFormContext, useWatch } from "react-hook-form";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { StringFieldArray } from "types/config";
import { diffObjects } from "utils/diff-objects";
import { isEmptyArray } from "utils";

interface Props {
name: string;
Expand Down Expand Up @@ -45,7 +46,9 @@ const FormList: FC<Props> = ({
// useDefaults when the fieldValues are undefined or the same as the defaults
const useDefaults = useMemo(
() =>
(defaults && diffObjects(fieldValues ?? fields ?? [], defaults)) ?? false,
(!isEmptyArray(defaults) &&
diffObjects(fieldValues ?? fields ?? [], defaults)) ??
false,
[fieldValues, defaults]
);
// trigger validation on change of defaults being used/not
Expand All @@ -68,7 +71,8 @@ const FormList: FC<Props> = ({
// and give the defaults if not overridden
useEffect(() => {
for (const item of fieldValues ?? fields ?? []) {
if ((item.arg ?? "") === "") {
const keys = Object.keys(item);
if (keys.length > 1 || !keys.includes("arg")) {
setValue(name, []);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FormItem, FormItemWithPreview } from "components/generic/form";
import { Accordion } from "react-bootstrap";
import { BooleanWithDefault } from "components/generic";
import { ServiceDashboardOptionsType } from "types/config";
import { firstNonDefault } from "components/modals/service-edit/util";
import { firstNonDefault } from "utils";

interface Props {
defaults?: ServiceDashboardOptionsType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
} from "types/config";
import { FC, memo, useEffect, useMemo } from "react";
import { FormItem, FormLabel, FormSelect } from "components/generic/form";
import { firstNonDefault, isEmptyOrNull } from "utils";
import { useFormContext, useWatch } from "react-hook-form";

import Command from "./command";
import { firstNonDefault } from "components/modals/service-edit/util";

const DockerRegistryOptions = [
{ label: "Docker Hub", value: "hub" },
Expand Down Expand Up @@ -87,7 +87,7 @@ const EditServiceLatestVersionRequire: FC<Props> = ({

useEffect(() => {
// Default to Docker Hub if no registry is selected and no default registry.
if ((selectedDockerRegistry ?? "") === "")
if (isEmptyOrNull(selectedDockerRegistry))
setValue("latest_version.require.docker.type", "hub");
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import EditServiceLatestVersionRequire from "./latest-version-require";
import FormURLCommands from "./latest-version-urlcommands";
import { LatestVersionLookupEditType } from "types/service-edit";
import VersionWithRefresh from "./version-with-refresh";
import { firstNonDefault } from "components/modals/service-edit/util";
import { firstNonDefault } from "utils";
import { useWatch } from "react-hook-form";

interface Props {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useEffect, useMemo } from "react";

import { NotifyBarkType } from "types/config";
import NotifyOptions from "components/modals/service-edit/notify-types/shared";
import { firstNonDefault } from "components/modals/service-edit/util";
import { firstNonDefault } from "utils";
import { normaliseForSelect } from "components/modals/service-edit/util";
import { useFormContext } from "react-hook-form";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { BooleanWithDefault } from "components/generic";
import { NotifyDiscordType } from "types/config";
import NotifyOptions from "components/modals/service-edit/notify-types/shared";
import { firstNonDefault } from "components/modals/service-edit/util";
import { firstNonDefault } from "utils";
import { strToBool } from "utils";
import { useMemo } from "react";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "react-bootstrap";
import { FC, memo, useCallback, useEffect, useMemo } from "react";
import { faMinus, faPlus } from "@fortawesome/free-solid-svg-icons";
import { isEmptyArray, isEmptyOrNull } from "utils";
import { useFieldArray, useFormContext, useWatch } from "react-hook-form";

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
Expand Down Expand Up @@ -55,7 +56,7 @@ const NtfyActions: FC<Props> = ({ name, label, tooltip, defaults }) => {
// useDefaults when the fieldValues are unset or the same as the defaults
const useDefaults = useMemo(
() =>
defaults &&
!isEmptyArray(defaults) &&
diffObjects(fieldValues ?? fields ?? [], defaults, [".action"]),
[fieldValues, defaults]
);
Expand All @@ -82,7 +83,7 @@ const NtfyActions: FC<Props> = ({ name, label, tooltip, defaults }) => {
useEffect(() => {
// ensure we don't have another types actions
for (const item of fieldValues ?? fields ?? []) {
if ((item.action ?? "") === "") {
if (isEmptyOrNull(item.action)) {
setValue(name, []);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { NotifyOpsGenieTarget } from "types/config";
import OpsGenieTarget from "./target";
import { convertOpsGenieTargetFromString } from "components/modals/service-edit/util";
import { diffObjects } from "utils/diff-objects";
import { isEmptyArray } from "utils";

interface Props {
name: string;
Expand Down Expand Up @@ -55,7 +56,7 @@ const OpsGenieTargets: FC<Props> = ({ name, label, tooltip, defaults }) => {
// useDefaults when the fieldValues are undefined or the same as the defaults
const useDefaults = useMemo(
() =>
defaults &&
!isEmptyArray(defaults) &&
diffObjects(fieldValues ?? fields ?? [], defaults, [
".type",
".sub_type",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

import { BooleanWithDefault } from "components/generic";
import NotifyOptions from "components/modals/service-edit/notify-types/shared";
import { firstNonDefault } from "components/modals/service-edit/util";
import { firstNonDefault } from "utils";
import { strToBool } from "utils";
import { useMemo } from "react";
import { useWatch } from "react-hook-form";
Expand Down
Loading

0 comments on commit 75c4dad

Please sign in to comment.