Insparkspokalen-ui/lib/teams/joinTeamPage.dart
2025-05-29 15:13:23 +02:00

114 lines
3.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:insparkspokalen_ui/models/teamModel.dart';
import 'package:insparkspokalen_ui/services/googleAuthService.dart';
import 'package:insparkspokalen_ui/services/backend/teamService.dart';
import 'package:insparkspokalen_ui/services/backend/userService.dart';
import 'package:insparkspokalen_ui/services/teamUserSyncService.dart';
class TeamCard extends StatelessWidget {
final TeamModel team;
final VoidCallback onJoin;
final TeamService teamService;
final UserService userService;
final TeamUserSyncService syncService;
const TeamCard({
super.key,
required this.team,
required this.onJoin,
required this.teamService,
required this.userService,
required this.syncService,
});
@override
Widget build(BuildContext context) {
return Card(
color: Colors.white,
margin: const EdgeInsets.symmetric(vertical: 6),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: const BorderSide(color: Colors.black12),
),
child: ListTile(
title: Text(team.name),
subtitle: Row(
children: [
const Text('', style: TextStyle(color: Colors.amber)),
Text(
'${team.userEmails.length}/20',
style: const TextStyle(
color: Colors.amber,
fontWeight: FontWeight.w600,
),
),
],
),
trailing: ElevatedButton(
onPressed: () async {
final isAuthenticated = await GoogleAuthService.ensureLoggedIn( // Probably remove this as flow protects unauthenticated users from accessing this page
context,
);
if (!isAuthenticated) return;
final shouldJoin = await showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("Bekräfta"),
content: Text(
"Vill du verkligen gå med i laget '${team.name}'?",
),
actions: [
TextButton(
child: const Text("Avbryt"),
onPressed: () => Navigator.of(context).pop(false),
),
TextButton(
child: const Text("Gå med"),
onPressed: () => Navigator.of(context).pop(true),
),
],
);
},
);
if (shouldJoin == false) return;
try {
if (await syncService.handleUserJoined(team.teamId)) {
onJoin();
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Det gick inte att gå med i laget."),
),
);
}
} catch (e, stacktrace) {
debugPrint("Join team error: $e");
debugPrint("Stacktrace: $stacktrace");
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Ett fel uppstod när du försökte gå med i laget.",
),
),
);
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[300],
foregroundColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
shadowColor: Colors.black45,
elevation: 4,
),
child: const Text('Join'),
),
),
);
}
}