Browse Source

refactoring of code

Yevheniia Akinshyna 1 year ago
parent
commit
55f930a2fd
1 changed files with 9 additions and 21 deletions
  1. 9 21
      src/components/EditorsPage.js

+ 9 - 21
src/components/EditorsPage.js

@@ -55,17 +55,13 @@ export const EditorsPage = ({ onSave }) => {
 
 	useEffect(() => {
 		const timeout = setTimeout(() => {
-			let html, css, javascript;
-			editors.forEach(e => {
-				if (e.type === "html") html = e.text;
-				if (e.type === "css") css = e.text;
-				if (e.type === "javascript") javascript = e.text;
-			});
+			const source = { html: "", css: "", javascript: "" };
+			editors.forEach(({ type, text }) => (source[type] = text));
 			setSrcDoc(`
             <html>
-              <body>${html}</body>
-              <style>${css}</style>
-              <script>${javascript}</script>
+              <body>${source.html}</body>
+              <style>${source.css}</style>
+              <script>${source.javascript}</script>
             </html>
           `);
 		}, 250);
@@ -99,23 +95,15 @@ export const EditorsPage = ({ onSave }) => {
 				);
 			})}
 
-			<button
-				className='button_plus'
-				onClick={newArray => {
-					let editors2 = [...editors];
-					editors2.push(newArray);
-					setEditors(editors2);
-				}}
-			>
+			<button className='button_plus' onClick={() => setEditors([...editors, { type: "", name: "", text: "" }])}>
 				+
 			</button>
 
 			<button
 				className='button_plus'
-				onClick={newArray => {
-					let editors2 = [...editors];
-					editors2.pop(newArray);
-					setEditors(editors2);
+				onClick={() => {
+					editors.pop();
+					setEditors([...editors]);
 				}}
 			>
 				-