Skip to content

Commit

Permalink
Add button to call fix iCal calendars cloud function to admin console (
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsreichardt authored May 8, 2024
1 parent 96e7836 commit dc62d0e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
8 changes: 8 additions & 0 deletions console/lib/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> openPage(BuildContext context, Widget widget) {
return Navigator.push(
Expand Down Expand Up @@ -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());
},
),
],
),
);
Expand Down
107 changes: 107 additions & 0 deletions console/lib/pages/fix_ical_calendars.dart
Original file line number Diff line number Diff line change
@@ -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<FixICalCalendars> createState() => _FixICalCalendarsState();
}

class _FixICalCalendarsState extends State<FixICalCalendars> {
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<int> 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<Map<String, dynamic>>();
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),
),
);
}
}

0 comments on commit dc62d0e

Please sign in to comment.