Update chat.js
Browse files
chat.js
CHANGED
@@ -43,10 +43,8 @@ function resetConversation() {
|
|
43 |
function selectTileType(type) {
|
44 |
state.tileType = type;
|
45 |
state.step = 'area';
|
46 |
-
|
47 |
addMessage('user', `${type.charAt(0).toUpperCase() + type.slice(1)} tiles`);
|
48 |
showTyping();
|
49 |
-
|
50 |
setTimeout(() => {
|
51 |
hideTyping();
|
52 |
addMessage('bot', `Great! What is the total area to cover (in sq.ft)?`);
|
@@ -56,7 +54,6 @@ function selectTileType(type) {
|
|
56 |
function sendMessage() {
|
57 |
const message = userInput.value.trim();
|
58 |
if (!message) return;
|
59 |
-
|
60 |
addMessage('user', message);
|
61 |
userInput.value = '';
|
62 |
processUserMessage(message);
|
@@ -64,10 +61,8 @@ function sendMessage() {
|
|
64 |
|
65 |
function processUserMessage(message) {
|
66 |
showTyping();
|
67 |
-
|
68 |
setTimeout(() => {
|
69 |
hideTyping();
|
70 |
-
|
71 |
if (state.step === 'area') {
|
72 |
const area = parseFloat(message);
|
73 |
if (isNaN(area) || area <= 0) {
|
@@ -76,9 +71,7 @@ function processUserMessage(message) {
|
|
76 |
state.area = area;
|
77 |
state.step = 'tileLength';
|
78 |
addMessage('bot', 'Now enter the tile length (in feet):');
|
79 |
-
}
|
80 |
-
|
81 |
-
else if (state.step === 'tileLength') {
|
82 |
const length = parseFloat(message);
|
83 |
if (isNaN(length) || length <= 0) {
|
84 |
return addMessage('bot', 'Please enter a valid positive number for tile length.');
|
@@ -86,9 +79,7 @@ function processUserMessage(message) {
|
|
86 |
state.tileLength = length;
|
87 |
state.step = 'tileWidth';
|
88 |
addMessage('bot', 'Now enter the tile width (in feet):');
|
89 |
-
}
|
90 |
-
|
91 |
-
else if (state.step === 'tileWidth') {
|
92 |
const width = parseFloat(message);
|
93 |
if (isNaN(width) || width <= 0) {
|
94 |
return addMessage('bot', 'Please enter a valid positive number for tile width.');
|
@@ -102,15 +93,15 @@ function processUserMessage(message) {
|
|
102 |
function calculateTiles() {
|
103 |
const { tileLength, tileWidth, area, tileType } = state;
|
104 |
const tileArea = tileLength * tileWidth;
|
105 |
-
const numTiles = Math.ceil((area / tileArea) * 1.1);
|
106 |
-
const numBoxes = Math.ceil(numTiles / 10);
|
107 |
|
108 |
chatArea.insertAdjacentHTML('beforeend', `
|
109 |
<div class="bot-message bg-gray-100 rounded-lg p-4 mb-3">
|
110 |
<p class="font-semibold">Calculation Results:</p>
|
111 |
<p>🧱 Tile Type: ${tileType}</p>
|
112 |
<p>📐 Area to Cover: ${area} sq.ft</p>
|
113 |
-
<p>🧮 Tile Size: ${tileLength} ft
|
114 |
<p class="mt-2">🔢 <strong>Tiles Needed:</strong> ${numTiles} (${numBoxes} boxes)</p>
|
115 |
</div>
|
116 |
`);
|
@@ -132,10 +123,13 @@ function calculateTiles() {
|
|
132 |
if (data.recommended_products?.length) {
|
133 |
loadProductRecommendations(data.recommended_products);
|
134 |
} else {
|
135 |
-
recommendations.innerHTML = `<div class="col-span-4 text-center py-4 text-gray-500">No
|
136 |
}
|
137 |
})
|
138 |
-
.catch(
|
|
|
|
|
|
|
139 |
}
|
140 |
|
141 |
function loadProductRecommendations(products) {
|
|
|
43 |
function selectTileType(type) {
|
44 |
state.tileType = type;
|
45 |
state.step = 'area';
|
|
|
46 |
addMessage('user', `${type.charAt(0).toUpperCase() + type.slice(1)} tiles`);
|
47 |
showTyping();
|
|
|
48 |
setTimeout(() => {
|
49 |
hideTyping();
|
50 |
addMessage('bot', `Great! What is the total area to cover (in sq.ft)?`);
|
|
|
54 |
function sendMessage() {
|
55 |
const message = userInput.value.trim();
|
56 |
if (!message) return;
|
|
|
57 |
addMessage('user', message);
|
58 |
userInput.value = '';
|
59 |
processUserMessage(message);
|
|
|
61 |
|
62 |
function processUserMessage(message) {
|
63 |
showTyping();
|
|
|
64 |
setTimeout(() => {
|
65 |
hideTyping();
|
|
|
66 |
if (state.step === 'area') {
|
67 |
const area = parseFloat(message);
|
68 |
if (isNaN(area) || area <= 0) {
|
|
|
71 |
state.area = area;
|
72 |
state.step = 'tileLength';
|
73 |
addMessage('bot', 'Now enter the tile length (in feet):');
|
74 |
+
} else if (state.step === 'tileLength') {
|
|
|
|
|
75 |
const length = parseFloat(message);
|
76 |
if (isNaN(length) || length <= 0) {
|
77 |
return addMessage('bot', 'Please enter a valid positive number for tile length.');
|
|
|
79 |
state.tileLength = length;
|
80 |
state.step = 'tileWidth';
|
81 |
addMessage('bot', 'Now enter the tile width (in feet):');
|
82 |
+
} else if (state.step === 'tileWidth') {
|
|
|
|
|
83 |
const width = parseFloat(message);
|
84 |
if (isNaN(width) || width <= 0) {
|
85 |
return addMessage('bot', 'Please enter a valid positive number for tile width.');
|
|
|
93 |
function calculateTiles() {
|
94 |
const { tileLength, tileWidth, area, tileType } = state;
|
95 |
const tileArea = tileLength * tileWidth;
|
96 |
+
const numTiles = Math.ceil((area / tileArea) * 1.1); // 10% extra
|
97 |
+
const numBoxes = Math.ceil(numTiles / 10); // 10 tiles/box
|
98 |
|
99 |
chatArea.insertAdjacentHTML('beforeend', `
|
100 |
<div class="bot-message bg-gray-100 rounded-lg p-4 mb-3">
|
101 |
<p class="font-semibold">Calculation Results:</p>
|
102 |
<p>🧱 Tile Type: ${tileType}</p>
|
103 |
<p>📐 Area to Cover: ${area} sq.ft</p>
|
104 |
+
<p>🧮 Tile Size: ${tileLength} ft × ${tileWidth} ft</p>
|
105 |
<p class="mt-2">🔢 <strong>Tiles Needed:</strong> ${numTiles} (${numBoxes} boxes)</p>
|
106 |
</div>
|
107 |
`);
|
|
|
123 |
if (data.recommended_products?.length) {
|
124 |
loadProductRecommendations(data.recommended_products);
|
125 |
} else {
|
126 |
+
recommendations.innerHTML = `<div class="col-span-4 text-center py-4 text-gray-500">No product recommendations found.</div>`;
|
127 |
}
|
128 |
})
|
129 |
+
.catch(err => {
|
130 |
+
console.error('Recommendation error:', err);
|
131 |
+
recommendations.innerHTML = `<div class="col-span-4 text-center py-4 text-red-500">Error fetching recommendations.</div>`;
|
132 |
+
});
|
133 |
}
|
134 |
|
135 |
function loadProductRecommendations(products) {
|