Add mitra online/offline status with heartbeat-based auto-offline, customer-mitra pairing via Valkey pub/sub blast, session management, and control center dashboard with real-time stats. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
89 lines
2.9 KiB
Dart
89 lines
2.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../../core/api/api_client.dart';
|
|
|
|
class ActiveSessionsScreen extends StatefulWidget {
|
|
const ActiveSessionsScreen({super.key});
|
|
|
|
@override
|
|
State<ActiveSessionsScreen> createState() => _ActiveSessionsScreenState();
|
|
}
|
|
|
|
class _ActiveSessionsScreenState extends State<ActiveSessionsScreen> {
|
|
List<Map<String, dynamic>> _sessions = [];
|
|
bool _loading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadSessions();
|
|
}
|
|
|
|
Future<void> _loadSessions() async {
|
|
try {
|
|
final apiClient = context.read<ApiClient>();
|
|
final response = await apiClient.get('/api/mitra/chat-requests/sessions/active');
|
|
setState(() {
|
|
_sessions = List<Map<String, dynamic>>.from(response['data'] as List);
|
|
_loading = false;
|
|
});
|
|
} catch (_) {
|
|
setState(() => _loading = false);
|
|
}
|
|
}
|
|
|
|
Future<void> _endSession(String sessionId) async {
|
|
final confirmed = await showDialog<bool>(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text('Akhiri Sesi?'),
|
|
content: const Text('Apakah kamu yakin ingin mengakhiri sesi ini?'),
|
|
actions: [
|
|
TextButton(onPressed: () => Navigator.of(ctx).pop(false), child: const Text('Batal')),
|
|
TextButton(onPressed: () => Navigator.of(ctx).pop(true), child: const Text('Ya, Akhiri')),
|
|
],
|
|
),
|
|
);
|
|
|
|
if (confirmed == true) {
|
|
try {
|
|
final apiClient = context.read<ApiClient>();
|
|
await apiClient.post('/api/mitra/chat-requests/sessions/$sessionId/end');
|
|
_loadSessions();
|
|
} catch (_) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Gagal mengakhiri sesi.')),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Sesi Aktif')),
|
|
body: _loading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: _sessions.isEmpty
|
|
? const Center(child: Text('Tidak ada sesi aktif.'))
|
|
: ListView.builder(
|
|
itemCount: _sessions.length,
|
|
itemBuilder: (context, index) {
|
|
final session = _sessions[index];
|
|
return ListTile(
|
|
leading: const Icon(Icons.person),
|
|
title: Text(session['customer_display_name'] as String? ?? 'Customer'),
|
|
subtitle: Text('Status: ${session['status']}'),
|
|
trailing: TextButton(
|
|
onPressed: () => _endSession(session['id'] as String),
|
|
child: const Text('Akhiri', style: TextStyle(color: Colors.red)),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|