auto-component 0.0.18 → 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,13 +1,17 @@
1
1
  {
2
2
  "name": "auto-component",
3
- "version": "0.0.18",
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": "*"
8
+ "react-dom": "*",
9
+ "js-beautify": "*"
9
10
  },
10
11
  "files": [
11
12
  "src"
12
- ]
13
+ ],
14
+ "dependencies": {
15
+ "js-beautify": "^1.13.0"
16
+ }
13
17
  }
@@ -1,39 +1,32 @@
1
1
  import { useState, useEffect } from 'react'
2
- import 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
-
30
23
  // get the html/style for the current page and set state
31
24
  const htmlContent = () => {
32
25
  const body = document.querySelector('body')
33
26
  const htmlContent = body ? body.innerHTML : ''
34
27
  const cssStyles = document.documentElement.innerHTML
35
28
 
36
- const cleanedHtml = cleanHtml(htmlContent, exclusions)
29
+ const cleanedHtml = cleanExclusions(htmlContent, exclusions)
37
30
  const cleanedStyles = cleanStyles(cssStyles)
38
31
 
39
32
  setHtml(cleanedHtml)
@@ -55,7 +48,7 @@ const AutoComponent = () => {
55
48
  // OR, (full) the element and it's content are excluded from output
56
49
 
57
50
  // partial exclusion
58
- const cleanHtml = (html) => {
51
+ const cleanExclusions = (html) => {
59
52
  const regexExcludeClass = /(<[^>]*\sclass\s*=\s*['"]([^'"]*exclude[^'"]*)['"][^>]*>)[\s\S]*?(<\/[^>]*>)/g;
60
53
  const cleanedHtml = html.replace(regexExcludeClass, '$1$3');
61
54
  const scriptIndex = cleanedHtml.lastIndexOf('<script');
@@ -64,7 +57,7 @@ const AutoComponent = () => {
64
57
  };
65
58
 
66
59
  // full exclusion
67
- const cleanHtmlFull = (html) => {
60
+ const cleanExclusionsFull = (html) => {
68
61
  const regexExcludeClass = /<[^>]*\sclass\s*=\s*['"]([^'"]*exclude[^'"]*)['"][^>]*>[\s\S]*?<\/[^>]*>/g
69
62
  const cleanedHtml = html.replace(regexExcludeClass, '')
70
63
  const scriptIndex = cleanedHtml.lastIndexOf('<script')
@@ -81,13 +74,16 @@ const AutoComponent = () => {
81
74
  const cleanedMatches = matches.map(match => match.replace(/\/\*[\s\S]*?\*\//g, ''))
82
75
  return cleanedMatches
83
76
  }
84
-
85
77
  return null
86
78
  }
87
79
 
80
+ const randomUser = () => {
81
+ setUser(Math.floor(Math.random()*100000))
82
+ }
88
83
 
89
84
  useEffect(() => {
90
85
  htmlContent()
86
+ randomUser()
91
87
  }, [])
92
88
 
93
89
  //**---------------------------**/
@@ -95,23 +91,47 @@ const AutoComponent = () => {
95
91
  //**-------------------------**/
96
92
 
97
93
  // TEMP FUNCTION FOR TESTING
98
- const sendRequest = (data) => {
99
- return(response)
100
- }
94
+ // const sendRequest = (data) => {
95
+ // return(response)
96
+ // }
101
97
 
102
- // 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
103
122
  const handleRequest = async () => {
104
123
  try {
105
- const res = await sendRequest(requestData)
106
- if (res) {
107
- setResponseData(formatHtml(res.html))
108
- setActiveTab('response')
124
+ const resData = await sendRequest();
125
+
126
+ if (resData) {
127
+ setResponseData(formatHtml(resData.response.content));
128
+ setActiveTab('response');
109
129
  }
110
130
  } catch (err) {
111
- console.log(err)
131
+ console.log(err);
112
132
  }
113
- setIsLoading(false)
114
- }
133
+ // setIsLoading(false);
134
+ };
115
135
 
116
136
  //**------------------------**/
117
137
  // ** UI/Button Handling ** //
@@ -124,15 +144,22 @@ const AutoComponent = () => {
124
144
 
125
145
  const handleGenerate = async (e) => {
126
146
  await setRequestData({
127
- request: currentRequest,
128
- html: currentHtml
147
+ "userId": user,
148
+ "request": currentRequest,
149
+ "html": currentHtml
129
150
  })
130
- handleRequest()
131
151
  }
132
152
 
153
+ useEffect(() => {
154
+ if (requestData !== null) {
155
+ handleRequest();
156
+ }
157
+ },[requestData])
158
+
133
159
  const handleReset = () => {
134
160
  setRequest('')
135
161
  setResponseData('')
162
+ randomUser()
136
163
  }
137
164
 
138
165
  const handleRequestTab = () => {