auto-component 0.0.17 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "auto-component",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "main": "src/auto-component.jsx",
5
5
  "module": "src/auto-component.jsx",
6
6
  "peerDependencies": {
7
7
  "react": "*",
8
- "react-dom": "*"
9
- },
10
- "dependencies": {
11
- "js-beautify": "^1.14.10"
8
+ "react-dom": "*",
9
+ "js-beautify": "*"
12
10
  },
13
11
  "files": [
14
12
  "src"
15
- ]
13
+ ],
14
+ "dependencies": {
15
+ "js-beautify": "^1.13.0"
16
+ }
16
17
  }
@@ -1,52 +1,32 @@
1
1
  import { useState, useEffect } from 'react'
2
- import { js_beautify, css_beautify, html_beautify } from 'js-beautify';
3
-
2
+ import beautify from 'js-beautify'
4
3
  import './auto-component.css'
5
4
 
6
- const AutoComponent = () => {
5
+ const AutoComponent = ({ exclusions }) => {
7
6
 
8
7
  //**-----------**/
9
8
  // ** State ** //
10
9
  //**---------**/
11
- const [currentHtml, setHtml] = useState('')
12
- const [currentStyle, setStyles] = useState('')
10
+ const [ currentHtml, setHtml ] = useState('')
11
+ const [ currentStyle, setStyles ] = useState('')
13
12
  const [ currentRequest, setRequest ] = useState('')
13
+ const [ user, setUser ] = useState('')
14
14
 
15
15
  const [ responseData, setResponseData ] = useState(null)
16
16
  const [ requestData, setRequestData ] = useState(null)
17
17
 
18
18
  const [ activeTab, setActiveTab ] = useState('request')
19
-
20
- // test response
21
- const response = {
22
- html: `{projects.map((project, index) => (<div key={index} style={{ border: '1px solid #ccc', padding: '16px', marginBottom: '16px', display: 'flex', flexDirection: 'column', alignItems: 'center' }}><h3>{project.title}</h3><p>{project.description}</p><div style={{ display: 'flex', justifyContent: 'space-between', width: '100%' }}>{project.images.map((image, imgIndex) => (<img key={imgIndex} src={image} alt={\`Project Image \${imgIndex + 1}\`} style={{ width: '100%', maxWidth: '150px', marginBottom: '8px' }} />))}</div><div style={{ display: 'flex', justifyContent: 'space-between', width: '100%' }}><a href={project.gitLink} style={{ color: 'blue' }}>GitHub</a><a href={project.deployLink} style={{ color: 'green' }}>Deployed</a></div></div>))}`
23
- }
24
19
 
25
20
  //**-------------------------**/
26
21
  // ** HTML/CSS Formatting ** //
27
22
  //**-----------------------**/
28
-
29
- function get_beautify() {
30
- // the default is js
31
- var beautify = function(src, config) {
32
- return js_beautify(src, config);
33
- };
34
-
35
- // short aliases
36
- beautify.js = js_beautify;
37
- beautify.css = css_beautify;
38
- beautify.html = html_beautify;
39
-
40
- return beautify;
41
- }
42
-
43
23
  // get the html/style for the current page and set state
44
24
  const htmlContent = () => {
45
25
  const body = document.querySelector('body')
46
26
  const htmlContent = body ? body.innerHTML : ''
47
27
  const cssStyles = document.documentElement.innerHTML
48
28
 
49
- const cleanedHtml = cleanHtml(htmlContent, exclusions)
29
+ const cleanedHtml = cleanExclusions(htmlContent, exclusions)
50
30
  const cleanedStyles = cleanStyles(cssStyles)
51
31
 
52
32
  setHtml(cleanedHtml)
@@ -68,7 +48,7 @@ const AutoComponent = () => {
68
48
  // OR, (full) the element and it's content are excluded from output
69
49
 
70
50
  // partial exclusion
71
- const cleanHtml = (html) => {
51
+ const cleanExclusions = (html) => {
72
52
  const regexExcludeClass = /(<[^>]*\sclass\s*=\s*['"]([^'"]*exclude[^'"]*)['"][^>]*>)[\s\S]*?(<\/[^>]*>)/g;
73
53
  const cleanedHtml = html.replace(regexExcludeClass, '$1$3');
74
54
  const scriptIndex = cleanedHtml.lastIndexOf('<script');
@@ -77,7 +57,7 @@ const AutoComponent = () => {
77
57
  };
78
58
 
79
59
  // full exclusion
80
- const cleanHtmlFull = (html) => {
60
+ const cleanExclusionsFull = (html) => {
81
61
  const regexExcludeClass = /<[^>]*\sclass\s*=\s*['"]([^'"]*exclude[^'"]*)['"][^>]*>[\s\S]*?<\/[^>]*>/g
82
62
  const cleanedHtml = html.replace(regexExcludeClass, '')
83
63
  const scriptIndex = cleanedHtml.lastIndexOf('<script')
@@ -94,13 +74,16 @@ const AutoComponent = () => {
94
74
  const cleanedMatches = matches.map(match => match.replace(/\/\*[\s\S]*?\*\//g, ''))
95
75
  return cleanedMatches
96
76
  }
97
-
98
77
  return null
99
78
  }
100
79
 
80
+ const randomUser = () => {
81
+ setUser(Math.floor(Math.random()*100000))
82
+ }
101
83
 
102
84
  useEffect(() => {
103
85
  htmlContent()
86
+ randomUser()
104
87
  }, [])
105
88
 
106
89
  //**---------------------------**/
@@ -108,23 +91,47 @@ const AutoComponent = () => {
108
91
  //**-------------------------**/
109
92
 
110
93
  // TEMP FUNCTION FOR TESTING
111
- const sendRequest = (data) => {
112
- return(response)
113
- }
94
+ // const sendRequest = (data) => {
95
+ // return(response)
96
+ // }
114
97
 
115
- // make api request with updated state data
98
+ const sendRequest = async () => {
99
+ const url = "https://server-auto-component-46830ff262f8.herokuapp.com/api";
100
+
101
+ try {
102
+ const res = await fetch(url, {
103
+ method: 'POST',
104
+ headers: {
105
+ 'Content-Type': 'application/json',
106
+ },
107
+ body: JSON.stringify(requestData),
108
+ });
109
+
110
+ if (res.ok) {
111
+ const jsonData = await res.json(); // Extract JSON data from the response body
112
+ return jsonData;
113
+ } else {
114
+ throw new Error("Invalid request!");
115
+ }
116
+ } catch (err) {
117
+ console.log(err.message);
118
+ }
119
+ };
120
+
121
+ // make API request with updated state data
116
122
  const handleRequest = async () => {
117
123
  try {
118
- const res = await sendRequest(requestData)
119
- if (res) {
120
- setResponseData(formatHtml(res.html))
121
- setActiveTab('response')
124
+ const resData = await sendRequest();
125
+
126
+ if (resData) {
127
+ setResponseData(formatHtml(resData.response.content));
128
+ setActiveTab('response');
122
129
  }
123
130
  } catch (err) {
124
- console.log(err)
131
+ console.log(err);
125
132
  }
126
- setIsLoading(false)
127
- }
133
+ // setIsLoading(false);
134
+ };
128
135
 
129
136
  //**------------------------**/
130
137
  // ** UI/Button Handling ** //
@@ -137,15 +144,22 @@ const AutoComponent = () => {
137
144
 
138
145
  const handleGenerate = async (e) => {
139
146
  await setRequestData({
140
- request: currentRequest,
141
- html: currentHtml
147
+ "userId": user,
148
+ "request": currentRequest,
149
+ "html": currentHtml
142
150
  })
143
- handleRequest()
144
151
  }
145
152
 
153
+ useEffect(() => {
154
+ if (requestData !== null) {
155
+ handleRequest();
156
+ }
157
+ },[requestData])
158
+
146
159
  const handleReset = () => {
147
160
  setRequest('')
148
161
  setResponseData('')
162
+ randomUser()
149
163
  }
150
164
 
151
165
  const handleRequestTab = () => {