|
| 1 | +import 'package:cloud_functions/cloud_functions.dart'; |
| 2 | +import 'package:firebase_auth/firebase_auth.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 5 | +import 'package:ksrvnjord_main_app/src/features/profiles/almanak_profile/api/has_send_birthday_message_provider.dart'; |
| 6 | +import 'package:ksrvnjord_main_app/src/features/profiles/api/user_provider.dart'; |
| 7 | + |
| 8 | +class AlmanakBirthdayButton extends ConsumerWidget { |
| 9 | + const AlmanakBirthdayButton({ |
| 10 | + super.key, |
| 11 | + required this.receiverId, |
| 12 | + required this.receiverFullName, |
| 13 | + }); |
| 14 | + final String receiverId; |
| 15 | + final String receiverFullName; |
| 16 | + |
| 17 | + @override |
| 18 | + Widget build(BuildContext context, WidgetRef ref) { |
| 19 | + final hasCongratulatedLidAsyncValue = |
| 20 | + ref.watch(hasSendBirthdayMessageProvider(receiverId)); |
| 21 | + final userAsyncVal = ref.watch(currentUserProvider); |
| 22 | + return hasCongratulatedLidAsyncValue.when( |
| 23 | + data: (hasCongratulated) => Padding( |
| 24 | + padding: const EdgeInsets.only(left: 8.0), |
| 25 | + child: ElevatedButton( |
| 26 | + style: ElevatedButton.styleFrom( |
| 27 | + backgroundColor: hasCongratulated |
| 28 | + ? Colors.grey // Set background color to grey if not pressable |
| 29 | + : Colors |
| 30 | + .pinkAccent, // Set background color to pink if pressable |
| 31 | + ), |
| 32 | + onPressed: hasCongratulated |
| 33 | + ? null |
| 34 | + : () async { |
| 35 | + userAsyncVal.whenData((sender) => _showBirthdayMessageDialog( |
| 36 | + context, sender.identifierString, sender.fullName)); |
| 37 | + }, |
| 38 | + child: Row( |
| 39 | + mainAxisSize: MainAxisSize.min, |
| 40 | + children: [ |
| 41 | + const Icon( |
| 42 | + Icons.cake, // Add a birthday cake icon |
| 43 | + size: 20, |
| 44 | + color: Colors.white, |
| 45 | + ), |
| 46 | + const SizedBox(width: 8), // Add spacing between icon and text |
| 47 | + const Text( |
| 48 | + "Verstuur Felicitatie", |
| 49 | + style: TextStyle(fontSize: 14, color: Colors.white), |
| 50 | + ), |
| 51 | + ], |
| 52 | + ), |
| 53 | + ), |
| 54 | + ), |
| 55 | + loading: () => const CircularProgressIndicator(), |
| 56 | + error: (error, stack) => Text('Error: $error'), |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + void _showBirthdayMessageDialog( |
| 61 | + BuildContext context, String senderId, String senderFullName) async { |
| 62 | + final TextEditingController messageController = TextEditingController(); |
| 63 | + showDialog( |
| 64 | + context: context, |
| 65 | + builder: (BuildContext context) { |
| 66 | + return AlertDialog( |
| 67 | + title: const Text('Felicitatie versturen'), |
| 68 | + content: Column( |
| 69 | + mainAxisSize: MainAxisSize.min, |
| 70 | + children: [ |
| 71 | + const Text('Inhoud bericht:'), |
| 72 | + TextField( |
| 73 | + controller: messageController, |
| 74 | + decoration: const InputDecoration( |
| 75 | + hintText: 'Typ hier je bericht (max. 50 tekens)', |
| 76 | + ), |
| 77 | + maxLines: null, |
| 78 | + maxLength: 100, // Limit the input to 140 characters |
| 79 | + ), |
| 80 | + ], |
| 81 | + ), |
| 82 | + actions: [ |
| 83 | + TextButton( |
| 84 | + onPressed: () { |
| 85 | + // Call the cloud function personalBirthdayMessage |
| 86 | + Navigator.of(context).pop(); |
| 87 | + }, |
| 88 | + child: const Text('Annuleren'), |
| 89 | + ), |
| 90 | + TextButton( |
| 91 | + onPressed: () async { |
| 92 | + final message = messageController.text.trim(); |
| 93 | + final user = FirebaseAuth.instance.currentUser; |
| 94 | + |
| 95 | + if (user != null) { |
| 96 | + await _sendBirthdayMessage( |
| 97 | + context, |
| 98 | + message, |
| 99 | + senderId, |
| 100 | + senderFullName, |
| 101 | + user, |
| 102 | + ); |
| 103 | + // ignore: use_build_context_synchronously |
| 104 | + Navigator.of(context).pop(); // only after message sent |
| 105 | + } else { |
| 106 | + ScaffoldMessenger.of(context).showSnackBar( |
| 107 | + const SnackBar( |
| 108 | + content: Text( |
| 109 | + 'Je moet ingelogd zijn om een bericht te versturen.'), |
| 110 | + ), |
| 111 | + ); |
| 112 | + } |
| 113 | + }, |
| 114 | + child: const Text('Versturen'), |
| 115 | + ), |
| 116 | + ], |
| 117 | + ); |
| 118 | + }, |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + Future<void> _sendBirthdayMessage(BuildContext context, String message, |
| 123 | + String senderId, String senderFullName, User senderUser) async { |
| 124 | + if (message.isEmpty) { |
| 125 | + ScaffoldMessenger.of(context).showSnackBar( |
| 126 | + const SnackBar( |
| 127 | + content: |
| 128 | + Text('Geen notificatie gestuurd, bericht mag niet leeg zijn.')), |
| 129 | + ); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + try { |
| 134 | + await FirebaseAuth.instance.currentUser?.getIdToken(true); |
| 135 | + final result = await FirebaseFunctions.instanceFor(region: 'europe-west1') |
| 136 | + .httpsCallable('personalBirthdayMessage') |
| 137 | + .call({ |
| 138 | + 'receiverId': receiverId, |
| 139 | + 'receiverFullName': receiverFullName, |
| 140 | + 'senderId': senderId, |
| 141 | + 'senderFullName': senderFullName, |
| 142 | + 'message': message, |
| 143 | + }); |
| 144 | + |
| 145 | + if (result.data['success'] == true && context.mounted) { |
| 146 | + ScaffoldMessenger.of(context).showSnackBar( |
| 147 | + const SnackBar(content: Text('Bericht succesvol verstuurd!')), |
| 148 | + ); |
| 149 | + } else if (context.mounted) { |
| 150 | + ScaffoldMessenger.of(context).showSnackBar( |
| 151 | + const SnackBar( |
| 152 | + content: Text( |
| 153 | + 'Er is iets misgegaan bij het versturen. Probeer het later opnieuw.')), |
| 154 | + ); |
| 155 | + } |
| 156 | + } catch (e) { |
| 157 | + if (context.mounted) { |
| 158 | + ScaffoldMessenger.of(context).showSnackBar( |
| 159 | + SnackBar(content: Text('Fout bij versturen: $e')), |
| 160 | + ); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments