Create chat.js
Browse files
chat.js
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const state = {
|
2 |
+
step: 'tileType',
|
3 |
+
tileType: null,
|
4 |
+
area: null,
|
5 |
+
tileLength: null,
|
6 |
+
tileWidth: null
|
7 |
+
};
|
8 |
+
|
9 |
+
const chatArea = document.getElementById('chat-area');
|
10 |
+
const userInput = document.getElementById('user-input');
|
11 |
+
const recommendations = document.getElementById('recommendations');
|
12 |
+
const resetBtn = document.getElementById('reset-btn');
|
13 |
+
|
14 |
+
resetBtn.addEventListener('click', resetConversation);
|
15 |
+
userInput.addEventListener('keypress', (e) => {
|
16 |
+
if (e.key === 'Enter') sendMessage();
|
17 |
+
});
|
18 |
+
|
19 |
+
resetConversation();
|
20 |
+
|
21 |
+
function resetConversation() {
|
22 |
+
Object.assign(state, {
|
23 |
+
step: 'tileType',
|
24 |
+
tileType: null,
|
25 |
+
area: null,
|
26 |
+
tileLength: null,
|
27 |
+
tileWidth: null
|
28 |
+
});
|
29 |
+
|
30 |
+
chatArea.innerHTML = `
|
31 |
+
<div class="bot-message bg-gray-100 rounded-lg p-4 max-w-xs mb-3">
|
32 |
+
<p>Hello! 👋 I'm your Tile Calculator Assistant. Let's estimate how many tiles you need.</p>
|
33 |
+
<p class="mt-2">Are you looking for <span class="font-semibold">floor</span> or <span class="font-semibold">wall</span> tiles?</p>
|
34 |
+
<div class="flex gap-2 mt-3">
|
35 |
+
<button onclick="selectTileType('floor')" class="quick-reply bg-white text-indigo-600 border border-indigo-600 px-4 py-2 rounded-full font-medium hover:bg-indigo-50">Floor</button>
|
36 |
+
<button onclick="selectTileType('wall')" class="quick-reply bg-white text-indigo-600 border border-indigo-600 px-4 py-2 rounded-full font-medium hover:bg-indigo-50">Wall</button>
|
37 |
+
</div>
|
38 |
+
</div>
|
39 |
+
`;
|
40 |
+
recommendations.innerHTML = '';
|
41 |
+
}
|
42 |
+
|
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)?`);
|
53 |
+
}, 800);
|
54 |
+
}
|
55 |
+
|
56 |
+
function sendMessage() {
|
57 |
+
const message = userInput.value.trim();
|
58 |
+
if (!message) return;
|
59 |
+
|
60 |
+
addMessage('user', message);
|
61 |
+
userInput.value = '';
|
62 |
+
processUserMessage(message);
|
63 |
+
}
|
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) {
|
74 |
+
return addMessage('bot', 'Please enter a valid positive number for area (e.g., 100).');
|
75 |
+
}
|
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.');
|
85 |
+
}
|
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.');
|
95 |
+
}
|
96 |
+
state.tileWidth = width;
|
97 |
+
calculateTiles();
|
98 |
+
}
|
99 |
+
}, 800);
|
100 |
+
}
|
101 |
+
|
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 x ${tileWidth} ft</p>
|
114 |
+
<p class="mt-2">🔢 <strong>Tiles Needed:</strong> ${numTiles} (${numBoxes} boxes)</p>
|
115 |
+
</div>
|
116 |
+
`);
|
117 |
+
|
118 |
+
state.step = 'complete';
|
119 |
+
|
120 |
+
fetch('/recommend', {
|
121 |
+
method: 'POST',
|
122 |
+
headers: { 'Content-Type': 'application/json' },
|
123 |
+
body: JSON.stringify({
|
124 |
+
tile_type: tileType,
|
125 |
+
coverage: tileArea,
|
126 |
+
area: area,
|
127 |
+
price_range: [3, 10]
|
128 |
+
})
|
129 |
+
})
|
130 |
+
.then(res => res.json())
|
131 |
+
.then(data => {
|
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 strong recommendations.</div>`;
|
136 |
+
}
|
137 |
+
})
|
138 |
+
.catch(console.error);
|
139 |
+
}
|
140 |
+
|
141 |
+
function loadProductRecommendations(products) {
|
142 |
+
recommendations.innerHTML = '';
|
143 |
+
products.slice(0, 4).forEach(p => {
|
144 |
+
recommendations.insertAdjacentHTML('beforeend', `
|
145 |
+
<div class="bg-white rounded-lg overflow-hidden shadow-sm border border-gray-100">
|
146 |
+
<div class="h-32 bg-gray-200 flex items-center justify-center">
|
147 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-12 w-12 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
148 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1" d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zM14 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zM14 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z" />
|
149 |
+
</svg>
|
150 |
+
</div>
|
151 |
+
<div class="p-3">
|
152 |
+
<h4 class="font-medium text-gray-800">${p.name || 'Tile Product'}</h4>
|
153 |
+
<p class="text-sm text-gray-600 mt-1">${p.price || '$0.00'}/box</p>
|
154 |
+
<button class="mt-2 w-full bg-indigo-600 text-white py-1 rounded text-sm hover:bg-indigo-700 transition">View Details</button>
|
155 |
+
</div>
|
156 |
+
</div>
|
157 |
+
`);
|
158 |
+
});
|
159 |
+
}
|
160 |
+
|
161 |
+
function addMessage(sender, text) {
|
162 |
+
const div = document.createElement('div');
|
163 |
+
div.className = `${sender}-message ${sender === 'user' ? 'ml-auto bg-indigo-600 text-white' : 'bg-gray-100'} rounded-lg p-4 max-w-xs mb-3`;
|
164 |
+
div.textContent = text;
|
165 |
+
chatArea.appendChild(div);
|
166 |
+
chatArea.scrollTop = chatArea.scrollHeight;
|
167 |
+
}
|
168 |
+
|
169 |
+
function showTyping() {
|
170 |
+
const typing = document.createElement('div');
|
171 |
+
typing.className = 'typing-indicator bg-gray-100 rounded-lg p-4 max-w-xs mb-3 flex gap-1';
|
172 |
+
typing.id = 'typing-indicator';
|
173 |
+
typing.innerHTML = '<span class="w-2 h-2 bg-gray-400 rounded-full"></span><span class="w-2 h-2 bg-gray-400 rounded-full"></span><span class="w-2 h-2 bg-gray-400 rounded-full"></span>';
|
174 |
+
chatArea.appendChild(typing);
|
175 |
+
chatArea.scrollTop = chatArea.scrollHeight;
|
176 |
+
}
|
177 |
+
|
178 |
+
function hideTyping() {
|
179 |
+
const el = document.getElementById('typing-indicator');
|
180 |
+
if (el) el.remove();
|
181 |
+
}
|