Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SalesforceLinkDialog.tsx 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* eslint-disable lines-around-comment */
  2. import Spinner from '@atlaskit/spinner';
  3. import { makeStyles } from '@material-ui/core';
  4. import React, { useCallback } from 'react';
  5. import { useTranslation } from 'react-i18next';
  6. import { useDispatch } from 'react-redux';
  7. // @ts-ignore
  8. import { Dialog, hideDialog } from '../../../base/dialog';
  9. import Icon from '../../../base/icons/components/Icon';
  10. import { IconSearch } from '../../../base/icons/svg';
  11. // @ts-ignore
  12. import { getFieldValue } from '../../../base/react';
  13. import BaseTheme from '../../../base/ui/components/BaseTheme.web';
  14. import { NOTES_MAX_LENGTH } from '../../constants';
  15. // @ts-ignore
  16. import { useSalesforceLinkDialog } from '../../useSalesforceLinkDialog';
  17. import { RecordItem } from './RecordItem';
  18. // @ts-ignore
  19. const useStyles = makeStyles((theme: any) => {
  20. return {
  21. container: {
  22. minHeight: '450px',
  23. overflowY: 'auto',
  24. position: 'relative'
  25. },
  26. recordsSearchContainer: {
  27. position: 'relative',
  28. padding: '1px'
  29. },
  30. searchIcon: {
  31. display: 'block',
  32. position: 'absolute',
  33. color: theme.palette.text03,
  34. left: 16,
  35. top: 10,
  36. width: 20,
  37. height: 20
  38. },
  39. resultLabel: {
  40. fontSize: '15px',
  41. margin: '16px 0 8px'
  42. },
  43. recordsSearch: {
  44. backgroundColor: theme.palette.field01,
  45. border: '1px solid',
  46. borderRadius: theme.shape.borderRadius,
  47. borderColor: theme.palette.ui05,
  48. color: theme.palette.text01,
  49. padding: '10px 16px 10px 44px',
  50. width: '100%',
  51. height: 40,
  52. '&::placeholder': {
  53. color: theme.palette.text03,
  54. ...theme.typography.bodyShortRegular,
  55. lineHeight: `${theme.typography.bodyShortRegular.lineHeight}px`
  56. }
  57. },
  58. spinner: {
  59. alignItems: 'center',
  60. display: 'flex',
  61. height: 'calc(100% - 100px)',
  62. justifyContent: 'center',
  63. width: '100%'
  64. },
  65. noRecords: {
  66. height: 'calc(100% - 150px)',
  67. display: 'flex',
  68. alignItems: 'center',
  69. justifyContent: 'center',
  70. flexDirection: 'column'
  71. },
  72. recordsError: {
  73. height: 'calc(100% - 80px)',
  74. display: 'flex',
  75. alignItems: 'center',
  76. justifyContent: 'center',
  77. flexDirection: 'column'
  78. },
  79. recordList: {
  80. listStyle: 'none',
  81. margin: '10px 0',
  82. padding: 0
  83. },
  84. recordInfo: {
  85. backgroundColor: theme.palette.ui03,
  86. padding: '0 16px',
  87. borderRadius: theme.shape.borderRadius,
  88. marginBottom: '28px'
  89. },
  90. detailsError: {
  91. padding: '10px 0'
  92. },
  93. addNote: {
  94. padding: '10px 0'
  95. },
  96. notes: {
  97. lineHeight: '18px',
  98. minHeight: '130px',
  99. resize: 'vertical',
  100. width: '100%',
  101. boxSizing: 'borderBox',
  102. overflow: 'hidden',
  103. border: '1px solid',
  104. borderColor: theme.palette.ui05,
  105. backgroundColor: theme.palette.field01,
  106. color: theme.palette.field02,
  107. borderRadius: theme.shape.borderRadius,
  108. padding: '10px 16px'
  109. }
  110. };
  111. });
  112. /**
  113. * Component that renders the Salesforce link dialog.
  114. *
  115. * @returns {React$Element<any>}
  116. */
  117. function SalesforceLinkDialog() {
  118. const { t } = useTranslation();
  119. const classes = useStyles();
  120. const dispatch = useDispatch();
  121. const {
  122. hasDetailsErrors,
  123. hasRecordsErrors,
  124. isLoading,
  125. linkMeeting,
  126. notes,
  127. records,
  128. searchTerm,
  129. selectedRecord,
  130. selectedRecordOwner,
  131. setNotes,
  132. setSearchTerm,
  133. setSelectedRecord,
  134. showNoResults,
  135. showSearchResults
  136. } = useSalesforceLinkDialog();
  137. const handleChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
  138. const value = getFieldValue(event);
  139. setSearchTerm(value);
  140. }, [ getFieldValue ]);
  141. const handleSubmit = useCallback(() => {
  142. dispatch(hideDialog());
  143. linkMeeting();
  144. }, [ hideDialog, linkMeeting ]);
  145. const renderSpinner = () => (
  146. <div className = { classes.spinner }>
  147. <Spinner
  148. // @ts-ignore
  149. isCompleting = { false }
  150. size = 'medium' />
  151. </div>
  152. );
  153. const renderDetailsErrors = () => (
  154. <div className = { classes.detailsError }>
  155. {t('dialog.searchResultsDetailsError')}
  156. </div>
  157. );
  158. const renderSelection = () => (
  159. <div>
  160. <div className = { classes.recordInfo }>
  161. <RecordItem { ...selectedRecord } />
  162. {selectedRecordOwner && <RecordItem { ...selectedRecordOwner } />}
  163. {hasDetailsErrors && renderDetailsErrors()}
  164. </div>
  165. <div className = { classes.addNote }>{t('dialog.addOptionalNote')}</div>
  166. <textarea
  167. autoFocus = { true }
  168. className = { classes.notes }
  169. maxLength = { NOTES_MAX_LENGTH }
  170. /* eslint-disable-next-line react/jsx-no-bind */
  171. onChange = { e => setNotes(e.target.value) }
  172. placeholder = { t('dialog.addMeetingNote') }
  173. rows = { 4 }
  174. value = { notes } />
  175. </div>
  176. );
  177. const renderRecordsSearch = () => !selectedRecord && (
  178. <div className = { classes.recordsSearchContainer }>
  179. <Icon
  180. className = { classes.searchIcon }
  181. color = { BaseTheme.palette.icon03 }
  182. src = { IconSearch } />
  183. <input
  184. autoComplete = 'off'
  185. autoFocus = { false }
  186. className = { classes.recordsSearch }
  187. name = 'recordsSearch'
  188. onChange = { handleChange }
  189. placeholder = { t('dialog.searchInSalesforce') }
  190. tabIndex = { 0 }
  191. value = { searchTerm } />
  192. {(!isLoading && !hasRecordsErrors) && (
  193. <div className = { classes.resultLabel }>
  194. {showSearchResults
  195. ? t('dialog.searchResults', { count: records.length })
  196. : t('dialog.recentlyUsedObjects')
  197. }
  198. </div>
  199. )}
  200. </div>
  201. );
  202. const renderNoRecords = () => showNoResults && (
  203. <div className = { classes.noRecords }>
  204. <div>{t('dialog.searchResultsNotFound')}</div>
  205. <div>{t('dialog.searchResultsTryAgain')}</div>
  206. </div>
  207. );
  208. const renderRecordsError = () => (
  209. <div className = { classes.recordsError }>
  210. {t('dialog.searchResultsError')}
  211. </div>
  212. );
  213. const renderContent = () => {
  214. if (isLoading) {
  215. return renderSpinner();
  216. }
  217. if (hasRecordsErrors) {
  218. return renderRecordsError();
  219. }
  220. if (showNoResults) {
  221. return renderNoRecords();
  222. }
  223. if (selectedRecord) {
  224. return renderSelection();
  225. }
  226. return (
  227. <ul className = { classes.recordList }>
  228. {records.map((item: any) => (
  229. <RecordItem
  230. key = { `record-${item.id}` }
  231. /* eslint-disable-next-line react/jsx-no-bind */
  232. onClick = { () => setSelectedRecord(item) }
  233. { ...item } />
  234. ))}
  235. </ul>
  236. );
  237. };
  238. return (
  239. <Dialog
  240. disableEnter = { true }
  241. disableFooter = { !selectedRecord }
  242. height = { 'medium' }
  243. okDisabled = { !selectedRecord }
  244. okKey = 'dialog.linkMeeting'
  245. /* eslint-disable-next-line react/jsx-no-bind */
  246. onDecline = { () => setSelectedRecord(null) }
  247. onSubmit = { handleSubmit }
  248. titleKey = 'dialog.linkMeetingTitle'
  249. width = { 'small' }>
  250. <div className = { classes.container } >
  251. {renderRecordsSearch()}
  252. {renderContent()}
  253. </div>
  254. </Dialog>
  255. );
  256. }
  257. export default SalesforceLinkDialog;