fix(EditFlowSettingsComponent): remove unused code and improve performance by removing unnecessary API calls to read flows from the database
feat(EditFlowSettingsComponent): add support for checking if the entered name is already in use by passing the list of unavailable names as a prop fix(FlowSettingsModal): remove unused state variable invalidName and setInvalidName function feat(FlowSettingsModal): add support for checking if the entered name is already in use by passing the list of unavailable names as a prop fix(ShareModal): add support for checking if the entered name is already in use by passing the list of unavailable names as a prop feat(ShareModal): add support for fetching the list of unavailable names from the API to check if the entered name is already in use fix(types): remove unused invalidName property from InputProps type
This commit is contained in:
parent
6f709dc3ee
commit
003a3e0783
5 changed files with 48 additions and 34 deletions
|
|
@ -1,29 +1,18 @@
|
||||||
import React, { ChangeEvent, useEffect, useRef, useState } from "react";
|
import React, { ChangeEvent, useState } from "react";
|
||||||
import { Input } from "../../components/ui/input";
|
import { Input } from "../../components/ui/input";
|
||||||
import { Label } from "../../components/ui/label";
|
import { Label } from "../../components/ui/label";
|
||||||
import { Textarea } from "../../components/ui/textarea";
|
import { Textarea } from "../../components/ui/textarea";
|
||||||
import { readFlowsFromDatabase } from "../../controllers/API";
|
|
||||||
import { InputProps } from "../../types/components";
|
import { InputProps } from "../../types/components";
|
||||||
import { FlowType } from "../../types/flow";
|
|
||||||
|
|
||||||
export const EditFlowSettings: React.FC<InputProps> = ({
|
export const EditFlowSettings: React.FC<InputProps> = ({
|
||||||
name,
|
name,
|
||||||
invalidName,
|
invalidNameList,
|
||||||
setInvalidName,
|
|
||||||
description,
|
description,
|
||||||
maxLength = 50,
|
maxLength = 50,
|
||||||
setName,
|
setName,
|
||||||
setDescription,
|
setDescription,
|
||||||
}: InputProps): JSX.Element => {
|
}: InputProps): JSX.Element => {
|
||||||
const [isMaxLength, setIsMaxLength] = useState(false);
|
const [isMaxLength, setIsMaxLength] = useState(false);
|
||||||
const nameLists = useRef<string[]>([]);
|
|
||||||
useEffect(() => {
|
|
||||||
readFlowsFromDatabase().then((flows) => {
|
|
||||||
flows.forEach((flow: FlowType) => {
|
|
||||||
nameLists.current.push(flow.name);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleNameChange = (event: ChangeEvent<HTMLInputElement>) => {
|
const handleNameChange = (event: ChangeEvent<HTMLInputElement>) => {
|
||||||
const { value } = event.target;
|
const { value } = event.target;
|
||||||
|
|
@ -32,19 +21,6 @@ export const EditFlowSettings: React.FC<InputProps> = ({
|
||||||
} else {
|
} else {
|
||||||
setIsMaxLength(false);
|
setIsMaxLength(false);
|
||||||
}
|
}
|
||||||
if (invalidName !== undefined) {
|
|
||||||
if (!nameLists.current.includes(value)) {
|
|
||||||
setInvalidName!(false);
|
|
||||||
} else {
|
|
||||||
setInvalidName!(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!nameLists.current.includes(value)) {
|
|
||||||
setInvalidName!(false);
|
|
||||||
} else {
|
|
||||||
setInvalidName!(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setName(value);
|
setName(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -60,7 +36,7 @@ export const EditFlowSettings: React.FC<InputProps> = ({
|
||||||
{isMaxLength && (
|
{isMaxLength && (
|
||||||
<span className="edit-flow-span">Character limit reached</span>
|
<span className="edit-flow-span">Character limit reached</span>
|
||||||
)}
|
)}
|
||||||
{invalidName && (
|
{invalidNameList?.includes(name ?? "") && (
|
||||||
<span className="edit-flow-span">Name already in use</span>
|
<span className="edit-flow-span">Name already in use</span>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -603,6 +603,7 @@ export async function getStoreComponents({
|
||||||
isPrivate = null,
|
isPrivate = null,
|
||||||
search = null,
|
search = null,
|
||||||
filterByUser = null,
|
filterByUser = null,
|
||||||
|
fields = null,
|
||||||
}: {
|
}: {
|
||||||
page?: number;
|
page?: number;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
|
|
@ -613,6 +614,7 @@ export async function getStoreComponents({
|
||||||
isPrivate?: boolean | null;
|
isPrivate?: boolean | null;
|
||||||
search?: string | null;
|
search?: string | null;
|
||||||
filterByUser?: boolean | null;
|
filterByUser?: boolean | null;
|
||||||
|
fields?: Array<string> | null;
|
||||||
}): Promise<StoreComponentResponse | undefined> {
|
}): Promise<StoreComponentResponse | undefined> {
|
||||||
try {
|
try {
|
||||||
let url = `${BASE_URL_API}store/components/`;
|
let url = `${BASE_URL_API}store/components/`;
|
||||||
|
|
@ -626,6 +628,9 @@ export async function getStoreComponents({
|
||||||
if (tags !== undefined && tags !== null && tags.length > 0) {
|
if (tags !== undefined && tags !== null && tags.length > 0) {
|
||||||
queryParams.push(`tags=${tags.join(encodeURIComponent(","))}`);
|
queryParams.push(`tags=${tags.join(encodeURIComponent(","))}`);
|
||||||
}
|
}
|
||||||
|
if (fields !== undefined && fields !== null && fields.length > 0) {
|
||||||
|
queryParams.push(`fields=${fields.join(encodeURIComponent(","))}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (sort !== undefined && sort !== null) {
|
if (sort !== undefined && sort !== null) {
|
||||||
queryParams.push(`sort=${sort}`);
|
queryParams.push(`sort=${sort}`);
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,9 @@ import IconComponent from "../../components/genericIconComponent";
|
||||||
import { Button } from "../../components/ui/button";
|
import { Button } from "../../components/ui/button";
|
||||||
import { SETTINGS_DIALOG_SUBTITLE } from "../../constants/constants";
|
import { SETTINGS_DIALOG_SUBTITLE } from "../../constants/constants";
|
||||||
import { FlowsContext } from "../../contexts/flowsContext";
|
import { FlowsContext } from "../../contexts/flowsContext";
|
||||||
|
import { readFlowsFromDatabase } from "../../controllers/API";
|
||||||
import { FlowSettingsPropsType } from "../../types/components";
|
import { FlowSettingsPropsType } from "../../types/components";
|
||||||
|
import { FlowType } from "../../types/flow";
|
||||||
import BaseModal from "../baseModal";
|
import BaseModal from "../baseModal";
|
||||||
|
|
||||||
export default function FlowSettingsModal({
|
export default function FlowSettingsModal({
|
||||||
|
|
@ -19,7 +21,6 @@ export default function FlowSettingsModal({
|
||||||
}, [flow!.name, flow!.description]);
|
}, [flow!.name, flow!.description]);
|
||||||
const [name, setName] = useState(flow!.name);
|
const [name, setName] = useState(flow!.name);
|
||||||
const [description, setDescription] = useState(flow!.description);
|
const [description, setDescription] = useState(flow!.description);
|
||||||
const [invalidName, setInvalidName] = useState(false);
|
|
||||||
|
|
||||||
function handleClick(): void {
|
function handleClick(): void {
|
||||||
let savedFlow = flows.find((flow) => flow.id === tabId);
|
let savedFlow = flows.find((flow) => flow.id === tabId);
|
||||||
|
|
@ -28,6 +29,18 @@ export default function FlowSettingsModal({
|
||||||
saveFlow(savedFlow!);
|
saveFlow(savedFlow!);
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [nameLists, setNameList] = useState<string[]>([]);
|
||||||
|
useEffect(() => {
|
||||||
|
const tempNameList: string[] = [];
|
||||||
|
readFlowsFromDatabase().then((flows) => {
|
||||||
|
flows.forEach((flow: FlowType) => {
|
||||||
|
tempNameList.push(flow.name);
|
||||||
|
});
|
||||||
|
setNameList(tempNameList.filter((name) => name !== flow!.name));
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BaseModal open={open} setOpen={setOpen} size="smaller">
|
<BaseModal open={open} setOpen={setOpen} size="smaller">
|
||||||
<BaseModal.Header description={SETTINGS_DIALOG_SUBTITLE}>
|
<BaseModal.Header description={SETTINGS_DIALOG_SUBTITLE}>
|
||||||
|
|
@ -36,8 +49,7 @@ export default function FlowSettingsModal({
|
||||||
</BaseModal.Header>
|
</BaseModal.Header>
|
||||||
<BaseModal.Content>
|
<BaseModal.Content>
|
||||||
<EditFlowSettings
|
<EditFlowSettings
|
||||||
invalidName={invalidName}
|
invalidNameList={nameLists}
|
||||||
setInvalidName={setInvalidName}
|
|
||||||
name={name}
|
name={name}
|
||||||
description={description}
|
description={description}
|
||||||
setName={setName}
|
setName={setName}
|
||||||
|
|
@ -46,7 +58,11 @@ export default function FlowSettingsModal({
|
||||||
</BaseModal.Content>
|
</BaseModal.Content>
|
||||||
|
|
||||||
<BaseModal.Footer>
|
<BaseModal.Footer>
|
||||||
<Button disabled={invalidName} onClick={handleClick} type="submit">
|
<Button
|
||||||
|
disabled={nameLists.includes(name) && name !== flow!.name}
|
||||||
|
onClick={handleClick}
|
||||||
|
type="submit"
|
||||||
|
>
|
||||||
Save
|
Save
|
||||||
</Button>
|
</Button>
|
||||||
</BaseModal.Footer>
|
</BaseModal.Footer>
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,11 @@ import { Button } from "../../components/ui/button";
|
||||||
import { Checkbox } from "../../components/ui/checkbox";
|
import { Checkbox } from "../../components/ui/checkbox";
|
||||||
import { alertContext } from "../../contexts/alertContext";
|
import { alertContext } from "../../contexts/alertContext";
|
||||||
import { FlowsContext } from "../../contexts/flowsContext";
|
import { FlowsContext } from "../../contexts/flowsContext";
|
||||||
import { getStoreTags, saveFlowStore } from "../../controllers/API";
|
import {
|
||||||
|
getStoreComponents,
|
||||||
|
getStoreTags,
|
||||||
|
saveFlowStore,
|
||||||
|
} from "../../controllers/API";
|
||||||
import { FlowType } from "../../types/flow";
|
import { FlowType } from "../../types/flow";
|
||||||
import { removeApiKeys } from "../../utils/reactflowUtils";
|
import { removeApiKeys } from "../../utils/reactflowUtils";
|
||||||
import { getTagsIds } from "../../utils/storeUtils";
|
import { getTagsIds } from "../../utils/storeUtils";
|
||||||
|
|
@ -38,10 +42,12 @@ export default function ShareModal({
|
||||||
const [loadingTags, setLoadingTags] = useState<boolean>(false);
|
const [loadingTags, setLoadingTags] = useState<boolean>(false);
|
||||||
const [sharePublic, setSharePublic] = useState(true);
|
const [sharePublic, setSharePublic] = useState(true);
|
||||||
const [selectedTags, setSelectedTags] = useState<string[]>([]);
|
const [selectedTags, setSelectedTags] = useState<string[]>([]);
|
||||||
|
const [unavaliableNames, setUnavaliableNames] = useState<string[]>([]);
|
||||||
const tagListId = useRef<{ id: string; name: string }[]>([]);
|
const tagListId = useRef<{ id: string; name: string }[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
handleGetTags();
|
handleGetTags();
|
||||||
|
handleGetNames();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
function handleGetTags() {
|
function handleGetTags() {
|
||||||
|
|
@ -51,6 +57,16 @@ export default function ShareModal({
|
||||||
setLoadingTags(false);
|
setLoadingTags(false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
function handleGetNames() {
|
||||||
|
const unavaliableNames: Array<string> = [];
|
||||||
|
getStoreComponents({ fields: ["name"], filterByUser: true }).then((res) => {
|
||||||
|
res?.results?.forEach((element: any) => {
|
||||||
|
unavaliableNames.push(element.name);
|
||||||
|
});
|
||||||
|
console.log(unavaliableNames);
|
||||||
|
setUnavaliableNames(unavaliableNames);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setName(component?.name ?? "");
|
setName(component?.name ?? "");
|
||||||
|
|
@ -117,6 +133,7 @@ export default function ShareModal({
|
||||||
<BaseModal.Content>
|
<BaseModal.Content>
|
||||||
<EditFlowSettings
|
<EditFlowSettings
|
||||||
name={name}
|
name={name}
|
||||||
|
invalidNameList={unavaliableNames}
|
||||||
description={description}
|
description={description}
|
||||||
setName={setName}
|
setName={setName}
|
||||||
setDescription={setDescription}
|
setDescription={setDescription}
|
||||||
|
|
@ -162,6 +179,7 @@ export default function ShareModal({
|
||||||
|
|
||||||
<BaseModal.Footer>
|
<BaseModal.Footer>
|
||||||
<Button
|
<Button
|
||||||
|
disabled={unavaliableNames.includes(name)}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
handleShareComponent();
|
handleShareComponent();
|
||||||
if (setOpen) setOpen(false);
|
if (setOpen) setOpen(false);
|
||||||
|
|
|
||||||
|
|
@ -223,10 +223,9 @@ export type InputProps = {
|
||||||
name: string | null;
|
name: string | null;
|
||||||
description: string | null;
|
description: string | null;
|
||||||
maxLength?: number;
|
maxLength?: number;
|
||||||
invalidName?: boolean;
|
|
||||||
setName: (name: string) => void;
|
setName: (name: string) => void;
|
||||||
setDescription: (description: string) => void;
|
setDescription: (description: string) => void;
|
||||||
setInvalidName?: (invalidName: boolean) => void;
|
invalidNameList?: string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TooltipProps = {
|
export type TooltipProps = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue