Velar/Frontend/lib/email.dart

144 lines
4.4 KiB
Dart
Raw Normal View History

2025-08-15 01:27:26 -07:00
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
2025-08-19 20:25:41 -07:00
import 'package:monarch/other_pages/enviroment.dart';
2025-08-15 01:27:26 -07:00
class EmailsScreen extends StatefulWidget {
final String accessToken;
final String userId;
const EmailsScreen({
super.key,
required this.accessToken,
required this.userId,
});
@override
State<EmailsScreen> createState() => _EmailsScreenState();
}
class _EmailsScreenState extends State<EmailsScreen> {
List<dynamic> _emails = [];
bool _loading = true;
String? _error;
@override
void initState() {
super.initState();
fetchEmails();
}
Future<void> fetchEmails() async {
try {
2025-08-19 20:25:41 -07:00
final uri = Uri.parse("${Environment.baseUrl}/api/sync-gmail");
2025-08-15 01:27:26 -07:00
final res = await http.post(
uri,
headers: {"Content-Type": "application/json"},
2025-08-19 22:07:44 -07:00
body: jsonEncode({
"accessToken": widget.accessToken,
"userId": widget.userId, // Added userId here
}),
2025-08-15 01:27:26 -07:00
);
if (res.statusCode == 200) {
final data = jsonDecode(res.body);
setState(() {
_emails = data['emails'] ?? [];
_loading = false;
});
} else {
setState(() {
_error = "Backend error: ${res.body}";
_loading = false;
});
}
} catch (e) {
setState(() {
_error = "⚠ Error fetching emails: $e";
_loading = false;
});
}
}
2025-08-18 23:38:58 -07:00
2025-08-15 01:27:26 -07:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2025-08-18 23:38:58 -07:00
title: const Text("Bank Transactions"),
2025-08-15 01:27:26 -07:00
actions: [
IconButton(icon: const Icon(Icons.refresh), onPressed: fetchEmails),
],
),
body:
_loading
? const Center(child: CircularProgressIndicator())
: _error != null
? Center(child: Text(_error!))
: _emails.isEmpty
? const Center(child: Text("No bank-related emails found."))
: ListView.builder(
itemCount: _emails.length,
itemBuilder: (context, index) {
final email = _emails[index];
2025-08-18 23:38:58 -07:00
return Card(
margin: const EdgeInsets.symmetric(
vertical: 6,
horizontal: 12,
),
child: ListTile(
leading: Icon(
email['type'] == 'debit'
? Icons.arrow_upward
: Icons.arrow_downward,
color:
email['type'] == 'debit'
? Colors.red
: Colors.green,
),
title: Text(
2025-08-19 00:23:56 -07:00
"${(email['amount'] is num) ? (email['amount'] as num).toStringAsFixed(2) : email['amount'] ?? '---'}",
2025-08-18 23:38:58 -07:00
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2025-08-19 00:23:56 -07:00
Text(
(email['vendor'] ?? "Unknown Vendor")
.replaceAll(
RegExp(
r'[a-z0-9._%+-]+@[a-z0-9.-]+',
caseSensitive: false,
),
'',
)
.trim(),
),
2025-08-18 23:38:58 -07:00
Text(
"Date: ${email['date'] ?? 'N/A'}",
style: const TextStyle(fontSize: 12),
),
],
),
trailing: Text(
email['type']?.toUpperCase() ?? "",
style: TextStyle(
color:
email['type'] == 'debit'
? Colors.red
: Colors.green,
fontWeight: FontWeight.bold,
),
),
),
2025-08-15 01:27:26 -07:00
);
},
),
);
}
}