feat: add an accept button in view invitations page
This commit is contained in:
parent
c9316b5190
commit
80a280a4aa
@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import '../../models/invitation_models.dart';
|
import '../../models/invitation_models.dart';
|
||||||
import '../../services/invitations_service.dart';
|
import '../../services/invitations_service.dart';
|
||||||
|
import '../../services/user_service.dart';
|
||||||
import '../../utils/invitation_utils.dart';
|
import '../../utils/invitation_utils.dart';
|
||||||
|
|
||||||
class InvitationDetailsPage extends StatefulWidget {
|
class InvitationDetailsPage extends StatefulWidget {
|
||||||
@ -23,11 +24,14 @@ class _InvitationDetailsPageState extends State<InvitationDetailsPage> {
|
|||||||
InvitationDetails? _invitationDetails;
|
InvitationDetails? _invitationDetails;
|
||||||
bool _isLoading = true;
|
bool _isLoading = true;
|
||||||
bool _isCancelling = false;
|
bool _isCancelling = false;
|
||||||
|
bool _isAccepting = false;
|
||||||
|
bool _isCurrentlyParticipant = false;
|
||||||
String? _errorMessage;
|
String? _errorMessage;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
_isCurrentlyParticipant = widget.isParticipant;
|
||||||
_loadInvitationDetails();
|
_loadInvitationDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +52,11 @@ class _InvitationDetailsPageState extends State<InvitationDetailsPage> {
|
|||||||
_errorMessage = result['message'];
|
_errorMessage = result['message'];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Update participation status after loading details
|
||||||
|
if (result['success']) {
|
||||||
|
await _updateParticipationStatus();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,6 +159,52 @@ class _InvitationDetailsPageState extends State<InvitationDetailsPage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _updateParticipationStatus() async {
|
||||||
|
if (_invitationDetails == null) return;
|
||||||
|
|
||||||
|
final userResult = await UserService.getCurrentUser();
|
||||||
|
if (userResult['success'] && userResult['data'] != null) {
|
||||||
|
final currentUserId = userResult['data']['id'];
|
||||||
|
final isParticipant = _invitationDetails!.attendees.any((attendee) => attendee.id == currentUserId);
|
||||||
|
setState(() {
|
||||||
|
_isCurrentlyParticipant = isParticipant;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _acceptInvitation() async {
|
||||||
|
setState(() {
|
||||||
|
_isAccepting = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
final result = await InvitationsService.acceptInvitation(widget.invitationId);
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {
|
||||||
|
_isAccepting = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result['success']) {
|
||||||
|
// Reload invitation details to reflect the new state
|
||||||
|
await _loadInvitationDetails();
|
||||||
|
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text('Invitation accepted successfully!'),
|
||||||
|
backgroundColor: Colors.green,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(result['message'] ?? 'Failed to accept invitation'),
|
||||||
|
backgroundColor: Colors.red,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return PopScope(
|
return PopScope(
|
||||||
@ -509,7 +564,45 @@ class _InvitationDetailsPageState extends State<InvitationDetailsPage> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
if (widget.isParticipant) ...[
|
|
||||||
|
// Accept button for non-participants who are not owners
|
||||||
|
if (!_isCurrentlyParticipant && !widget.isOwner) ...[
|
||||||
|
SizedBox(height: 32),
|
||||||
|
Container(
|
||||||
|
width: double.infinity,
|
||||||
|
height: 56,
|
||||||
|
child: ElevatedButton(
|
||||||
|
onPressed: _isAccepting ? null : _acceptInvitation,
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: Color(0xFF6A4C93),
|
||||||
|
foregroundColor: Colors.white,
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
),
|
||||||
|
elevation: 2,
|
||||||
|
),
|
||||||
|
child: _isAccepting
|
||||||
|
? SizedBox(
|
||||||
|
height: 20,
|
||||||
|
width: 20,
|
||||||
|
child: CircularProgressIndicator(
|
||||||
|
strokeWidth: 2,
|
||||||
|
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: Text(
|
||||||
|
'Accept Invitation',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 16,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
|
||||||
|
// Cancel/Leave button for participants
|
||||||
|
if (_isCurrentlyParticipant) ...[
|
||||||
SizedBox(height: 32),
|
SizedBox(height: 32),
|
||||||
Container(
|
Container(
|
||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
|
|||||||
@ -49,7 +49,9 @@ class _InvitationsPageState extends State<InvitationsPage>
|
|||||||
_isLoading = false;
|
_isLoading = false;
|
||||||
_errorMessage = null;
|
_errorMessage = null;
|
||||||
});
|
});
|
||||||
print('Invitations UI updated with ${updatedInvitationsData.created.length + updatedInvitationsData.accepted.length + updatedInvitationsData.available.length} total invitations');
|
print(
|
||||||
|
'Invitations UI updated with ${updatedInvitationsData.created.length + updatedInvitationsData.accepted.length + updatedInvitationsData.available.length} total invitations',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onError: (error) {
|
onError: (error) {
|
||||||
@ -69,7 +71,9 @@ class _InvitationsPageState extends State<InvitationsPage>
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
final result = await InvitationsService.getAllInvitations(forceRefresh: forceRefresh);
|
final result = await InvitationsService.getAllInvitations(
|
||||||
|
forceRefresh: forceRefresh,
|
||||||
|
);
|
||||||
|
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
setState(() {
|
setState(() {
|
||||||
@ -551,7 +555,10 @@ class _InvitationsPageState extends State<InvitationsPage>
|
|||||||
),
|
),
|
||||||
automaticallyImplyLeading: false,
|
automaticallyImplyLeading: false,
|
||||||
actions: [
|
actions: [
|
||||||
IconButton(icon: Icon(Icons.refresh), onPressed: () => _loadInvitations(forceRefresh: true)),
|
IconButton(
|
||||||
|
icon: Icon(Icons.refresh),
|
||||||
|
onPressed: () => _loadInvitations(forceRefresh: true),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
body: _isLoading
|
body: _isLoading
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user