-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.js
More file actions
132 lines (117 loc) · 5.52 KB
/
shared.js
File metadata and controls
132 lines (117 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// --- Shared Logic & Data ---
const LOCATION_DATA = {
"Karnataka": ["Bangalore Urban", "Bangalore Rural", "Mysore", "Hubli-Dharwad", "Mangalore", "Belgaum", "Davanagere", "Bellary", "Bijapur", "Gulbarga", "Ramanagara", "Mandya"],
"Maharashtra": ["Mumbai City", "Mumbai Suburban", "Pune", "Nagpur", "Nashik", "Aurangabad", "Solapur", "Amravati"],
"Tamil Nadu": ["Chennai", "Coimbatore", "Madurai", "Tiruchirappalli", "Salem", "Tirunelveli", "Erode"],
"Kerala": ["Thiruvananthapuram", "Kochi", "Kozhikode", "Thrissur", "Kollam", "Alappuzha"],
"Andhra Pradesh": ["Visakhapatnam", "Vijayawada", "Guntur", "Nellore", "Kurnool", "Rajahmundry"]
};
// --- Mock Data ---
const VENDORS_DATA = [
{ id: 'v1', type: 'food', name: 'Nandhana Palace', area: 'Nagarbhavi', rating: 4.5, time: '30 mins', image: 'https://via.placeholder.com/150/ff7f50/ffffff?text=NP', tags: ['Andhra', 'Biryani'] },
{ id: 'v2', type: 'food', name: 'Empire Restaurant', area: 'Indiranagar', rating: 4.2, time: '45 mins', image: 'https://via.placeholder.com/150/dc143c/ffffff?text=EMP', tags: ['Mughlai', 'Kebabs'] },
{ id: 'v3', type: 'food', name: 'A2B - Adyar Ananda Bhavan', area: 'Nagarbhavi', rating: 4.6, time: '25 mins', image: 'https://via.placeholder.com/150/ffd700/000000?text=A2B', tags: ['South Indian', 'Sweets'] }
];
const PRODUCTS_DATA = {
'v1': [
{ id: 'p1', name: 'Andhra Chicken Meal', price: 250, veg: false, desc: 'Authentic spicy Andhra style chicken meals.' },
{ id: 'p2', name: 'Hyderabadi Chicken Biryani', price: 280, veg: false, desc: 'Aromatic basmati rice cooked with tender chicken and spices.' }
]
};
// --- Firebase Initialization ---
const firebaseConfig = {
apiKey: "AIzaSyBxtBndq0keNI7TYcP2SglbHD_Nnfwz1yA",
authDomain: "grami-d6a46.firebaseapp.com",
projectId: "grami-d6a46",
storageBucket: "grami-d6a46.firebasestorage.app",
messagingSenderId: "746609393277",
appId: "1:746609393277:web:63a9a645f8a4111fca7b14",
measurementId: "G-LHHM196FF4"
};
// Initialize Firebase
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
const db = firebase.database();
// --- Cloud Data Functions [UPDATED] ---
const getDrivers = async () => {
try {
const snapshot = await db.ref('drivers').once('value');
let drivers = snapshot.val();
if (!drivers) {
// Initial Seed for Cloud
drivers = [
{ id: 1, name: 'John Doe', type: 'auto', state: 'Karnataka', district: 'Bangalore Urban', area: 'Nagarbhavi', vehicle: 'KA 01 AB 1234', capacity: 3, phone: '9876543210' },
{ id: 2, name: 'Jane Smith', type: 'auto', state: 'Karnataka', district: 'Bangalore Urban', area: 'Nagarbhavi', vehicle: 'KA 02 MG 5678', capacity: 3, phone: '9123456789' },
{
id: 3, name: 'City Express', type: 'bus', vehicle: 'KL 11 AV 1234', capacity: 40, phone: '9988776655',
routes: [
{ stop: 'Vazhakkad', time: '09:30 AM' },
{ stop: 'Kozhikode', time: '11:15 AM' }
]
}
];
await db.ref('drivers').set(drivers);
}
return Array.isArray(drivers) ? drivers : Object.values(drivers);
} catch (e) {
console.error("Firebase Error (getDrivers):", e);
return [];
}
};
const getUsers = async () => {
const snapshot = await db.ref('users').once('value');
const data = snapshot.val();
if (!data) return [];
return Array.isArray(data) ? data : Object.values(data);
};
const saveUser = async (user) => {
const users = await getUsers();
users.push(user);
await db.ref('users').set(users);
};
const getHistory = async () => {
const snapshot = await db.ref('history').once('value');
return snapshot.val() || [];
};
const saveHistory = async (history) => {
await db.ref('history').set(history);
};
// Search history still local for personal privacy/speed
const getSearchHistory = () => JSON.parse(localStorage.getItem('grami_search_history')) || [];
const saveSearchHistory = (state, district, area) => {
const history = getSearchHistory();
const newEntry = { state, district, area, date: new Date().toLocaleDateString() };
if (!history.find(h => h.area === area && h.district === district)) {
history.unshift(newEntry);
localStorage.setItem('grami_search_history', JSON.stringify(history.slice(0, 5)));
}
};
const getCart = () => JSON.parse(localStorage.getItem('grami_cart')) || [];
const saveCart = (cart) => localStorage.setItem('grami_cart', JSON.stringify(cart));
const clearCart = () => localStorage.removeItem('grami_cart');
const showToast = (message) => {
const container = document.getElementById('toast-container');
if (!container) return;
const toast = document.createElement('div');
toast.className = 'toast';
toast.innerHTML = `<i class="fas fa-info-circle"></i> ${message}`;
container.appendChild(toast);
setTimeout(() => toast.remove(), 3000);
};
// Exporting functions globally
window.LOCATION_DATA = LOCATION_DATA;
window.VENDORS_DATA = VENDORS_DATA;
window.PRODUCTS_DATA = PRODUCTS_DATA;
window.getDrivers = getDrivers;
window.getUsers = getUsers;
window.saveUser = saveUser;
window.getHistory = getHistory;
window.saveHistory = saveHistory;
window.getSearchHistory = getSearchHistory;
window.saveSearchHistory = saveSearchHistory;
window.getCart = getCart;
window.saveCart = saveCart;
window.clearCart = clearCart;
window.showToast = showToast;
window.storage = db; // Global reference if needed