WIP
Showing
1 changed file
with
22 additions
and
5 deletions
| ... | @@ -44,7 +44,6 @@ function App() { | ... | @@ -44,7 +44,6 @@ function App() { |
| 44 | 44 | ||
| 45 | async function handleSubmit(e){ | 45 | async function handleSubmit(e){ |
| 46 | e.preventDefault(); | 46 | e.preventDefault(); |
| 47 | // console.log(chatInput) | ||
| 48 | const userInput = ['what', 'why', 'when', 'where' , 'which', 'did', 'do', 'how', 'can', 'are', 'who']; | 47 | const userInput = ['what', 'why', 'when', 'where' , 'which', 'did', 'do', 'how', 'can', 'are', 'who']; |
| 49 | const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi'); | 48 | const userInputRegex = new RegExp(`\\b(${userInput.join('|')})\\b`, 'gi'); |
| 50 | const inputMatches = chatInput.match(userInputRegex); | 49 | const inputMatches = chatInput.match(userInputRegex); |
| ... | @@ -53,25 +52,43 @@ function App() { | ... | @@ -53,25 +52,43 @@ function App() { |
| 53 | const userPunctuationRegex = new RegExp(`[${userPunctuation.join('')}]$`); | 52 | const userPunctuationRegex = new RegExp(`[${userPunctuation.join('')}]$`); |
| 54 | const punctuationMatches = chatInput.match(userPunctuationRegex); | 53 | const punctuationMatches = chatInput.match(userPunctuationRegex); |
| 55 | 54 | ||
| 56 | // console.log(punctuationMatches) | ||
| 57 | var userModifiedInput = chatInput | 55 | var userModifiedInput = chatInput |
| 58 | 56 | ||
| 59 | if (!punctuationMatches) { | 57 | if (!punctuationMatches) { |
| 60 | if (!inputMatches) { | 58 | if (!inputMatches) { |
| 61 | userModifiedInput = chatInput + "."; | 59 | userModifiedInput = chatInput + "."; |
| 62 | // console.log("not a question!") | ||
| 63 | } else { | 60 | } else { |
| 64 | userModifiedInput = chatInput + "?"; | 61 | userModifiedInput = chatInput + "?"; |
| 65 | // console.log("its a question!") | ||
| 66 | } | 62 | } |
| 67 | } | 63 | } |
| 68 | 64 | ||
| 69 | let chatLogNew = [...chatLog, { user: "me", message: `${userModifiedInput}`} ] | 65 | let chatLogNew = [...chatLog, { user: "me", message: `${userModifiedInput}`} ] |
| 66 | const messages = chatLogNew.map((message) => message.message).join("\n") | ||
| 67 | |||
| 68 | // Find the table format and replace it with HTML table tags | ||
| 69 | const parsedData = chatLogNew.map(message => { | ||
| 70 | if (message.message.includes("|")) { console.log("Have") | ||
| 71 | const rows = message.message.split("\n"); | ||
| 72 | const headers = rows[0].split("|").filter(h => h); | ||
| 73 | const divider = headers.map(h => "-".repeat(h.length)); | ||
| 74 | const data = rows.slice(2, rows.length - 1).map(row => { | ||
| 75 | const values = row.split("|").filter(v => v); | ||
| 76 | return values.reduce((obj, value, index) => { | ||
| 77 | obj[headers[index]] = value.trim(); | ||
| 78 | return obj; | ||
| 79 | }, {}); | ||
| 80 | }); | ||
| 81 | const table = `<table><thead><tr>${headers.map(h => `<th>${h.trim()}</th>`).join("")}</tr></thead><tbody>${data.map(row => `<tr>${headers.map(h => `<td>${row[h.trim()]}</td>`).join("")}</tr>`).join("")}</tbody></table>`; | ||
| 82 | return table; | ||
| 83 | } else { | ||
| 84 | return message.message; | ||
| 85 | } | ||
| 86 | }).join("\n"); | ||
| 87 | |||
| 70 | setChatInput(""); | 88 | setChatInput(""); |
| 71 | setChatLog(chatLogNew) | 89 | setChatLog(chatLogNew) |
| 72 | 90 | ||
| 73 | // fetch response to the api combining the chat log array of messages and seinding it as a message to localhost:3000 as a post | 91 | // fetch response to the api combining the chat log array of messages and seinding it as a message to localhost:3000 as a post |
| 74 | const messages = chatLogNew.map((message) => message.message).join("\n") | ||
| 75 | 92 | ||
| 76 | try { | 93 | try { |
| 77 | const response = await fetch(process.env.REACT_APP_SERVER_URL + "/api", { | 94 | const response = await fetch(process.env.REACT_APP_SERVER_URL + "/api", { | ... | ... |
-
Please register or sign in to post a comment