Navbar.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React from 'react';
  2. import queryString from 'query-string';
  3. import useStore from '../store';
  4. import useActions from '../actions';
  5. import NavbarComponent from '../components/Navbar';
  6. import Search from './Search';
  7. import Thumbnails from './Thumbnails';
  8. import AnnotationList from './AnnotationList';
  9. import { downloadFileWithUri, printPdf } from '../helpers/utility';
  10. import { delay } from '../helpers/time';
  11. const Navbar: React.FC = () => {
  12. const [{ navbarState, displayMode, info, totalPage }, dispatch] = useStore();
  13. const { setNavbar, setLoading } = useActions(dispatch);
  14. const onClick = (state: string): void => {
  15. switch (state) {
  16. case 'export': {
  17. const parsed = queryString.parse(window.location.search);
  18. const fileName = atob(info.token as string);
  19. let outputPath = `/api/v1/output.pdf?f=${info.token}`;
  20. if (parsed.watermark) {
  21. outputPath = `/api/v1/output.pdf?f=${
  22. info.token
  23. }&user_id=${window.encodeURI(parsed.watermark)}&pages=0-${totalPage}`;
  24. }
  25. setLoading(true);
  26. downloadFileWithUri(fileName, outputPath);
  27. // downloadFileWithUri(fileName, `http://127.0.0.1:3000${outputPath}`);
  28. delay(1000).then(() => {
  29. setLoading(false);
  30. });
  31. break;
  32. }
  33. case 'print': {
  34. const parsed = queryString.parse(window.location.search);
  35. let outputPath = `/api/v1/output.pdf?f=${info.token}`;
  36. if (parsed.watermark) {
  37. outputPath = `/api/v1/output.pdf?f=${
  38. info.token
  39. }&user_id=${window.encodeURI(parsed.watermark)}&pages=0-${totalPage}`;
  40. }
  41. setLoading(true);
  42. printPdf(outputPath).then(() => {
  43. setLoading(false);
  44. });
  45. // printPdf(`http://127.0.0.1:3000${outputPath}`).then(() => {
  46. // setLoading(false);
  47. // });
  48. break;
  49. }
  50. case navbarState:
  51. setNavbar('');
  52. break;
  53. default:
  54. setNavbar(state);
  55. break;
  56. }
  57. };
  58. return (
  59. <NavbarComponent
  60. onClick={onClick}
  61. navbarState={navbarState}
  62. displayMode={displayMode}
  63. fileName={
  64. info.token ? decodeURIComponent(escape(window.atob(info.token))) : ''
  65. }
  66. >
  67. <Search />
  68. <AnnotationList />
  69. <Thumbnails />
  70. </NavbarComponent>
  71. );
  72. };
  73. export default Navbar;