Files
Auto-GPT/frontend/lib/views/task_queue/leaderboard_submission_dialog.dart
hunteraraujo 105b80101e Update Leaderboard Submission Dialog to Pass Parameters to ViewModel
This commit updates the Leaderboard Submission Dialog to pass the team name, repository URL, and commit SHA as parameters to the submitToLeaderboard function in the SkillTreeViewModel. These changes ensure that the dialog and the ViewModel are aligned in terms of parameter requirements, facilitating efficient and accurate leaderboard submissions.
2023-09-27 15:33:11 -07:00

230 lines
7.3 KiB
Dart

import 'package:auto_gpt_flutter_client/constants/app_colors.dart';
import 'package:auto_gpt_flutter_client/utils/uri_utility.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LeaderboardSubmissionDialog extends StatefulWidget {
final Function(String, String, String)? onSubmit;
const LeaderboardSubmissionDialog({
Key? key,
this.onSubmit,
}) : super(key: key);
@override
_LeaderboardSubmissionDialogState createState() =>
_LeaderboardSubmissionDialogState();
}
class _LeaderboardSubmissionDialogState
extends State<LeaderboardSubmissionDialog> {
final TextEditingController _teamNameController = TextEditingController();
final TextEditingController _repoUrlController = TextEditingController();
final TextEditingController _commitShaController = TextEditingController();
String? _teamNameError;
String? _repoUrlError;
String? _commitShaError;
// TODO: Do we want this dialog to have the responsibiltiy of managing shared preferences?
SharedPreferences? _prefs;
@override
void initState() {
super.initState();
_initSharedPreferences();
}
Future<void> _initSharedPreferences() async {
_prefs = await SharedPreferences.getInstance();
setState(() {
_teamNameController.text = _prefs!.getString('teamName') ?? '';
_repoUrlController.text = _prefs!.getString('repoUrl') ?? '';
_commitShaController.text = _prefs!.getString('commitSha') ?? '';
});
}
void _validateAndSubmit() {
bool isValid = true;
if (_teamNameController.text.isEmpty) {
isValid = false;
_teamNameError = 'Team Name is required';
} else {
_teamNameError = null;
}
if (_repoUrlController.text.isEmpty) {
isValid = false;
_repoUrlError = 'Repo URL is required';
} else if (!UriUtility.isURL(_repoUrlController.text)) {
isValid = false;
_repoUrlError = 'Invalid URL format';
} else {
_repoUrlError = null;
}
if (_commitShaController.text.isEmpty) {
isValid = false;
_commitShaError = 'Commit SHA is required';
} else {
_commitShaError = null;
}
if (isValid) {
_saveToSharedPreferences();
widget.onSubmit?.call(_teamNameController.text, _repoUrlController.text,
_commitShaController.text);
} else {
setState(() {});
}
}
Future<void> _saveToSharedPreferences() async {
await _prefs!.setString('teamName', _teamNameController.text);
await _prefs!.setString('repoUrl', _repoUrlController.text);
await _prefs!.setString('commitSha', _commitShaController.text);
}
@override
Widget build(BuildContext context) {
final containerHeight = 324.0 +
(_teamNameError == null ? 0 : 22) +
(_repoUrlError == null ? 0 : 22) +
(_commitShaError == null ? 0 : 22);
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
child: Container(
width: 260,
height: containerHeight,
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Title
const Text(
'Leaderboard Submission',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontFamily: 'Archivo',
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 14),
// Team Name Field
const Text('Team Name'),
TextField(
controller: _teamNameController,
decoration: InputDecoration(
hintText: 'Keyboard Warriors',
errorText: _teamNameError,
border: OutlineInputBorder(
borderSide: BorderSide(
color: _teamNameError != null ? Colors.red : Colors.grey,
),
),
),
),
const SizedBox(height: 8),
// Github Repo URL Field
const Text('Github Repo URL'),
TextField(
controller: _repoUrlController,
decoration: InputDecoration(
hintText: 'https://github.com/KeyboardWarriors/BestAgentEver',
errorText: _repoUrlError,
border: OutlineInputBorder(
borderSide: BorderSide(
color: _repoUrlError != null ? Colors.red : Colors.grey,
),
),
),
),
const SizedBox(height: 8),
// Commit SHA Field
const Text('Commit SHA'),
TextField(
controller: _commitShaController,
decoration: InputDecoration(
hintText: '389131f2ab78c2cc5bdd2ec257be2d18b3a63da3',
errorText: _commitShaError,
border: OutlineInputBorder(
borderSide: BorderSide(
color: _commitShaError != null ? Colors.red : Colors.grey,
),
),
),
),
const SizedBox(height: 14),
// Buttons
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Cancel Button
SizedBox(
width: 106,
height: 28,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
onPressed: () => Navigator.of(context).pop(),
child: const Text(
'Cancel',
style: TextStyle(
color: Colors.white,
fontSize: 12.50,
fontFamily: 'Archivo',
fontWeight: FontWeight.w400,
),
),
),
),
SizedBox(width: 8),
// Submit Button
SizedBox(
width: 106,
height: 28,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.primaryLight,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
onPressed: _validateAndSubmit,
child: const Text(
'Submit',
style: TextStyle(
color: Colors.white,
fontSize: 12.50,
fontFamily: 'Archivo',
fontWeight: FontWeight.w400,
),
),
),
),
],
),
],
),
),
);
}
@override
void dispose() {
_teamNameController.dispose();
_repoUrlController.dispose();
_commitShaController.dispose();
super.dispose();
}
}