import { memo, useEffect, useState } from 'react';
import { Typography } from '@mui/material';
import { getComponentFromMap } from '@pega/react-sdk-components/lib/bridge/helpers/sdk_component_map';
import type { PConnProps } from '@pega/react-sdk-components/lib/types/PConnProps';
import { getUserId, isUserNameAvailable } from './UserReferenceUtils';
const DROPDOWN_LIST = 'Drop-down list';
const SEARCH_BOX = 'Search box';
interface UserReferenceProps extends PConnProps {
// If any, enter additional props that only exist on URLComponent here
displayAs?: string;
displayMode?: string;
label?: string;
value?: any;
testId?: string;
placeholder?: string;
helperText?: string;
disabled?: boolean;
readOnly?: boolean;
required?: boolean;
validatemessage?: string;
showAsFormattedText?: boolean;
additionalProps?: object;
hideLabel?: boolean;
variant?: string;
onChange?: any;
}
const UserReference = (props: UserReferenceProps) => {
// Get emitted components from map (so we can get any override that may exist)
const AutoComplete = getComponentFromMap('AutoComplete');
const Dropdown = getComponentFromMap('Dropdown');
const FieldValueList = getComponentFromMap('FieldValueList');
const {
label = '',
displayAs = '',
displayMode = '',
getPConnect,
value = '',
testId = '',
helperText = '',
validatemessage = '',
placeholder = '',
showAsFormattedText = false,
additionalProps = {},
hideLabel = false,
readOnly = false,
required = false,
disabled = false,
onChange,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
variant = 'inline'
} = props;
const [dropDownDataSource, setDropDownDataSource] = useState([]);
const [userName, setUserName] = useState('');
const OPERATORS_DP = PCore.getEnvironmentInfo().getDefaultOperatorDP();
const userId = getUserId(value);
useEffect(() => {
if (userId && readOnly && showAsFormattedText) {
if (isUserNameAvailable(value)) {
setUserName(value.userName);
} else {
// if same user ref field is referred in view as editable & readonly formatted text
// referenced users won't be available, so get user details from dx api
const { getOperatorDetails } = PCore.getUserApi();
getOperatorDetails(userId).then((res: any) => {
if (res.data && res.data.pyOperatorInfo && res.data.pyOperatorInfo.pyUserName) {
setUserName(res.data.pyOperatorInfo.pyUserName);
}
});
}
} else if (displayAs === DROPDOWN_LIST) {
const queryPayload = {
dataViewName: OPERATORS_DP
};
PCore.getRestClient()
.invokeRestApi('getListData', { queryPayload })
.then(res => {
const ddDataSource = res.data.data.map(listItem => ({
key: listItem.pyUserIdentifier,
value: listItem.pyUserName
}));
setDropDownDataSource(ddDataSource);
})
.catch(err => {
console.error(err);
});
}
}, [displayAs, readOnly, showAsFormattedText, value]);
let userReferenceComponent: any = null;
if (displayMode === 'DISPLAY_ONLY') {
return