Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SalesforceLinkDialog.js 8.2KB

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