From dc62d0e01b2913ed95a123a8a1e0c7755ece3281 Mon Sep 17 00:00:00 2001 From: Nils Reichardt Date: Wed, 8 May 2024 19:55:45 +0200 Subject: [PATCH] Add button to call fix iCal calendars cloud function to admin console (#1625) ![image](https://github.com/SharezoneApp/sharezone-app/assets/24459435/f17a2b7b-35cd-4a89-9d1e-e4d361d10c1a) --- console/lib/home_page.dart | 8 ++ console/lib/pages/fix_ical_calendars.dart | 107 ++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 console/lib/pages/fix_ical_calendars.dart diff --git a/console/lib/home_page.dart b/console/lib/home_page.dart index e051c2a66..d764bacdb 100644 --- a/console/lib/home_page.dart +++ b/console/lib/home_page.dart @@ -10,6 +10,7 @@ import 'package:flutter/material.dart'; import 'package:sharezone_console/activation_codes/activation_codes_page.dart'; import 'package:sharezone_console/pages/change_type_of_user.dart'; import 'package:sharezone_console/pages/feedbacks/feedbacks_page.dart'; +import 'package:sharezone_console/pages/fix_ical_calendars.dart'; Future openPage(BuildContext context, Widget widget) { return Navigator.push( @@ -48,6 +49,13 @@ class HomePage extends StatelessWidget { openPage(context, FeedbacksPage()); }, ), + ListTile( + leading: Icon(Icons.calendar_today), + title: const Text("Fix iCal Calendars"), + onTap: () { + openPage(context, FixICalCalendars()); + }, + ), ], ), ); diff --git a/console/lib/pages/fix_ical_calendars.dart b/console/lib/pages/fix_ical_calendars.dart new file mode 100644 index 000000000..2c0ebaf60 --- /dev/null +++ b/console/lib/pages/fix_ical_calendars.dart @@ -0,0 +1,107 @@ +// Copyright (c) 2024 Sharezone UG (haftungsbeschränkt) +// Licensed under the EUPL-1.2-or-later. +// +// You may obtain a copy of the Licence at: +// https://joinup.ec.europa.eu/software/page/eupl +// +// SPDX-License-Identifier: EUPL-1.2 + +import 'package:cloud_functions/cloud_functions.dart'; +import 'package:flutter/material.dart'; + +class FixICalCalendars extends StatefulWidget { + const FixICalCalendars({super.key}); + + @override + State createState() => _FixICalCalendarsState(); +} + +class _FixICalCalendarsState extends State { + int? amountOfFixedCalendars; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text("Fix iCal Calendars"), + ), + body: Center( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + SizedBox( + width: 300, + child: Text( + 'A cloud function that can be used to check if some iCal calendars are not in sync with the database. If they are not in sync, they will be fixed.', + textAlign: TextAlign.center, + ), + ), + const SizedBox(height: 12), + _Button( + (int amount) { + setState(() { + amountOfFixedCalendars = amount; + }); + }, + ), + if (amountOfFixedCalendars != null) + Padding( + padding: const EdgeInsets.only(top: 8), + child: Text("Fixed $amountOfFixedCalendars calendars"), + ), + ], + ), + ), + ); + } +} + +class _Button extends StatefulWidget { + const _Button(this.amountOfFixedCalendars); + + final ValueChanged amountOfFixedCalendars; + + @override + State<_Button> createState() => _ButtonState(); +} + +class _ButtonState extends State<_Button> { + bool isLoading = false; + + @override + Widget build(BuildContext context) { + return ElevatedButton( + onPressed: isLoading + ? null + : () async { + setState(() { + isLoading = true; + }); + + try { + final cfs = + FirebaseFunctions.instanceFor(region: 'europe-west1'); + final function = cfs.httpsCallable('fixICalCalendars'); + + final result = await function.call>(); + widget.amountOfFixedCalendars( + result.data['amountOfFixedCalendars']); + } catch (e) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text('Error: $e'), + ), + ); + } finally { + setState(() { + isLoading = false; + }); + } + }, + child: Text( + isLoading ? "Loading..." : "Fix", + style: TextStyle(color: Colors.white), + ), + ); + } +}