Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | 66x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 54x 54x 8x 8x 8x 8x 8x 8x 8x 8x 8x | import React, { ReactElement, ReactNode, useState } from 'react'; import { v4 as uuid } from 'uuid'; import { Input, Checkbox, Button, Popover, Form, Alert, Space } from 'antd'; import { useTranslation } from 'react-i18next'; import { getOrganization, getUserId } from '@utils/TokenUtils'; import { FilterFilled, SaveOutlined, CloseCircleOutlined, SettingOutlined, DeleteOutlined } from '@ant-design/icons'; import './filter.less'; import { getSearchConditionsForDomain } from '@kernel/ServerSideSearcher'; import { DynamicFilterResponseProps, FetchSearchResultProps, InternalServerErrorResp, SearchConditions } from '@props/RecordProps'; import { CreateFilterProps, saveFilter, UpdateFilterProps } from './FilterFetchUtils'; import { useClickToggleVisible } from '@utils/hooks'; import { stopPropagationAndPreventDefault } from '@utils/ObjectUtils'; import { TooltipPlacement } from 'antd/es/tooltip'; import { DeleteComponent } from '../../form'; import { DynamicFilterDomainName } from '@config/domain'; import { openInfoNotification } from '@utils/NotificationUtils'; interface SaveDynamicFilterPopoverProps { domainName: string; zIndex: number; filter?: DynamicFilterResponseProps; displayElement?: ReactNode; updateCallback: () => void; fullTextSearchConditions?: FetchSearchResultProps; } const SaveDynamicFilterPopover = (props: SaveDynamicFilterPopoverProps): ReactElement => { const { domainName, zIndex, filter, updateCallback, displayElement, fullTextSearchConditions } = props; const [label, setLabel] = useState(filter?.label ?? ''); const [description, setDescription] = useState(filter?.description ?? ''); const [isDefault, setIsDefault] = useState(filter?.isDefault ?? false); const [saving, setSaving] = useState<boolean>(false); const [errorMessage, setErrorMessage] = useState<string>(); const { t } = useTranslation(); const [visible, setVisible] = useClickToggleVisible(); const mode = (filter == null) ? 'create' : 'edit'; const conditionsObj: SearchConditions | FetchSearchResultProps | undefined = fullTextSearchConditions ?? getSearchConditionsForDomain(domainName); const conditionsStr = JSON.stringify(conditionsObj, (key, value) => { Iif (value === undefined) { return value; } return value; }); const handleSave = (): void => { if (label.trim() === '') { setErrorMessage(t('Label field is required')); return; } if ((conditionsObj == null || Object.keys(conditionsObj).length === 0) && fullTextSearchConditions == null) { console.error('No conditions to save'); return; } //Display sequence is set to current max display sequence + 10 on backend const value = (mode === 'edit' && filter != null) ? ({ id: filter.id, label: label, isDefault: isDefault ?? filter.isDefault, description, conditions: conditionsStr, } as UpdateFilterProps) : ({ //Append uuid to avoid name conflict name: `${label}_${uuid()}`, label: label, isSystem: false, isDefault, description, organization: { id: getOrganization().id }, owner: { id: getUserId() }, enableRoles: "", conditions: conditionsStr, icon: "FilterOutlined" } as CreateFilterProps); setSaving(true); saveFilter({ domainName, value, successCallback: () => { setErrorMessage(''); openInfoNotification(t('Save dynamic filter success')); updateCallback?.(); }, failCallback: (error: unknown) => { if ("message" in (error as object)) { setErrorMessage((error as InternalServerErrorResp).message); } else { setErrorMessage(JSON.stringify(error)); } }, finallyCallback: () => { setSaving(false); setVisible(false); } }); }; const errorMessageAlert = (errorMessage == null || errorMessage === '') ? <></> : (<Alert message={t('Save dynamic filter failed')} type="error" description={errorMessage} showIcon />); const instrument = (filter == null) ? <></> : ( <Alert message={t('Will replace exist condition with current applies one')} type="info" showIcon /> ); const content = ( <Space direction="vertical" size="middle" style={{ width: "94%", margin: "auto", display: "block" }} onClick={(e) => { stopPropagationAndPreventDefault(e); }} > {instrument} {errorMessageAlert} <Form layout="horizontal"> <Form.Item> <Input id="labelField" value={label} onChange={(e) => setLabel(e.target.value)} placeholder={t('Dynamic filter name')} /> </Form.Item> <Form.Item> <Input.TextArea id="descriptionField" onChange={(e) => setDescription(e.target.value)} placeholder={t('Dynamic filter description')} > {description} </Input.TextArea> </Form.Item> <Form.Item> <Checkbox checked={isDefault} onChange={(e) => setIsDefault(e.target.checked)} > <span onClick={() => setIsDefault(!isDefault)}> {t('Is Default(Apply automatically when showing the table)')} </span> </Checkbox> </Form.Item> <Form.Item style={{ display: 'flex', justifyContent: 'center' }}> <Space size={12}> <Button type="primary" onClick={handleSave} size="small" icon={<SaveOutlined />} loading={saving} > {t('Save')} </Button> <Button onClick={() => { setVisible(false); }} size="small" icon={<CloseCircleOutlined />} > {t('Cancel')} </Button> {filter != null && <DeleteComponent id={filter?.id} domainName={DynamicFilterDomainName} callback={() => { updateCallback?.(); setVisible(false); }} displayElement={<Button type="primary" danger size="small" icon={<DeleteOutlined />} > {t('Delete')} </Button>} />} </Space> </Form.Item> </Form> </Space> ); const goldenRatio = 0.618; const width = 300; const height = width * goldenRatio; const uiConfig = { "edit": { displayElement: (<SettingOutlined title={t("Edit this filter")} />), displayPlacement: "bottomRight" as TooltipPlacement }, "create": { displayElement: (<Button icon={<FilterFilled title={t("FilterPanelLocked")} />} title={t('Save search condition as dynamic filter')} className="save-dynamic-filter-button" size={"small"} > {t('Create dynamic filter')} </Button>), displayPlacement: "bottomLeft" as TooltipPlacement } }; return ( <Popover content={content} trigger="click" overlayStyle={{ width: `${width}px`, height: `${height}px`, paddingTop: 0, }} open={visible} showArrow={false} placement={uiConfig[mode].displayPlacement} zIndex={zIndex + 2} overlayClassName={`save-dynamic-filter-popover save-dynamic-filter-popover-${mode}`} > <span onClick={(e) => { stopPropagationAndPreventDefault(e); setVisible(!visible); }}> {displayElement ?? uiConfig[mode].displayElement} </span> </Popover> ); }; export default SaveDynamicFilterPopover; |