WipPageAlert.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import React, { useCallback } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { toastSuccess, toastError } from '~/client/util/toastr';
  4. import { useSWRMUTxCurrentPage, useSWRxCurrentPage } from '~/stores/page';
  5. import { mutatePageTree } from '~/stores/page-listing';
  6. import { publish } from '../../client/services/page-operation';
  7. export const WipPageAlert = (): JSX.Element => {
  8. const { t } = useTranslation();
  9. const { data: currentPage } = useSWRxCurrentPage();
  10. const { trigger: mutateCurrentPage } = useSWRMUTxCurrentPage();
  11. const clickPagePublishButton = useCallback(async() => {
  12. const pageId = currentPage?._id;
  13. if (pageId == null) {
  14. return;
  15. }
  16. try {
  17. await publish(pageId);
  18. await mutateCurrentPage();
  19. await mutatePageTree();
  20. toastSuccess(t('wip_page.success_publish_page'));
  21. }
  22. catch {
  23. toastError(t('wip_page.fail_publish_page'));
  24. }
  25. }, [currentPage?._id, mutateCurrentPage, t]);
  26. if (!currentPage?.wip) {
  27. return <></>;
  28. }
  29. return (
  30. <p className="d-flex align-items-center alert alert-secondary py-3 px-4">
  31. <span className="material-symbols-outlined me-1 fs-5">info</span>
  32. <span>{t('wip_page.alert')}</span>
  33. <button
  34. type="button"
  35. className="btn btn-outline-secondary ms-auto"
  36. onClick={clickPagePublishButton}
  37. >
  38. {t('wip_page.publish_page') }
  39. </button>
  40. </p>
  41. );
  42. };