import { DefaultButton, Dropdown, IDropdownOption, IStyleFunctionOrObject, ITextFieldStyleProps, ITextFieldStyles, Label, Stack, TextField, } from "@fluentui/react"; import { FullTextIndex, FullTextPath, FullTextPolicy } from "Contracts/DataModels"; import { CollapsibleSectionComponent } from "Explorer/Controls/CollapsiblePanel/CollapsibleSectionComponent"; import * as React from "react"; export interface FullTextPoliciesComponentProps { fullTextPolicy: FullTextPolicy; onFullTextPathChange: ( fullTextPolicy: FullTextPolicy, fullTextIndexes: FullTextIndex[], validationPassed: boolean, ) => void; discardChanges?: boolean; onChangesDiscarded?: () => void; } export interface FullTextPolicyData { path: string; language: string; pathError: string; } const labelStyles = { root: { fontSize: 12, }, }; const textFieldStyles: IStyleFunctionOrObject = { fieldGroup: { height: 27, }, field: { fontSize: 12, padding: "0 8px", }, }; const dropdownStyles = { title: { height: 27, lineHeight: "24px", fontSize: 12, }, dropdown: { height: 27, lineHeight: "24px", }, dropdownItem: { fontSize: 12, }, }; export const FullTextPoliciesComponent: React.FunctionComponent = ({ fullTextPolicy, onFullTextPathChange, discardChanges, onChangesDiscarded, }): JSX.Element => { const getFullTextPathError = (path: string, index?: number): string => { let error = ""; if (!path) { error = "Full text path should not be empty"; } if ( index >= 0 && fullTextPathData?.find( (fullTextPath: FullTextPolicyData, dataIndex: number) => dataIndex !== index && fullTextPath.path === path, ) ) { error = "Full text path is already defined"; } return error; }; const initializeData = (fullTextPolicy: FullTextPolicy): FullTextPolicyData[] => { if (!fullTextPolicy) { fullTextPolicy = { defaultLanguage: getFullTextLanguageOptions()[0].key as never, fullTextPaths: [] }; } return fullTextPolicy.fullTextPaths.map((fullTextPath: FullTextPath) => ({ ...fullTextPath, pathError: getFullTextPathError(fullTextPath.path), })); }; const [fullTextPathData, setFullTextPathData] = React.useState(initializeData(fullTextPolicy)); const [defaultLanguage, setDefaultLanguage] = React.useState( fullTextPolicy ? fullTextPolicy.defaultLanguage : (getFullTextLanguageOptions()[0].key as never), ); React.useEffect(() => { propagateData(); }, [fullTextPathData, defaultLanguage]); React.useEffect(() => { if (discardChanges) { setFullTextPathData(initializeData(fullTextPolicy)); setDefaultLanguage(fullTextPolicy.defaultLanguage); onChangesDiscarded(); } }, [discardChanges]); const propagateData = () => { const newFullTextPolicy: FullTextPolicy = { defaultLanguage: defaultLanguage, fullTextPaths: fullTextPathData.map((policy: FullTextPolicyData) => ({ path: policy.path, language: policy.language, })), }; const fullTextIndexes: FullTextIndex[] = fullTextPathData.map((policy) => ({ path: policy.path, })); const validationPassed = fullTextPathData.every((policy: FullTextPolicyData) => policy.pathError === ""); onFullTextPathChange(newFullTextPolicy, fullTextIndexes, validationPassed); }; const onFullTextPathValueChange = (index: number, event: React.ChangeEvent) => { const value = event.target.value.trim(); const fullTextPaths = [...fullTextPathData]; if (!fullTextPaths[index]?.path && !value.startsWith("/")) { fullTextPaths[index].path = "/" + value; } else { fullTextPaths[index].path = value; } fullTextPaths[index].pathError = getFullTextPathError(value, index); setFullTextPathData(fullTextPaths); }; const onFullTextPathPolicyChange = (index: number, option: IDropdownOption): void => { const policies = [...fullTextPathData]; policies[index].language = option.key as never; setFullTextPathData(policies); }; const onAdd = () => { setFullTextPathData([ ...fullTextPathData, { path: "", language: defaultLanguage, pathError: getFullTextPathError(""), }, ]); }; const onDelete = (index: number) => { const policies = fullTextPathData.filter((_uniqueKey, j) => index !== j); setFullTextPathData(policies); }; return ( , option: IDropdownOption) => setDefaultLanguage(option.key as never) } > {fullTextPathData && fullTextPathData.length > 0 && fullTextPathData.map((fullTextPolicy: FullTextPolicyData, index: number) => ( onDelete(index)} > ) => onFullTextPathValueChange(index, event)} value={fullTextPolicy.path || ""} errorMessage={fullTextPolicy.pathError} /> , option: IDropdownOption) => onFullTextPathPolicyChange(index, option) } > ))} Add full text path ); }; export const getFullTextLanguageOptions = (): IDropdownOption[] => { return [ { key: "en-US", text: "English (US)", }, ]; };