Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redundant click prevention bug #656

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CheckmarkTaskWidget extends StatefulWidget {
}

class _CheckmarkTaskWidgetState extends State<CheckmarkTaskWidget> {
DateTime _lastClickTime = DateTime.now();
DateTime? _lastClickTime;
bool _isLoading = false;

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class QuestionnaireTaskWidget extends StatefulWidget {
class _QuestionnaireTaskWidgetState extends State<QuestionnaireTaskWidget> {
dynamic response;
late bool responseValidator;
DateTime _lastClickTime = DateTime.now();
DateTime? _lastClickTime;
bool _isLoading = false;
final GlobalKey<FormState> formKey = GlobalKey<FormState>();

Expand Down Expand Up @@ -86,12 +86,8 @@ class _QuestionnaireTaskWidgetState extends State<QuestionnaireTaskWidget> {
backgroundColor: WidgetStateProperty.all<Color>(Colors.green),
),
onPressed: () async {
if (isRedundantClick(_lastClickTime)) {
return;
}
if (!formKey.currentState!.validate()) {
return;
}
if (isRedundantClick(_lastClickTime)) return;
if (!formKey.currentState!.validate()) return;
setState(() {
_isLoading = true;
_lastClickTime = DateTime.now();
Expand Down
5 changes: 3 additions & 2 deletions app/lib/util/misc.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
bool isRedundantClick(
DateTime lastClickTime, {
Duration interval = const Duration(seconds: 2),
DateTime? lastClickTime, {
Duration interval = const Duration(seconds: 1),
}) {
if (lastClickTime == null) return false;
final now = DateTime.now();
if (now.difference(lastClickTime) > interval) {
return false;
Expand Down
Loading