Commit c2c69eec authored by aleenaasghar's avatar aleenaasghar

feat: Implement user-specific addresses

parent 101cfa5c
......@@ -2,6 +2,7 @@ import 'package:uuid/uuid.dart';
class DeliveryAddress {
final String id;
final String userId;
final String streetAddress;
final String city;
final String state;
......@@ -13,6 +14,7 @@ class DeliveryAddress {
DeliveryAddress({
String? id,
required this.userId,
required this.streetAddress,
required this.city,
required this.state,
......@@ -30,6 +32,7 @@ class DeliveryAddress {
Map<String, dynamic> toJson() => {
'id': id,
'userId': userId,
'streetAddress': streetAddress,
'city': city,
'state': state,
......@@ -42,6 +45,7 @@ class DeliveryAddress {
factory DeliveryAddress.fromJson(Map<String, dynamic> json) => DeliveryAddress(
id: json['id'],
userId: json['userId'],
streetAddress: json['streetAddress'],
city: json['city'],
state: json['state'],
......@@ -53,6 +57,7 @@ class DeliveryAddress {
);
DeliveryAddress copyWith({
String? userId,
String? streetAddress,
String? city,
String? state,
......@@ -62,6 +67,7 @@ class DeliveryAddress {
String? notes,
}) => DeliveryAddress(
id: id,
userId: userId ?? this.userId,
streetAddress: streetAddress ?? this.streetAddress,
city: city ?? this.city,
state: state ?? this.state,
......
......@@ -40,16 +40,22 @@ class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
}
void _showAddEditAddressDialog({DeliveryAddress? address}) {
if (_user == null) return;
showDialog(
context: context,
builder: (context) => AddEditAddressDialog(
address: address,
onSave: (address) => _firestoreService.saveAddress(address),
userId: _user!.uid,
onSave: (address) {
_firestoreService.saveAddress(address);
},
),
);
}
Future<void> _showUploadCsvDialog() async {
if (_user == null) return;
try {
final result = await FilePicker.platform.pickFiles(
type: FileType.custom,
......@@ -72,6 +78,7 @@ class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
final addresses = list.map((row) {
try {
return DeliveryAddress(
userId: _user!.uid,
streetAddress: row[0].toString(),
city: row[1].toString(),
state: row[2].toString(),
......@@ -194,6 +201,9 @@ class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
}
Widget _buildLoggedInView(BuildContext context) {
if (_user == null) {
return const Center(child: CircularProgressIndicator());
}
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
......@@ -229,7 +239,7 @@ class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
child: AddressList(
onEdit: (address) => _showAddEditAddressDialog(address: address),
onDelete: _deleteAddress,
addressesStream: _firestoreService.getAddresses(),
addressesStream: _firestoreService.getAddresses(_user!.uid),
),
),
const SizedBox(height: 16),
......
......@@ -5,9 +5,13 @@ class FirestoreService {
final FirebaseFirestore _db = FirebaseFirestore.instance;
final String _collectionPath = 'addresses';
// Get a stream of all addresses
Stream<List<DeliveryAddress>> getAddresses() {
return _db.collection(_collectionPath).snapshots().map((snapshot) =>
// Get a stream of addresses for a specific user
Stream<List<DeliveryAddress>> getAddresses(String userId) {
return _db
.collection(_collectionPath)
.where('userId', isEqualTo: userId)
.snapshots()
.map((snapshot) =>
snapshot.docs.map((doc) => DeliveryAddress.fromJson(doc.data())).toList());
}
......
......@@ -3,9 +3,15 @@ import '../models/delivery_address.dart';
class AddEditAddressDialog extends StatefulWidget {
final DeliveryAddress? address;
final String userId;
final Function(DeliveryAddress) onSave;
const AddEditAddressDialog({super.key, this.address, required this.onSave});
const AddEditAddressDialog({
super.key,
this.address,
required this.userId,
required this.onSave,
});
@override
State<AddEditAddressDialog> createState() => _AddEditAddressDialogState();
......@@ -98,6 +104,7 @@ class _AddEditAddressDialogState extends State<AddEditAddressDialog> {
if (_formKey.currentState!.validate()) {
final address = DeliveryAddress(
id: widget.address?.id, // Keep original ID if editing
userId: widget.userId,
streetAddress: _streetController.text,
city: _cityController.text,
state: _stateController.text,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment