Add Tournament Creation page
This commit is contained in:
@@ -0,0 +1,169 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:frontend_splatournament_manager/providers/tournament_provider.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:intl/intl.dart';
|
||||||
|
|
||||||
|
class CreateTournamentPage extends StatefulWidget {
|
||||||
|
const CreateTournamentPage({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<CreateTournamentPage> createState() => _CreateTournamentPageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CreateTournamentPageState extends State<CreateTournamentPage> {
|
||||||
|
final _formKey = GlobalKey<FormState>();
|
||||||
|
final _nameController = TextEditingController();
|
||||||
|
final _descriptionController = TextEditingController();
|
||||||
|
final _maxTeamAmountController = TextEditingController();
|
||||||
|
|
||||||
|
DateTime? _startDate;
|
||||||
|
DateTime? _endDate;
|
||||||
|
|
||||||
|
bool _isLoading = false;
|
||||||
|
|
||||||
|
final DateFormat _dateFormat = DateFormat('yyyy-MM-dd');
|
||||||
|
|
||||||
|
Future<void> _selectDate(BuildContext context, bool isStart) async {
|
||||||
|
final initialDate = DateTime.now();
|
||||||
|
final picked = await showDatePicker(
|
||||||
|
context: context,
|
||||||
|
initialDate: initialDate,
|
||||||
|
firstDate: initialDate,
|
||||||
|
lastDate: DateTime(2101),
|
||||||
|
);
|
||||||
|
if (picked != null) {
|
||||||
|
setState(() {
|
||||||
|
if (isStart) {
|
||||||
|
_startDate = picked;
|
||||||
|
} else {
|
||||||
|
_endDate = picked;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _submitForm() async {
|
||||||
|
if (!_formKey.currentState!.validate()) return;
|
||||||
|
if (_startDate == null || _endDate == null) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('Please select both start and end dates.')),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_endDate!.isBefore(_startDate!)) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('End date cannot be before start date.')),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setState(() => _isLoading = true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
final provider = Provider.of<TournamentProvider>(context, listen: false);
|
||||||
|
await provider.createTournament(
|
||||||
|
_nameController.text,
|
||||||
|
_descriptionController.text,
|
||||||
|
int.parse(_maxTeamAmountController.text),
|
||||||
|
_startDate!,
|
||||||
|
_endDate!,
|
||||||
|
);
|
||||||
|
if (!context.mounted) return;
|
||||||
|
Navigator.pop(context);
|
||||||
|
} catch (e) {
|
||||||
|
if (!context.mounted) return;
|
||||||
|
ScaffoldMessenger.of(
|
||||||
|
context,
|
||||||
|
).showSnackBar(SnackBar(content: Text('Error: $e')));
|
||||||
|
} finally {
|
||||||
|
if (context.mounted) setState(() => _isLoading = false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_nameController.dispose();
|
||||||
|
_descriptionController.dispose();
|
||||||
|
_maxTeamAmountController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: const Text('Create Tournament')),
|
||||||
|
body: SingleChildScrollView(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: Form(
|
||||||
|
key: _formKey,
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
TextFormField(
|
||||||
|
controller: _nameController,
|
||||||
|
decoration: const InputDecoration(labelText: 'Name'),
|
||||||
|
validator: (value) =>
|
||||||
|
value == null || value.isEmpty ? 'Required' : null,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
TextFormField(
|
||||||
|
controller: _descriptionController,
|
||||||
|
decoration: const InputDecoration(labelText: 'Description'),
|
||||||
|
maxLines: 3,
|
||||||
|
validator: (value) =>
|
||||||
|
value == null || value.isEmpty ? 'Required' : null,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
TextFormField(
|
||||||
|
controller: _maxTeamAmountController,
|
||||||
|
decoration: const InputDecoration(labelText: 'Max Teams'),
|
||||||
|
keyboardType: TextInputType.number,
|
||||||
|
validator: (value) {
|
||||||
|
if (value == null || value.isEmpty) return 'Required';
|
||||||
|
if (int.tryParse(value) == null || int.parse(value) <= 0) {
|
||||||
|
return 'Must be a positive integer';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: OutlinedButton(
|
||||||
|
onPressed: () => _selectDate(context, true),
|
||||||
|
child: Text(
|
||||||
|
_startDate == null
|
||||||
|
? 'Select Start Date'
|
||||||
|
: 'Start: ${_dateFormat.format(_startDate!)}',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 16),
|
||||||
|
Expanded(
|
||||||
|
child: OutlinedButton(
|
||||||
|
onPressed: () => _selectDate(context, false),
|
||||||
|
child: Text(
|
||||||
|
_endDate == null
|
||||||
|
? 'Select End Date'
|
||||||
|
: 'End: ${_dateFormat.format(_endDate!)}',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
const SizedBox(height: 32),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: _isLoading ? null : _submitForm,
|
||||||
|
child: _isLoading
|
||||||
|
? const CircularProgressIndicator()
|
||||||
|
: const Text('Create Tournament'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:frontend_splatournament_manager/providers/tournament_provider.dart';
|
import 'package:frontend_splatournament_manager/providers/tournament_provider.dart';
|
||||||
import 'package:frontend_splatournament_manager/widgets/available_tournament_list.dart';
|
import 'package:frontend_splatournament_manager/widgets/available_tournament_list.dart';
|
||||||
|
import 'package:frontend_splatournament_manager/pages/create_tournament_page.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
@@ -15,8 +16,10 @@ class HomePage extends StatelessWidget {
|
|||||||
actions: [
|
actions: [
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
final tournamentProvider =
|
final tournamentProvider = Provider.of<TournamentProvider>(
|
||||||
Provider.of<TournamentProvider>(context, listen: false);
|
context,
|
||||||
|
listen: false,
|
||||||
|
);
|
||||||
try {
|
try {
|
||||||
await tournamentProvider.refreshAvailableTournaments();
|
await tournamentProvider.refreshAvailableTournaments();
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
@@ -43,6 +46,17 @@ class HomePage extends StatelessWidget {
|
|||||||
padding: EdgeInsets.fromLTRB(0, 12, 0, 24),
|
padding: EdgeInsets.fromLTRB(0, 12, 0, 24),
|
||||||
child: Column(children: [Spacer(), AvailableTournamentList()]),
|
child: Column(children: [Spacer(), AvailableTournamentList()]),
|
||||||
),
|
),
|
||||||
|
floatingActionButton: FloatingActionButton(
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => const CreateTournamentPage(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: const Icon(Icons.add),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,9 +62,9 @@ class _TournamentDetailPageState extends State<TournamentDetailPage> {
|
|||||||
onPressed: () {
|
onPressed: () {
|
||||||
//TODO: Backend Call
|
//TODO: Backend Call
|
||||||
ScaffoldMessenger.of(context).clearSnackBars();
|
ScaffoldMessenger.of(context).clearSnackBars();
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(
|
||||||
SnackBar(content: Text("tournament entered")),
|
context,
|
||||||
);
|
).showSnackBar(SnackBar(content: Text("tournament entered")));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -90,8 +90,10 @@ class _TournamentTeamsWidgetState extends State<TournamentTeamsWidget> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_teamsFuture = Provider.of<TeamProvider>(context, listen: false)
|
_teamsFuture = Provider.of<TeamProvider>(
|
||||||
.getTeamsByTournament(widget.tournament.id);
|
context,
|
||||||
|
listen: false,
|
||||||
|
).getTeamsByTournament(widget.tournament.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -132,20 +134,31 @@ class _TournamentTeamsWidgetState extends State<TournamentTeamsWidget> {
|
|||||||
? Text(team.description)
|
? Text(team.description)
|
||||||
: null,
|
: null,
|
||||||
trailing: IconButton(
|
trailing: IconButton(
|
||||||
icon: Icon(Icons.remove_circle_outline, color: Colors.red),
|
icon: Icon(
|
||||||
|
Icons.remove_circle_outline,
|
||||||
|
color: Colors.red,
|
||||||
|
),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
try {
|
try {
|
||||||
await Provider.of<TeamProvider>(context, listen: false)
|
await Provider.of<TeamProvider>(
|
||||||
.removeTeamFromTournament(widget.tournament.id, team.id);
|
context,
|
||||||
|
listen: false,
|
||||||
|
).removeTeamFromTournament(
|
||||||
|
widget.tournament.id,
|
||||||
|
team.id,
|
||||||
|
);
|
||||||
setState(() {
|
setState(() {
|
||||||
_teamsFuture =
|
_teamsFuture = Provider.of<TeamProvider>(
|
||||||
Provider.of<TeamProvider>(context, listen: false)
|
context,
|
||||||
.getTeamsByTournament(widget.tournament.id);
|
listen: false,
|
||||||
|
).getTeamsByTournament(widget.tournament.id);
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!context.mounted) return;
|
if (!context.mounted) return;
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(content: Text('Failed to remove team: $e')),
|
SnackBar(
|
||||||
|
content: Text('Failed to remove team: $e'),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -160,7 +173,6 @@ class _TournamentTeamsWidgetState extends State<TournamentTeamsWidget> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class TournamentContentWidget extends StatelessWidget {
|
class TournamentContentWidget extends StatelessWidget {
|
||||||
@@ -205,8 +217,7 @@ class TournamentContentWidget extends StatelessWidget {
|
|||||||
fontSize: 17,
|
fontSize: 17,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
//TODO: Should show the format instead
|
Text("Single Elimination"),
|
||||||
Text(tournament.description),
|
|
||||||
Spacer(),
|
Spacer(),
|
||||||
SizedBox(
|
SizedBox(
|
||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
|
|||||||
@@ -40,4 +40,35 @@ class TournamentProvider extends ChangeNotifier {
|
|||||||
_initialLoadFuture = fetchAvailableTournaments();
|
_initialLoadFuture = fetchAvailableTournaments();
|
||||||
return _initialLoadFuture!;
|
return _initialLoadFuture!;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Future<void> createTournament(
|
||||||
|
String name,
|
||||||
|
String description,
|
||||||
|
int maxTeamAmount,
|
||||||
|
DateTime registrationStartDate,
|
||||||
|
DateTime registrationEndDate,
|
||||||
|
) async {
|
||||||
|
final response = await http.post(
|
||||||
|
Uri.parse('$baseUrl/tournaments'),
|
||||||
|
headers: {'Content-Type': 'application/json'},
|
||||||
|
body: jsonEncode({
|
||||||
|
'name': name,
|
||||||
|
'description': description,
|
||||||
|
'maxTeamAmount': maxTeamAmount,
|
||||||
|
//weird date formatting
|
||||||
|
'registrationStartDate': registrationStartDate.toIso8601String().split(
|
||||||
|
'T',
|
||||||
|
)[0],
|
||||||
|
'registrationEndDate': registrationEndDate.toIso8601String().split(
|
||||||
|
'T',
|
||||||
|
)[0],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode != HttpStatus.created) {
|
||||||
|
throw Exception('Failed to create tournament (${response.statusCode})');
|
||||||
|
}
|
||||||
|
|
||||||
|
await refreshAvailableTournaments();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ dependencies:
|
|||||||
http: ^1.6.0
|
http: ^1.6.0
|
||||||
provider: ^6.1.5+1
|
provider: ^6.1.5+1
|
||||||
go_router: ^17.1.0
|
go_router: ^17.1.0
|
||||||
|
intl: ^0.20.2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user