SaveExportModal.jsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React from "react";
  2. import { useTranslation } from "react-i18next";
  3. import { toast } from "react-toastify";
  4. import DownloadLink from "./DownloadLink";
  5. import Modal from "../../ui/Modal";
  6. import useGame from "../../hooks/useGame";
  7. const SaveExportGameModal = ({ show, setShow }) => {
  8. const { t } = useTranslation();
  9. const { saveGame, getGame } = useGame();
  10. const handleSave = React.useCallback(async () => {
  11. try {
  12. await saveGame();
  13. toast.success(t("Game saved"), { autoClose: 1500 });
  14. } catch (e) {
  15. if (e.message === "Forbidden") {
  16. toast.error(t("Action forbidden. Try logging in again."));
  17. } else {
  18. console.log(e);
  19. toast.error(t("Error while saving game. Try again later..."));
  20. }
  21. }
  22. setShow(false);
  23. }, [saveGame, setShow, t]);
  24. React.useEffect(() => {
  25. const callback = (e) => {
  26. if (e.ctrlKey && e.key === "s") {
  27. e.preventDefault();
  28. handleSave();
  29. }
  30. };
  31. window.addEventListener("keydown", callback);
  32. return () => window.removeEventListener("keydown", callback);
  33. }, [handleSave]);
  34. return (
  35. <Modal title={t("Save & export")} setShow={setShow} show={show}>
  36. <header>
  37. <h3>{t("Save you work?")}</h3>
  38. </header>
  39. <section>
  40. <p>
  41. {t(
  42. "You game will be saved and you can access it later in the studio!"
  43. )}
  44. </p>
  45. <p>
  46. {t(
  47. "If you have checked the publish checkbox your game will be public."
  48. )}
  49. </p>
  50. <button className="success button icon" onClick={handleSave}>
  51. {t("Save game")}
  52. <img
  53. src={"https://icongr.am/entypo/save.svg?size=24&color=f9fbfa"}
  54. alt="icon"
  55. />
  56. </button>
  57. </section>
  58. <header>
  59. <h3>{t("Export the board?")}</h3>
  60. </header>
  61. <section>
  62. <p>{t("You can also save it to your computer.")}</p>
  63. <DownloadLink getData={getGame} />
  64. </section>
  65. </Modal>
  66. );
  67. };
  68. export default SaveExportGameModal;