Commit c2c69eec authored by aleenaasghar's avatar aleenaasghar

feat: Implement user-specific addresses

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