Update categorie (id for filter and update)

This commit is contained in:
Thomas Fransolet 2024-04-11 17:05:57 +02:00
parent c2972b11ea
commit 785301d908
5 changed files with 132 additions and 15 deletions

View File

@ -27,7 +27,7 @@ class _DropDownInputContainerCategoriesState extends State<DropDownInputContaine
@override
void initState() {
if(widget.initialValue != null) {
selectedCategorieDTO = widget.categories.firstWhere((element) => element.order == widget.initialValue!.order);
selectedCategorieDTO = widget.categories.firstWhere((element) => element.id == widget.initialValue!.id);
}
List<TranslationDTO> label = [];
label.add(TranslationDTO(language: "FR", value: "Aucune catégorie"));

View File

@ -23,9 +23,12 @@ class CategoryInputContainer extends StatefulWidget {
}
class _CategoryInputContainerState extends State<CategoryInputContainer> {
late List<CategorieDTO> middleCategories;
@override
void initState() {
middleCategories = widget.initialValue;
print("initStateinitStateinitState");
super.initState();
}
@ -47,10 +50,18 @@ class _CategoryInputContainerState extends State<CategoryInputContainer> {
child: InkWell(
onTap: () {
List<CategorieDTO> newValues = <CategorieDTO>[];
List<CategorieDTO> initials = widget.initialValue;
showCreateOrUpdateCategories("Catégories", initials, newValues, (value) {
print("HERE COMES THE FUCKKKERS");
print(middleCategories);
showCreateOrUpdateCategories("Catégories", middleCategories, newValues, (value) {
print("RESULT HERERERER");
print(value);
setState(() {
widget.onChanged(value);
middleCategories = value;
widget.initialValue = value;
});
}, context);
},
child: Container(
@ -78,7 +89,7 @@ class _CategoryInputContainerState extends State<CategoryInputContainer> {
}
}
showCreateOrUpdateCategories(String modalLabel, List<CategorieDTO> values, List<CategorieDTO> newValues, Function onGetResult, BuildContext context) {
showCreateOrUpdateCategories(String modalLabel, List<CategorieDTO> initials, List<CategorieDTO> newValues, Function onGetResult, BuildContext context) {
showDialog(
builder: (BuildContext context) {
Size size = MediaQuery.of(context).size;
@ -95,9 +106,10 @@ showCreateOrUpdateCategories(String modalLabel, List<CategorieDTO> values, List<
),
height: size.height * 0.7,
width: size.width * 0.65,
child: CategoryList(categories: values, onChanged: (result) {
child: CategoryList(categories: initials, onChanged: (result) {
print("ON CHANGZED HERE");
newValues = result;
onGetResult(result);
//onGetResult(result);
}),
)
),
@ -113,7 +125,9 @@ showCreateOrUpdateCategories(String modalLabel, List<CategorieDTO> values, List<
icon: Icons.undo,
color: kSecond,
press: () {
onGetResult(values);
onGetResult(initials);
print("INITALASSS");
print(initials);
Navigator.of(context).pop();
},
fontSize: 20,
@ -132,6 +146,7 @@ showCreateOrUpdateCategories(String modalLabel, List<CategorieDTO> values, List<
if (!deepEq(values, newValues)) {
onGetResult(newValues);
}*/
onGetResult(newValues);
Navigator.of(context).pop();
},
fontSize: 20,

View File

@ -22,12 +22,47 @@ class CategoryList extends StatefulWidget {
class _CategoryListState extends State<CategoryList> {
late List<CategorieDTO> categoriesMiddle;
late int currentIndex;
@override
void initState() {
print("INISTASTETETTETET CATEGORY LIIST");
super.initState();
categoriesMiddle = new List<CategorieDTO>.from(widget.categories);
categoriesMiddle.sort((a, b) => a.order!.compareTo(b.order!));
currentIndex = getNextAvailableIndex(categoriesMiddle);
assignIdsToNull(categoriesMiddle);
print(currentIndex);
print(categoriesMiddle);
}
int getNextAvailableIndex(List<CategorieDTO> categoriesMiddle) {
List<int> existingIds = [];
for (var categorie in categoriesMiddle) {
if (categorie.id != null) {
existingIds.add(categorie.id!);
}
}
existingIds.sort();
if (existingIds.isEmpty) {
return 0;
}
return existingIds.last + 1;
}
void assignIdsToNull(List<CategorieDTO> categoriesMiddle) {
int nextIndex = getNextAvailableIndex(categoriesMiddle);
for (var categorie in categoriesMiddle) {
if (categorie.id == null) {
categorie.id = nextIndex;
nextIndex++;
}
}
}
void _resetOrder() {
@ -94,7 +129,7 @@ class _CategoryListState extends State<CategoryList> {
onTap: () async {
CategorieDTO newCategory = CategorieDTO(order: null);
var result = await showNewOrUpdateCategory(newCategory, appContext, context, "Création catégorie");
var result = await showNewOrUpdateCategory(newCategory, appContext, context, "Création catégorie", currentIndex);
if (result != null)
{
setState(() {
@ -168,7 +203,7 @@ class _CategoryListState extends State<CategoryList> {
message: "Modifier",
child: InkWell(
onTap: () async {
var result = await showNewOrUpdateCategory(category, appContext, context, "Modification catégorie");
var result = await showNewOrUpdateCategory(category, appContext, context, "Modification catégorie", currentIndex);
if (result != null)
{
setState(() {

View File

@ -1,7 +1,8 @@
import 'package:diacritic/diacritic.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:manager_app/Components/category_input_container.dart';
import 'package:manager_app/Components/multi_select_dropdown_language_container.dart';
import 'package:manager_app/Screens/Configurations/Section/SubSection/Map/category_input_container.dart';
import 'package:manager_app/Components/dropDown_input_container.dart';
import 'package:manager_app/Components/fetch_section_icon.dart';
import 'package:manager_app/Components/resource_input_container.dart';
@ -54,6 +55,24 @@ class _MapConfigState extends State<MapConfig> {
selectedCategories = mapDTO.categories!.map((categorie) => categorie.label!.firstWhere((element) => element.language == 'FR').value!).toList();
pointsToShow.forEach((pts) {
if(pts.categorieId == null && pts.categorie != null && mapDTO.categories!.any((e) => e.order == pts.categorie!.order)) {
print("SET CATEGORIE ID FOR CAT");
print(pts.categorie);
var testCat = mapDTO.categories!.where((c) => c.label![0].value == pts.categorie!.label![0].value);
if(testCat.isNotEmpty) {
pts.categorieId = testCat.first.id;
pts.categorie!.id = testCat.first.id;
} else {
pts.categorie = null; // Categorie no more existing so we can reset cat
}
print(pts.categorieId);
}
});
print(pointsToShow);
if(mapDTO.mapType != null) {
switch(mapDTO.mapType!.value) {
case 0:
@ -219,7 +238,43 @@ class _MapConfigState extends State<MapConfig> {
color: kPrimaryColor,
onChanged: (List<CategorieDTO>? value) {
if(value != null) {
//print("COUCOU CategoryInputContainerCategoryInputContainerCategoryInputContainer");
//print(value);
mapDTO.categories = value;
//update point's category
if(mapDTO.points != null) {
mapDTO.points!.forEach((p) {
// Check if category still exist - Delete
if(p.categorieId != null && !mapDTO.categories!.map((c) => c.id).any((e) => e != null && e == p.categorieId))
{
print("DELETE CAT REF");
print(p.categorieId);
print(p.categorie);
// delete cat ref from point
p.categorieId = null;
p.categorie = null;
}
// update point category - Update
if(p.categorieId != null && mapDTO.categories!.map((c) => c.id).any((e) => e != null && e == p.categorieId))
{
var categorie = mapDTO.categories!.firstWhere((c) => c.id == p.categorieId);
p.categorie = categorie;
print("UPDATE CAT REF");
}
print(p);
if(p.categorieId == null && p.categorie != null && p.categorie!.label != null)
{
// need to update with label
var categorie = mapDTO.categories!.firstWhere((c) => c.label![0].value == p.categorie!.label![0].value);
//p.categorie = categorie;
p.categorieId = categorie.id;
}
});
}
/*print("mapDTO.points");
print(mapDTO.points);*/
widget.onChanged(jsonEncode(mapDTO).toString());
}
},
@ -289,7 +344,8 @@ class _MapConfigState extends State<MapConfig> {
}
});
},
)),
)
),
Positioned(
top: 0,
right: 150,
@ -322,6 +378,13 @@ class _MapConfigState extends State<MapConfig> {
setState(() {
mapDTO.points!.add(geoPoint);
mapDTO.points!.sort((a, b) => a.title!.firstWhere((t) => t.language == 'FR').value!.toLowerCase().compareTo(b.title!.firstWhere((t) => t.language == 'FR').value!.toLowerCase()));
if(selectedCategories == null || selectedCategories!.length == 0) {
pointsToShow = mapDTO.points!.where((point) => point.categorie == null).toList();
} else {
pointsToShow = mapDTO.points!.where((point) => selectedCategories!.any((tps) => point.categorie == null || point.categorie?.label?.firstWhere((lab) => lab.language == 'FR').value == tps)).toList();
}
widget.onChanged(jsonEncode(mapDTO).toString());
});
},
@ -419,8 +482,12 @@ class _MapConfigState extends State<MapConfig> {
child: InkWell(
onTap: () {
setState(() {
mapDTO.points!.removeAt(index);
var pointToRemove = pointsToShow[index];
mapDTO.points = mapDTO.points!.where((p) => p.longitude != pointToRemove.longitude || p.latitude != p.latitude).toList();
pointsToShow.removeAt(index);
});
widget.onChanged(jsonEncode(mapDTO).toString());
},
child: Icon(

View File

@ -9,7 +9,7 @@ import 'package:manager_app/app_context.dart';
import 'package:manager_app/constants.dart';
import 'package:manager_api_new/api.dart';
Future<CategorieDTO?> showNewOrUpdateCategory(CategorieDTO? inputCategorieDTO, AppContext appContext, BuildContext context, String text) async {
Future<CategorieDTO?> showNewOrUpdateCategory(CategorieDTO? inputCategorieDTO, AppContext appContext, BuildContext context, String text, int currentIndex) async {
CategorieDTO categorieDTO = new CategorieDTO();
if (inputCategorieDTO != null) {