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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -247,8 +247,44 @@ 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,7 +94,8 @@ class _ManageBucketsScreenState extends State<ManageBucketsScreen> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
body: Column(
|
body: SafeArea(
|
||||||
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
Padding(
|
Padding(
|
||||||
padding: MySpacing.xy(16, 12),
|
padding: MySpacing.xy(16, 12),
|
||||||
@ -189,32 +190,117 @@ class _ManageBucketsScreenState extends State<ManageBucketsScreen> {
|
|||||||
matchedEmployees,
|
matchedEmployees,
|
||||||
canEdit: canEdit,
|
canEdit: canEdit,
|
||||||
onEdit: () {
|
onEdit: () {
|
||||||
Get.back(); // Close the bottom sheet first
|
Get.back();
|
||||||
Future.delayed(const Duration(milliseconds: 300), () {
|
Future.delayed(const Duration(milliseconds: 300),
|
||||||
EditBucketBottomSheet.show(context, bucket, manageBucketController.allEmployees);
|
() {
|
||||||
|
EditBucketBottomSheet.show(context, bucket,
|
||||||
|
manageBucketController.allEmployees);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
child: Row(
|
child: Row(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
const Padding(
|
|
||||||
padding: EdgeInsets.only(top: 4),
|
|
||||||
child: Icon(Icons.label_outline,
|
|
||||||
size: 26, color: Colors.indigo),
|
|
||||||
),
|
|
||||||
MySpacing.width(12),
|
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
MyText.titleSmall(
|
Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Row(
|
||||||
|
crossAxisAlignment:
|
||||||
|
CrossAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: MyText.titleSmall(
|
||||||
bucket.name,
|
bucket.name,
|
||||||
fontWeight: 700,
|
fontWeight: 700,
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
),
|
),
|
||||||
MySpacing.height(4),
|
),
|
||||||
|
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(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
const Icon(Icons.contacts_outlined,
|
const Icon(Icons.contacts_outlined,
|
||||||
@ -226,8 +312,6 @@ class _ManageBucketsScreenState extends State<ManageBucketsScreen> {
|
|||||||
fontWeight: 600,
|
fontWeight: 600,
|
||||||
),
|
),
|
||||||
MySpacing.width(12),
|
MySpacing.width(12),
|
||||||
Row(
|
|
||||||
children: [
|
|
||||||
const Icon(Icons.ios_share_outlined,
|
const Icon(Icons.ios_share_outlined,
|
||||||
size: 14, color: Colors.grey),
|
size: 14, color: Colors.grey),
|
||||||
MySpacing.width(4),
|
MySpacing.width(4),
|
||||||
@ -238,12 +322,9 @@ class _ManageBucketsScreenState extends State<ManageBucketsScreen> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
],
|
MySpacing.height(4),
|
||||||
),
|
|
||||||
if (bucket.description.isNotEmpty)
|
if (bucket.description.isNotEmpty)
|
||||||
Padding(
|
LayoutBuilder(
|
||||||
padding: const EdgeInsets.only(top: 4),
|
|
||||||
child: 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,7 +376,6 @@ class _ManageBucketsScreenState extends State<ManageBucketsScreen> {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -309,6 +388,7 @@ class _ManageBucketsScreenState extends State<ManageBucketsScreen> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user