Added Delete button with permission control
This commit is contained in:
parent
befbead7ff
commit
072bc34cde
@ -3,11 +3,14 @@ import 'package:marco/helpers/services/app_logger.dart';
|
|||||||
import 'package:marco/helpers/services/api_service.dart';
|
import 'package:marco/helpers/services/api_service.dart';
|
||||||
import 'package:marco/model/employee_model.dart';
|
import 'package:marco/model/employee_model.dart';
|
||||||
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
||||||
|
import 'package:marco/controller/directory/directory_controller.dart';
|
||||||
|
|
||||||
class ManageBucketController extends GetxController {
|
class ManageBucketController extends GetxController {
|
||||||
RxList<EmployeeModel> allEmployees = <EmployeeModel>[].obs;
|
RxList<EmployeeModel> allEmployees = <EmployeeModel>[].obs;
|
||||||
RxBool isLoading = false.obs;
|
RxBool isLoading = false.obs;
|
||||||
|
|
||||||
|
final DirectoryController directoryController = Get.find();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onInit() {
|
void onInit() {
|
||||||
super.onInit();
|
super.onInit();
|
||||||
@ -42,7 +45,6 @@ class ManageBucketController extends GetxController {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build payload: mark new ones active, removed ones inactive
|
|
||||||
final allInvolvedIds = {...originalEmployeeIds, ...employeeIds}.toList();
|
final allInvolvedIds = {...originalEmployeeIds, ...employeeIds}.toList();
|
||||||
|
|
||||||
final assignPayload = allInvolvedIds.map((empId) {
|
final assignPayload = allInvolvedIds.map((empId) {
|
||||||
@ -93,8 +95,8 @@ class ManageBucketController extends GetxController {
|
|||||||
try {
|
try {
|
||||||
final response = await ApiService.getAllEmployees();
|
final response = await ApiService.getAllEmployees();
|
||||||
if (response != null && response.isNotEmpty) {
|
if (response != null && response.isNotEmpty) {
|
||||||
allEmployees
|
allEmployees.assignAll(
|
||||||
.assignAll(response.map((json) => EmployeeModel.fromJson(json)));
|
response.map((json) => EmployeeModel.fromJson(json)));
|
||||||
logSafe(
|
logSafe(
|
||||||
"All Employees fetched for Manage Bucket: ${allEmployees.length}",
|
"All Employees fetched for Manage Bucket: ${allEmployees.length}",
|
||||||
level: LogLevel.info,
|
level: LogLevel.info,
|
||||||
@ -113,4 +115,38 @@ class ManageBucketController extends GetxController {
|
|||||||
isLoading.value = false;
|
isLoading.value = false;
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> deleteBucket(String bucketId) async {
|
||||||
|
isLoading.value = true;
|
||||||
|
update();
|
||||||
|
|
||||||
|
try {
|
||||||
|
final deleted = await ApiService.deleteBucket(bucketId);
|
||||||
|
if (deleted) {
|
||||||
|
directoryController.contactBuckets.removeWhere((b) => b.id == bucketId);
|
||||||
|
showAppSnackbar(
|
||||||
|
title: "Deleted",
|
||||||
|
message: "Bucket deleted successfully.",
|
||||||
|
type: SnackbarType.success,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
showAppSnackbar(
|
||||||
|
title: "Delete Failed",
|
||||||
|
message: "Unable to delete bucket.",
|
||||||
|
type: SnackbarType.error,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e, stack) {
|
||||||
|
logSafe("Error deleting bucket: $e", level: LogLevel.error);
|
||||||
|
logSafe("Stack: $stack", level: LogLevel.debug);
|
||||||
|
showAppSnackbar(
|
||||||
|
title: "Unexpected Error",
|
||||||
|
message: "Failed to delete bucket.",
|
||||||
|
type: SnackbarType.error,
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
isLoading.value = false;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -246,9 +246,45 @@ class ApiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Directory calling the API
|
/// Directory calling the API
|
||||||
|
|
||||||
|
static Future<bool> deleteBucket(String id) async {
|
||||||
|
final endpoint = "${ApiEndpoints.updateBucket}/$id";
|
||||||
|
|
||||||
|
try {
|
||||||
|
final token = await _getToken();
|
||||||
|
if (token == null) {
|
||||||
|
logSafe("Token is null. Cannot proceed with DELETE request.",
|
||||||
|
level: LogLevel.error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final uri = Uri.parse("${ApiEndpoints.baseUrl}$endpoint");
|
||||||
|
|
||||||
|
logSafe("Sending DELETE request to $uri", level: LogLevel.debug);
|
||||||
|
|
||||||
|
final response =
|
||||||
|
await http.delete(uri, headers: _headers(token)).timeout(timeout);
|
||||||
|
|
||||||
|
logSafe("DELETE bucket response status: ${response.statusCode}");
|
||||||
|
logSafe("DELETE bucket response body: ${response.body}");
|
||||||
|
|
||||||
|
final json = jsonDecode(response.body);
|
||||||
|
if (response.statusCode == 200 && json['success'] == true) {
|
||||||
|
logSafe("Bucket deleted successfully.");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
logSafe(
|
||||||
|
"Failed to delete bucket: ${json['message'] ?? 'Unknown error'}",
|
||||||
|
level: LogLevel.warning);
|
||||||
|
}
|
||||||
|
} catch (e, stack) {
|
||||||
|
logSafe("Exception during deleteBucket API: $e", level: LogLevel.error);
|
||||||
|
logSafe("StackTrace: $stack", level: LogLevel.debug);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// Update an existing bucket by ID
|
|
||||||
static Future<bool> updateBucket({
|
static Future<bool> updateBucket({
|
||||||
required String id,
|
required String id,
|
||||||
required String name,
|
required String name,
|
||||||
|
@ -94,156 +94,237 @@ class _ManageBucketsScreenState extends State<ManageBucketsScreen> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
body: Column(
|
body: SafeArea(
|
||||||
children: [
|
child: Column(
|
||||||
Padding(
|
children: [
|
||||||
padding: MySpacing.xy(16, 12),
|
Padding(
|
||||||
child: SizedBox(
|
padding: MySpacing.xy(16, 12),
|
||||||
height: 38,
|
child: SizedBox(
|
||||||
child: TextField(
|
height: 38,
|
||||||
controller: searchController,
|
child: TextField(
|
||||||
onChanged: (value) {
|
controller: searchController,
|
||||||
setState(() {
|
onChanged: (value) {
|
||||||
searchText = value.toLowerCase();
|
setState(() {
|
||||||
});
|
searchText = value.toLowerCase();
|
||||||
},
|
});
|
||||||
decoration: InputDecoration(
|
},
|
||||||
contentPadding:
|
decoration: InputDecoration(
|
||||||
const EdgeInsets.symmetric(horizontal: 12, vertical: 0),
|
contentPadding:
|
||||||
prefixIcon:
|
const EdgeInsets.symmetric(horizontal: 12, vertical: 0),
|
||||||
const Icon(Icons.search, size: 18, color: Colors.grey),
|
prefixIcon:
|
||||||
suffixIcon: searchText.isNotEmpty
|
const Icon(Icons.search, size: 18, color: Colors.grey),
|
||||||
? IconButton(
|
suffixIcon: searchText.isNotEmpty
|
||||||
icon: const Icon(Icons.close, color: Colors.grey),
|
? IconButton(
|
||||||
onPressed: _clearSearch,
|
icon: const Icon(Icons.close, color: Colors.grey),
|
||||||
)
|
onPressed: _clearSearch,
|
||||||
: null,
|
)
|
||||||
hintText: 'Search buckets...',
|
: null,
|
||||||
filled: true,
|
hintText: 'Search buckets...',
|
||||||
fillColor: Colors.white,
|
filled: true,
|
||||||
border: OutlineInputBorder(
|
fillColor: Colors.white,
|
||||||
borderRadius: BorderRadius.circular(10),
|
border: OutlineInputBorder(
|
||||||
borderSide: BorderSide(color: Colors.grey.shade300),
|
borderRadius: BorderRadius.circular(10),
|
||||||
),
|
borderSide: BorderSide(color: Colors.grey.shade300),
|
||||||
enabledBorder: OutlineInputBorder(
|
),
|
||||||
borderRadius: BorderRadius.circular(10),
|
enabledBorder: OutlineInputBorder(
|
||||||
borderSide: BorderSide(color: Colors.grey.shade300),
|
borderRadius: BorderRadius.circular(10),
|
||||||
|
borderSide: BorderSide(color: Colors.grey.shade300),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
Expanded(
|
||||||
Expanded(
|
child: Obx(() {
|
||||||
child: Obx(() {
|
final buckets =
|
||||||
final buckets =
|
directoryController.contactBuckets.where((bucket) {
|
||||||
directoryController.contactBuckets.where((bucket) {
|
return bucket.name.toLowerCase().contains(searchText) ||
|
||||||
return bucket.name.toLowerCase().contains(searchText) ||
|
bucket.description.toLowerCase().contains(searchText) ||
|
||||||
bucket.description.toLowerCase().contains(searchText) ||
|
bucket.numberOfContacts.toString().contains(searchText);
|
||||||
bucket.numberOfContacts.toString().contains(searchText);
|
}).toList();
|
||||||
}).toList();
|
|
||||||
|
|
||||||
if (directoryController.isLoading.value ||
|
if (directoryController.isLoading.value ||
|
||||||
manageBucketController.isLoading.value) {
|
manageBucketController.isLoading.value) {
|
||||||
return const Center(child: CircularProgressIndicator());
|
return const Center(child: CircularProgressIndicator());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buckets.isEmpty) {
|
if (buckets.isEmpty) {
|
||||||
return Center(
|
return Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
|
||||||
const Icon(Icons.folder_off,
|
|
||||||
size: 48, color: Colors.grey),
|
|
||||||
MySpacing.height(12),
|
|
||||||
MyText.bodyMedium("No buckets available.",
|
|
||||||
fontWeight: 600, color: Colors.grey[700]),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ListView.separated(
|
|
||||||
padding: MySpacing.fromLTRB(16, 0, 16, 24),
|
|
||||||
itemCount: buckets.length,
|
|
||||||
separatorBuilder: (_, __) => MySpacing.height(16),
|
|
||||||
itemBuilder: (context, index) {
|
|
||||||
final bucket = buckets[index];
|
|
||||||
final isOwner = currentUserId != null &&
|
|
||||||
bucket.createdBy.id == currentUserId;
|
|
||||||
final canEdit = isOwner ||
|
|
||||||
widget.permissionController
|
|
||||||
.hasPermission(Permissions.directoryAdmin) ||
|
|
||||||
widget.permissionController
|
|
||||||
.hasPermission(Permissions.directoryManager);
|
|
||||||
final isExpanded = _expandedMap[bucket.id] ?? false;
|
|
||||||
|
|
||||||
final matchedEmployees = manageBucketController.allEmployees
|
|
||||||
.where((emp) => bucket.employeeIds.contains(emp.id))
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
return GestureDetector(
|
|
||||||
onTap: () {
|
|
||||||
TeamMembersBottomSheet.show(
|
|
||||||
context,
|
|
||||||
bucket,
|
|
||||||
matchedEmployees,
|
|
||||||
canEdit: canEdit,
|
|
||||||
onEdit: () {
|
|
||||||
Get.back(); // Close the bottom sheet first
|
|
||||||
Future.delayed(const Duration(milliseconds: 300), () {
|
|
||||||
EditBucketBottomSheet.show(context, bucket, manageBucketController.allEmployees);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
|
||||||
child: Row(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
children: [
|
||||||
const Padding(
|
const Icon(Icons.folder_off,
|
||||||
padding: EdgeInsets.only(top: 4),
|
size: 48, color: Colors.grey),
|
||||||
child: Icon(Icons.label_outline,
|
MySpacing.height(12),
|
||||||
size: 26, color: Colors.indigo),
|
MyText.bodyMedium("No buckets available.",
|
||||||
),
|
fontWeight: 600, color: Colors.grey[700]),
|
||||||
MySpacing.width(12),
|
],
|
||||||
Expanded(
|
),
|
||||||
child: Column(
|
);
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
}
|
||||||
children: [
|
|
||||||
MyText.titleSmall(
|
return ListView.separated(
|
||||||
bucket.name,
|
padding: MySpacing.fromLTRB(16, 0, 16, 24),
|
||||||
fontWeight: 700,
|
itemCount: buckets.length,
|
||||||
overflow: TextOverflow.ellipsis,
|
separatorBuilder: (_, __) => MySpacing.height(16),
|
||||||
),
|
itemBuilder: (context, index) {
|
||||||
MySpacing.height(4),
|
final bucket = buckets[index];
|
||||||
Row(
|
final isOwner = currentUserId != null &&
|
||||||
children: [
|
bucket.createdBy.id == currentUserId;
|
||||||
const Icon(Icons.contacts_outlined,
|
final canEdit = isOwner ||
|
||||||
size: 14, color: Colors.grey),
|
widget.permissionController
|
||||||
MySpacing.width(4),
|
.hasPermission(Permissions.directoryAdmin) ||
|
||||||
MyText.labelSmall(
|
widget.permissionController
|
||||||
'${bucket.numberOfContacts} contact(s)',
|
.hasPermission(Permissions.directoryManager);
|
||||||
color: Colors.red,
|
final isExpanded = _expandedMap[bucket.id] ?? false;
|
||||||
fontWeight: 600,
|
|
||||||
),
|
final matchedEmployees = manageBucketController.allEmployees
|
||||||
MySpacing.width(12),
|
.where((emp) => bucket.employeeIds.contains(emp.id))
|
||||||
Row(
|
.toList();
|
||||||
children: [
|
|
||||||
const Icon(Icons.ios_share_outlined,
|
return GestureDetector(
|
||||||
size: 14, color: Colors.grey),
|
onTap: () {
|
||||||
MySpacing.width(4),
|
TeamMembersBottomSheet.show(
|
||||||
MyText.labelSmall(
|
context,
|
||||||
'Shared with (${matchedEmployees.length})',
|
bucket,
|
||||||
color: Colors.indigo,
|
matchedEmployees,
|
||||||
fontWeight: 600,
|
canEdit: canEdit,
|
||||||
),
|
onEdit: () {
|
||||||
],
|
Get.back();
|
||||||
),
|
Future.delayed(const Duration(milliseconds: 300),
|
||||||
],
|
() {
|
||||||
),
|
EditBucketBottomSheet.show(context, bucket,
|
||||||
if (bucket.description.isNotEmpty)
|
manageBucketController.allEmployees);
|
||||||
Padding(
|
});
|
||||||
padding: const EdgeInsets.only(top: 4),
|
},
|
||||||
child: LayoutBuilder(
|
);
|
||||||
|
},
|
||||||
|
child: Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Row(
|
||||||
|
crossAxisAlignment:
|
||||||
|
CrossAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: MyText.titleSmall(
|
||||||
|
bucket.name,
|
||||||
|
fontWeight: 700,
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
if (canEdit)
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(
|
||||||
|
Icons.delete_outline,
|
||||||
|
color: Colors.red),
|
||||||
|
onPressed: () {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (ctx) => AlertDialog(
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius:
|
||||||
|
BorderRadius.circular(
|
||||||
|
16),
|
||||||
|
),
|
||||||
|
title: Row(
|
||||||
|
children: const [
|
||||||
|
Icon(
|
||||||
|
Icons
|
||||||
|
.warning_amber_rounded,
|
||||||
|
color: Colors.red),
|
||||||
|
SizedBox(width: 8),
|
||||||
|
Text("Delete Bucket",
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight:
|
||||||
|
FontWeight
|
||||||
|
.bold)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
content: const Text(
|
||||||
|
"Are you sure you want to delete this bucket? This action cannot be undone.",
|
||||||
|
style:
|
||||||
|
TextStyle(fontSize: 15),
|
||||||
|
),
|
||||||
|
actionsPadding:
|
||||||
|
const EdgeInsets
|
||||||
|
.symmetric(
|
||||||
|
horizontal: 12,
|
||||||
|
vertical: 8),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () =>
|
||||||
|
Navigator.of(ctx)
|
||||||
|
.pop(),
|
||||||
|
child:
|
||||||
|
const Text("Cancel"),
|
||||||
|
),
|
||||||
|
ElevatedButton.icon(
|
||||||
|
style: ElevatedButton
|
||||||
|
.styleFrom(
|
||||||
|
foregroundColor:
|
||||||
|
Colors.white,
|
||||||
|
backgroundColor:
|
||||||
|
Colors.red,
|
||||||
|
shape:
|
||||||
|
RoundedRectangleBorder(
|
||||||
|
borderRadius:
|
||||||
|
BorderRadius
|
||||||
|
.circular(8),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
icon: const Icon(
|
||||||
|
Icons.delete),
|
||||||
|
label:
|
||||||
|
const Text("Delete"),
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.of(ctx).pop();
|
||||||
|
manageBucketController
|
||||||
|
.deleteBucket(
|
||||||
|
bucket.id);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
if (!canEdit) MySpacing.height(12),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
const Icon(Icons.contacts_outlined,
|
||||||
|
size: 14, color: Colors.grey),
|
||||||
|
MySpacing.width(4),
|
||||||
|
MyText.labelSmall(
|
||||||
|
'${bucket.numberOfContacts} contact(s)',
|
||||||
|
color: Colors.red,
|
||||||
|
fontWeight: 600,
|
||||||
|
),
|
||||||
|
MySpacing.width(12),
|
||||||
|
const Icon(Icons.ios_share_outlined,
|
||||||
|
size: 14, color: Colors.grey),
|
||||||
|
MySpacing.width(4),
|
||||||
|
MyText.labelSmall(
|
||||||
|
'Shared with (${matchedEmployees.length})',
|
||||||
|
color: Colors.indigo,
|
||||||
|
fontWeight: 600,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
MySpacing.height(4),
|
||||||
|
if (bucket.description.isNotEmpty)
|
||||||
|
LayoutBuilder(
|
||||||
builder: (context, constraints) {
|
builder: (context, constraints) {
|
||||||
final span = TextSpan(
|
final span = TextSpan(
|
||||||
text: bucket.description,
|
text: bucket.description,
|
||||||
@ -257,7 +338,6 @@ class _ManageBucketsScreenState extends State<ManageBucketsScreen> {
|
|||||||
maxLines: 2,
|
maxLines: 2,
|
||||||
textDirection: TextDirection.ltr,
|
textDirection: TextDirection.ltr,
|
||||||
)..layout(maxWidth: constraints.maxWidth);
|
)..layout(maxWidth: constraints.maxWidth);
|
||||||
|
|
||||||
final hasOverflow = tp.didExceedMaxLines;
|
final hasOverflow = tp.didExceedMaxLines;
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
@ -296,18 +376,18 @@ class _ManageBucketsScreenState extends State<ManageBucketsScreen> {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
],
|
),
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
],
|
),
|
||||||
),
|
);
|
||||||
);
|
},
|
||||||
},
|
);
|
||||||
);
|
}),
|
||||||
}),
|
),
|
||||||
),
|
],
|
||||||
],
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user