Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { ComponentType } from 'react';
  2. import { IStore } from '../../app/types';
  3. import {
  4. HIDE_DIALOG,
  5. HIDE_SHEET,
  6. OPEN_DIALOG,
  7. OPEN_SHEET
  8. } from './actionTypes';
  9. import { isDialogOpen } from './functions';
  10. import logger from './logger';
  11. /**
  12. * Signals Dialog to close its dialog.
  13. *
  14. * @param {Object} [component] - The {@code Dialog} component to close/hide. If
  15. * {@code undefined}, closes/hides {@code Dialog} regardless of which
  16. * component it's rendering; otherwise, closes/hides {@code Dialog} only if
  17. * it's rendering the specified {@code component}.
  18. * @returns {{
  19. * type: HIDE_DIALOG,
  20. * component: (React.Component | undefined)
  21. * }}
  22. */
  23. export function hideDialog(component?: ComponentType<any>) {
  24. logger.info(`Hide dialog: ${getComponentDisplayName(component)}`);
  25. return {
  26. type: HIDE_DIALOG,
  27. component
  28. };
  29. }
  30. /**
  31. * Closes the active sheet.
  32. *
  33. * @returns {{
  34. * type: HIDE_SHEET,
  35. * }}
  36. */
  37. export function hideSheet() {
  38. return {
  39. type: HIDE_SHEET
  40. };
  41. }
  42. /**
  43. * Signals Dialog to open dialog.
  44. *
  45. * @param {Object} component - The component to display as dialog.
  46. * @param {Object} [componentProps] - The React {@code Component} props of the
  47. * specified {@code component}.
  48. * @returns {{
  49. * type: OPEN_DIALOG,
  50. * component: React.Component,
  51. * componentProps: (Object | undefined)
  52. * }}
  53. */
  54. export function openDialog(component: ComponentType<any>, componentProps?: Object) {
  55. logger.info(`Open dialog: ${getComponentDisplayName(component)}`);
  56. return {
  57. type: OPEN_DIALOG,
  58. component,
  59. componentProps
  60. };
  61. }
  62. /**
  63. * Opens the requested sheet.
  64. *
  65. * @param {Object} component - The component to display as a sheet.
  66. * @param {Object} [componentProps] - The React {@code Component} props of the
  67. * specified {@code component}.
  68. * @returns {{
  69. * type: OPEN_SHEET,
  70. * component: React.Component,
  71. * componentProps: (Object | undefined)
  72. * }}
  73. */
  74. export function openSheet(component: ComponentType<any>, componentProps?: Object) {
  75. return {
  76. type: OPEN_SHEET,
  77. component,
  78. componentProps
  79. };
  80. }
  81. /**
  82. * Signals Dialog to open a dialog with the specified component if the component
  83. * is not already open. If it is open, then Dialog is signaled to close its
  84. * dialog.
  85. *
  86. * @param {Object} component - The component to display as dialog.
  87. * @param {Object} [componentProps] - The React {@code Component} props of the
  88. * specified {@code component}.
  89. * @returns {Function}
  90. */
  91. export function toggleDialog(component: ComponentType<any>, componentProps?: Object) {
  92. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  93. if (isDialogOpen(getState, component)) {
  94. dispatch(hideDialog(component));
  95. } else {
  96. dispatch(openDialog(component, componentProps));
  97. }
  98. };
  99. }
  100. /**
  101. * Extracts a printable name for a dialog component.
  102. *
  103. * @param {Object} component - The component to extract the name for.
  104. *
  105. * @returns {string} The display name.
  106. */
  107. function getComponentDisplayName(component?: ComponentType<any>) {
  108. if (!component) {
  109. return '';
  110. }
  111. const name = component.displayName ?? component.name ?? 'Component';
  112. return name.replace('withI18nextTranslation(Connect(', '') // dialogs with translations
  113. .replace('))', ''); // dialogs with translations suffix
  114. }