PrivateLegacyPagesMigrationModal.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React, { useState } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import {
  4. Modal, ModalHeader, ModalBody, ModalFooter,
  5. } from 'reactstrap';
  6. import { apiv3Post } from '~/client/util/apiv3-client';
  7. import { usePrivateLegacyPagesMigrationModal } from '~/stores/modal';
  8. import ApiErrorMessageList from './PageManagement/ApiErrorMessageList';
  9. export const PrivateLegacyPagesMigrationModal = (): JSX.Element => {
  10. const { t } = useTranslation();
  11. const { data: status, close } = usePrivateLegacyPagesMigrationModal();
  12. const isOpened = status?.isOpened ?? false;
  13. const [isRecursively, setIsRecursively] = useState(true);
  14. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  15. const [errs, setErrs] = useState<Error[] | null>(null);
  16. async function submit() {
  17. if (status == null || status.pages == null || status.pages.length === 0) {
  18. return;
  19. }
  20. const { pages, onSubmited } = status;
  21. const pageIds = pages.map(page => page.pageId);
  22. try {
  23. await apiv3Post<void>('/pages/legacy-pages-migration', {
  24. pageIds,
  25. isRecursively: isRecursively ? true : undefined,
  26. });
  27. if (onSubmited != null) {
  28. onSubmited(pages, isRecursively);
  29. }
  30. }
  31. catch (err) {
  32. setErrs([err]);
  33. }
  34. }
  35. function renderForm() {
  36. return (
  37. <div className="custom-control custom-checkbox custom-checkbox-warning">
  38. <input
  39. className="custom-control-input"
  40. id="convertRecursively"
  41. type="checkbox"
  42. checked={isRecursively}
  43. onChange={(e) => {
  44. setIsRecursively(e.target.checked);
  45. }}
  46. />
  47. <label className="custom-control-label" htmlFor="convertRecursively">
  48. { t('private_legacy_pages.modal.convert_recursively_label') }
  49. <p className="form-text text-muted mt-0"> { t('private_legacy_pages.modal.convert_recursively_desc') }</p>
  50. </label>
  51. </div>
  52. );
  53. }
  54. const renderPageIds = () => {
  55. if (status != null && status.pages != null) {
  56. return status.pages.map(page => <div key={page.pageId}><code>{ page.path }</code></div>);
  57. }
  58. return <></>;
  59. };
  60. return (
  61. <Modal size="lg" isOpen={isOpened} toggle={close}>
  62. <ModalHeader tag="h4" toggle={close} className="bg-primary text-light">
  63. { t('private_legacy_pages.modal.title') }
  64. </ModalHeader>
  65. <ModalBody>
  66. <div className="form-group grw-scrollable-modal-body pb-1">
  67. <label>{ t('private_legacy_pages.modal.converting_pages') }:</label><br />
  68. {/* Todo: change the way to show path on modal when too many pages are selected */}
  69. {/* https://redmine.weseek.co.jp/issues/82787 */}
  70. {renderPageIds()}
  71. </div>
  72. {renderForm()}
  73. </ModalBody>
  74. <ModalFooter>
  75. <ApiErrorMessageList errs={errs} />
  76. <button type="button" className="btn btn-primary" onClick={submit}>
  77. <i className="icon-fw icon-refresh" aria-hidden="true"></i>
  78. { t('private_legacy_pages.modal.button_label') }
  79. </button>
  80. </ModalFooter>
  81. </Modal>
  82. );
  83. };