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

Add Schools system #447

Merged
merged 8 commits into from
Jan 14, 2025
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
48 changes: 48 additions & 0 deletions lib/admin/class/school.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:myecl/admin/tools/function.dart';

class School {
School({
required this.name,
required this.id,
required this.emailRegex,
});
late final String name;
late final String id;
late final String emailRegex;

School.fromJson(Map<String, dynamic> json) {
name = getSchoolNameFromId(json['id'], json['name']);
id = json['id'];
emailRegex = json['email_regex'];
}

Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['name'] = name;
data['id'] = id;
data['email_regex'] = emailRegex;
return data;
}

School copyWith({
String? name,
String? id,
String? emailRegex,
}) =>
School(
name: name ?? this.name,
id: id ?? this.id,
emailRegex: emailRegex ?? this.emailRegex,
);

School.empty() {
name = 'Nom';
id = '';
emailRegex = '';
}

@override
String toString() {
return 'School(id: $id, name: $name, emailRegex: $emailRegex)';
}
}
13 changes: 13 additions & 0 deletions lib/admin/providers/school_id_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';

class SchoolIdNotifier extends StateNotifier<String> {
SchoolIdNotifier() : super("");

void setId(String id) {
state = id;
}
}

final schoolIdProvider = StateNotifierProvider<SchoolIdNotifier, String>(
(ref) => SchoolIdNotifier(),
);
59 changes: 59 additions & 0 deletions lib/admin/providers/school_list_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:myecl/admin/class/school.dart';
import 'package:myecl/admin/repositories/school_repository.dart';
import 'package:myecl/tools/providers/list_notifier.dart';
import 'package:myecl/tools/token_expire_wrapper.dart';

class SchoolListNotifier extends ListNotifier<School> {
final SchoolRepository schoolRepository;
SchoolListNotifier({required this.schoolRepository})
: super(const AsyncValue.loading());

Future<AsyncValue<List<School>>> loadSchools() async {
return await loadList(schoolRepository.getSchoolList);
}

Future<bool> createSchool(School school) async {
return await add(schoolRepository.createSchool, school);
}

Future<bool> updateSchool(School school) async {
return await update(
schoolRepository.updateSchool,
(schools, school) =>
schools..[schools.indexWhere((g) => g.id == school.id)] = school,
school,
);
}

Future<bool> deleteSchool(School school) async {
return await delete(
schoolRepository.deleteSchool,
(schools, school) => schools..removeWhere((i) => i.id == school.id),
school.id,
school,
);
}

void setSchool(School school) {
state.whenData(
(d) {
if (d.indexWhere((g) => g.id == school.id) == -1) return;
state = AsyncValue.data(
d..[d.indexWhere((g) => g.id == school.id)] = school,
);
},
);
}
}

final allSchoolListProvider =
StateNotifierProvider<SchoolListNotifier, AsyncValue<List<School>>>((ref) {
final schoolRepository = ref.watch(schoolRepositoryProvider);
SchoolListNotifier provider =
SchoolListNotifier(schoolRepository: schoolRepository);
tokenExpireWrapperAuth(ref, () async {
await provider.loadSchools();
});
return provider;
});
17 changes: 17 additions & 0 deletions lib/admin/providers/school_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:myecl/admin/class/school.dart';
import 'package:myecl/admin/repositories/school_repository.dart';

class SchoolNotifier extends StateNotifier<School> {
final SchoolRepository schoolRepository;
SchoolNotifier({required this.schoolRepository}) : super(School.empty());

void setSchool(School school) {
state = school;
}
}

final schoolProvider = StateNotifierProvider<SchoolNotifier, School>((ref) {
final schoolRepository = ref.watch(schoolRepositoryProvider);
return SchoolNotifier(schoolRepository: schoolRepository);
});
37 changes: 37 additions & 0 deletions lib/admin/repositories/school_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:myecl/admin/class/school.dart';
import 'package:myecl/auth/providers/openid_provider.dart';
import 'package:myecl/tools/repository/repository.dart';

class SchoolRepository extends Repository {
@override
// ignore: overridden_fields
final ext = "schools/";

Future<List<School>> getSchoolList() async {
return List<School>.from(
(await getList()).map((x) => School.fromJson(x)),
);
}

Future<School> getSchool(String schoolId) async {
return School.fromJson(await getOne(schoolId));
}

Future<bool> deleteSchool(String schoolId) async {
return await delete(schoolId);
}

Future<bool> updateSchool(School school) async {
return await update(school.toJson(), school.id);
}

Future<School> createSchool(School school) async {
return School.fromJson(await create(school.toJson()));
}
}

final schoolRepositoryProvider = Provider((ref) {
final token = ref.watch(tokenProvider);
return SchoolRepository()..setToken(token);
});
86 changes: 66 additions & 20 deletions lib/admin/router.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:myecl/admin/providers/is_admin_provider.dart';
import 'package:myecl/admin/ui/pages/add_association_page/add_association_page.dart'
deferred as add_association_page;
import 'package:myecl/admin/ui/pages/add_group_page/add_group_page.dart'
deferred as add_group_page;
import 'package:myecl/admin/ui/pages/add_loaner_page/add_loaner_page.dart'
deferred as add_loaner_page;
import 'package:myecl/admin/ui/pages/edit_module_visibility/edit_module_visibility.dart'
deferred as edit_module_visibility;
import 'package:myecl/admin/ui/pages/edit_page/edit_page.dart'
deferred as edit_page;
import 'package:myecl/admin/ui/pages/edit_group_page/edit_group_page.dart'
deferred as edit_group_page;
import 'package:myecl/admin/ui/pages/group_page/group_page.dart'
deferred as group_page;
import 'package:myecl/admin/ui/pages/school_page/school_page.dart'
deferred as school_page;
import 'package:myecl/admin/ui/pages/add_school_page/add_school_page.dart'
deferred as add_school_page;
import 'package:myecl/admin/ui/pages/edit_school_page/edit_school_page.dart'
deferred as edit_school_page;
import 'package:myecl/admin/ui/pages/main_page/main_page.dart'
deferred as main_page;
import 'package:myecl/tools/middlewares/admin_middleware.dart';
Expand All @@ -18,9 +26,13 @@ import 'package:qlevar_router/qlevar_router.dart';
class AdminRouter {
final Ref ref;
static const String root = '/admin';
static const String addAssociation = '/add_association';
static const String groups = '/groups';
static const String addGroup = '/add_group';
static const String editGroup = '/edit_group';
static const String addLoaner = '/add_loaner';
static const String editAssociation = '/edit_association';
static const String schools = '/schools';
static const String addSchool = '/add_school';
static const String editSchool = '/edit_school';
static const String editModuleVisibility = '/edit_module_visibility';
AdminRouter(this.ref);

Expand All @@ -35,31 +47,65 @@ class AdminRouter {
],
children: [
QRoute(
path: addAssociation,
builder: () => add_association_page.AddAssociationPage(),
path: groups,
builder: () => group_page.GroupsPage(),
middleware: [
DeferredLoadingMiddleware(add_association_page.loadLibrary),
DeferredLoadingMiddleware(group_page.loadLibrary),
],
),
QRoute(
path: addLoaner,
builder: () => add_loaner_page.AddLoanerPage(),
middleware: [
DeferredLoadingMiddleware(add_loaner_page.loadLibrary),
children: [
QRoute(
path: addGroup,
builder: () => add_group_page.AddGroupPage(),
middleware: [
DeferredLoadingMiddleware(add_group_page.loadLibrary),
],
),
QRoute(
path: editGroup,
builder: () => edit_group_page.EditGroupPage(),
middleware: [
DeferredLoadingMiddleware(edit_group_page.loadLibrary),
],
),
QRoute(
path: addLoaner,
builder: () => add_loaner_page.AddLoanerPage(),
middleware: [
DeferredLoadingMiddleware(add_loaner_page.loadLibrary),
],
),
],
),
QRoute(
path: editAssociation,
builder: () => edit_page.EditAssociationPage(),
middleware: [DeferredLoadingMiddleware(edit_page.loadLibrary)],
),
QRoute(
path: editModuleVisibility,
builder: () => edit_module_visibility.EditModulesVisibilityPage(),
middleware: [
DeferredLoadingMiddleware(edit_module_visibility.loadLibrary),
],
),
QRoute(
path: schools,
builder: () => school_page.SchoolsPage(),
middleware: [
DeferredLoadingMiddleware(school_page.loadLibrary),
],
children: [
QRoute(
path: addSchool,
builder: () => add_school_page.AddSchoolPage(),
middleware: [
DeferredLoadingMiddleware(add_school_page.loadLibrary),
],
),
QRoute(
path: editSchool,
builder: () => edit_school_page.EditSchoolPage(),
middleware: [
DeferredLoadingMiddleware(edit_school_page.loadLibrary),
],
),
],
),
],
);
}
41 changes: 27 additions & 14 deletions lib/admin/tools/constants.dart
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
class AdminTextConstants {
static const String accountTypes = "Types de compte";
static const String add = "Ajouter";
static const String addAssociation = "Ajouter une association";
static const String addedAssociation = "Association ajoutée";
static const String addGroup = "Ajouter un groupe";
static const String addedGroup = "Groupe créé";
static const String addedLoaner = "Préteur ajouté";
static const String addedMember = "Membre ajouté";
static const String addingError = "Erreur lors de l'ajout";
static const String addingMember = "Ajout d'un membre";
static const String addLoaningAssociation = "Ajouter une association de prêt";
static const String addLoaningGroup = "Ajouter un groupe de prêt";
static const String addSchool = "Ajouter une école";
static const String addedSchool = "École créée";
static const String administration = "Administration";
static const String association = "Association";
static const String group = "Groupe";
static const String groups = "Groupes";
static const String delete = "Supprimer";
static const String deleteAssociation = "Supprimer l'association ?";
static const String deletedAssociation = "Association supprimée";
static const String deleteGroup = "Supprimer le groupe ?";
static const String deletedGroup = "Groupe supprimé";
static const String deleteSchool = "Supprimer l'école ?";
static const String deletedSchool = "École supprimée";
static const String deleting = "Suppression";
static const String deletingError = "Erreur lors de la suppression";
static const String description = "Description";
static const String eclSchool = "Centrale Lyon";
static const String edit = "Modifier";
static const String emailRegex = "Email Regex";
static const String emptyFieldError = "Le nom ne peut pas être vide";
static const String error = "Erreur";
static const String loaningAssociation = "Association de prêt";
static const String loaningGroup = "Groupe de prêt";
static const String looking = "Recherche";
static const String members = "Membres";
static const String modifyModuleVisibility =
"Modifier la visibilité des modules";
static const String modifyModuleVisibility = "Visibilité des modules";
static const String name = "Nom";
static const String noMoreLoaner = "Aucun prêteur n'est disponible";
static const String removeAssociationMember =
"Supprimer le membre de l'association ?";
static const String updatedAssociation = "Association modifiée";
static const String noSchool = "Sans école";
static const String removeGroupMember = "Supprimer le membre du groupe ?";
static const String schools = "Écoles";
static const String updatedGroup = "Groupe modifié";
static const String updatingError = "Erreur lors de la modification";
static const String visibilities = "Visibilités";
}

static const String accountTypes = "Types de compte";
enum SchoolIdConstant {
noSchool("dce19aa2-8863-4c93-861e-fb7be8f610ed"),
eclSchool("d9772da7-1142-4002-8b86-b694b431dfed");

static const String groups = "Groupes";
const SchoolIdConstant(this.value);
final String value;
}
11 changes: 11 additions & 0 deletions lib/admin/tools/function.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:myecl/admin/tools/constants.dart';

String getSchoolNameFromId(String id, String name) {
if (id == SchoolIdConstant.noSchool.value) {
return AdminTextConstants.noSchool;
}
if (id == SchoolIdConstant.eclSchool.value) {
return AdminTextConstants.eclSchool;
}
return name;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/material.dart';

class CardUi extends StatelessWidget {
class ItemCardUi extends StatelessWidget {
final List<Widget> children;
const CardUi({super.key, required this.children});
const ItemCardUi({super.key, required this.children});

@override
Widget build(BuildContext context) {
Expand All @@ -21,7 +21,7 @@ class CardUi extends StatelessWidget {
],
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisAlignment: MainAxisAlignment.center,
children: children,
),
);
Expand Down
Loading
Loading