Project.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { useEffect } from "react";
  2. import { connect } from "react-redux";
  3. import { actionSnippetById } from "./../actions/actionSnippetById";
  4. import { useState } from "react";
  5. import { Editor } from "../components/Editor";
  6. import "../components/EditorsPage.css";
  7. import { Link } from "react-router-dom";
  8. // const [editors, setEditors] = useState(datas);
  9. // const [title, setTitle] = useState("");
  10. // const [description, setDescription] = useState("");
  11. // const [srcDoc, setSrcDoc] = useState("");
  12. // const [theme, setTheme] = useState();
  13. // const [font, setFont] = useState();
  14. // useEffect(() => {
  15. // const timeout = setTimeout(() => {
  16. // let html, css, javascript;
  17. // editors.forEach((e) => {
  18. // if (e.type === "html") html = e.text;
  19. // if (e.type === "css") css = e.text;
  20. // if (e.type === "javascript") javascript = e.text;
  21. // });
  22. // setSrcDoc(`
  23. // <html>
  24. // <body>${html}</body>
  25. // <style>${css}</style>
  26. // <script>${javascript}</script>
  27. // </html>
  28. // `);
  29. // }, 250);
  30. // return () => clearTimeout(timeout);
  31. // }, [editors]);
  32. const ProjectSnippet = ({
  33. getSnippet,
  34. match: {
  35. params: { _id },
  36. },
  37. titleText,
  38. descriptionText,
  39. filesArr,
  40. nameText,
  41. }) => {
  42. useEffect(() => {
  43. getSnippet(_id);
  44. }, [_id, getSnippet]);
  45. const [editors, setEditors] = useState([]);
  46. const [title, setTitle] = useState("");
  47. const [description, setDescription] = useState("");
  48. useEffect(() => {
  49. setEditors(filesArr);
  50. setTitle(titleText);
  51. setDescription(descriptionText);
  52. }, [filesArr, titleText, descriptionText]);
  53. const [srcDoc, setSrcDoc] = useState("");
  54. const html = editors?.filter((e) => {
  55. return e?.type === "html";
  56. })[0]?.text;
  57. const css = editors?.filter((e) => {
  58. return e?.type === "css";
  59. })[0]?.text;
  60. const javascript = editors?.filter((e) => {
  61. return e?.type === "javascript";
  62. })[0]?.text;
  63. console.log(javascript);
  64. useEffect(() => {
  65. const timeout = setTimeout(() => {
  66. setSrcDoc(`
  67. <html>
  68. <body>${html}</body>
  69. <style>${css}</style>
  70. <script>${javascript}</script>
  71. </html>
  72. `);
  73. }, 250);
  74. return () => clearTimeout(timeout);
  75. }, [html, css, javascript]);
  76. return (
  77. <div>
  78. <div>
  79. <Link to="/search">
  80. <button
  81. className="btn_search"
  82. style={{ margin: 10, marginBottom: 50 }}
  83. >
  84. Back to search
  85. </button>
  86. </Link>
  87. </div>
  88. <br />
  89. {editors?.map((data, index) => (
  90. <>
  91. <Editor
  92. onDelete={() =>
  93. setEditors(editors?.filter((item) => item !== data))
  94. }
  95. data={data}
  96. onChange={({ type, name, text }) =>
  97. setEditors([
  98. ...editors.slice(0, index),
  99. { type, name, text },
  100. ...editors.slice(index, 1),
  101. ])
  102. }
  103. />
  104. </>
  105. ))}
  106. <br />
  107. <div>
  108. <div>
  109. <iframe
  110. className="pane"
  111. srcDoc={srcDoc}
  112. title="output"
  113. sandbox="allow-scripts"
  114. frameBorder="0"
  115. style={{ marginTop: 20 }}
  116. width="95%"
  117. height="95%"
  118. />
  119. </div>
  120. <div>
  121. <p className="text">Name of your project: </p>
  122. <input
  123. className="input_title"
  124. placeholder="Name of your project"
  125. value={title}
  126. onChange={(e) => setTitle(e.target.value)}
  127. />
  128. </div>
  129. <p className="text">Description: </p>
  130. <textarea
  131. className="text_desc"
  132. placeholder="Description"
  133. value={description}
  134. onChange={(e) => setDescription(e.target.value)}
  135. style={{ marginBottom: 50 }}
  136. />
  137. </div>
  138. </div>
  139. );
  140. };
  141. const ConnectedProject = connect(
  142. (state) => ({
  143. titleText:
  144. state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.title,
  145. descriptionText:
  146. state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.description,
  147. nameText: state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.name,
  148. filesArr: state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.files,
  149. }),
  150. { getSnippet: actionSnippetById }
  151. )(ProjectSnippet);
  152. export default ConnectedProject;