Project.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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='/propjects'>
  80. <button className='btn_search' style={{ margin: 10, marginBottom: 50 }}>
  81. Back to
  82. </button>
  83. </Link>
  84. </div>
  85. <br />
  86. {editors?.map((data, index) => (
  87. <>
  88. <Editor
  89. onDelete={() => setEditors(editors?.filter(item => item !== data))}
  90. data={data}
  91. onChange={({ type, name, text }) =>
  92. setEditors([...editors.slice(0, index), { type, name, text }, ...editors.slice(index, 1)])
  93. }
  94. />
  95. </>
  96. ))}
  97. <br />
  98. <div>
  99. <div>
  100. <iframe
  101. className='pane'
  102. srcDoc={srcDoc}
  103. title='output'
  104. sandbox='allow-scripts'
  105. frameBorder='0'
  106. style={{ marginTop: 20 }}
  107. width='95%'
  108. height='95%'
  109. />
  110. </div>
  111. <div>
  112. <p className='text'>Name of your project: </p>
  113. <input
  114. className='input_title'
  115. placeholder='Name of your project'
  116. value={title}
  117. onChange={e => setTitle(e.target.value)}
  118. />
  119. </div>
  120. <p className='text'>Description: </p>
  121. <textarea
  122. className='text_desc'
  123. placeholder='Description'
  124. value={description}
  125. onChange={e => setDescription(e.target.value)}
  126. style={{ marginBottom: 50 }}
  127. />
  128. </div>
  129. </div>
  130. );
  131. };
  132. const ConnectedProject = connect(
  133. state => ({
  134. titleText: state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.title,
  135. descriptionText: state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.description,
  136. nameText: state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.name,
  137. filesArr: state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.files,
  138. }),
  139. { getSnippet: actionSnippetById },
  140. )(ProjectSnippet);
  141. export default ConnectedProject;