Skip to content

Commit

Permalink
FacAPI , make announcements
Browse files Browse the repository at this point in the history
  • Loading branch information
anoop-rkt committed Apr 13, 2024
1 parent 65bbe4d commit 31280cf
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 73 deletions.
8 changes: 4 additions & 4 deletions lib/api.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//Server and local links
String klocalLink = "127.0.0.1:8000";
String kserverLink = "172.27.39.179:8000";
String kserverLink = "172.27.113.117:8000";
// String kserverLink = "172.27.16.214:8000";

//Login Service
// String kAuthUrl = "172.27.16.214:8000";
String kAuthUrl = "172.27.39.179:8000";
String kAuthUrl = "172.27.113.117:8000";
String kAuthLogin = "/api/auth/login/";
// String kAuthUrl = "172.27.16.214:8000";
// String kAuthLogin = "/accounts/login";

//Profile Service
Expand All @@ -16,9 +16,9 @@ String kProfile = "/api/profile/";
String kAcademicProcedures = "/academic-procedures/api/stu/";

//Department details
String kFacView = "/dep/api/fac-view/";
String kDepartmentAnnouncements = "/dep/api/announcements/";
// String kDepMainAnnouncements = "/dep/api/dep-main/";
// String kFacView = "/dep/api/fac-view/";

//Complaint
String kComplaintService = "/complaint/api/studentcomplain";
Expand Down
78 changes: 60 additions & 18 deletions lib/screens/Department/Announcements/browse_announcement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,64 @@ class _BrowseAnnouncementScreenState extends State<BrowseAnnouncementScreen> {
}

void _loadAnnouncements() async {
String? userRole = data?.profile?['user_type'];
print(userRole);
try {
http.Response jsonResponse = await _departmentService.getAnnouncements();

print('API Response Status Code: ${jsonResponse.statusCode}');
print('API Response Body: ${jsonResponse.body}');
if (userRole == 'student') {
try {
http.Response jsonResponse =
await _departmentService.getAnnouncements();
List<dynamic> jsonData = json.decode(jsonResponse.body);
List<Announcement> announcements = jsonData.map((item) {
return Announcement.fromJson(item);
}).toList();

List<dynamic> jsonData = json.decode(jsonResponse.body);
_announcementsController.add(announcements.toList());
} catch (error) {
print('Error loading announcements: $error');
}
} else if (userRole == 'faculty') {
try {
http.Response jsonResponse =
await _departmentService.getFacViewAnnouncements();
Map<String, dynamic> jsonData = json.decode(jsonResponse.body);
List<dynamic> announcementsData = jsonData['announcements'] ?? [];
List<Announcement> announcements = [];
for (var list in announcementsData) {
if (list is List) {
for (var item in list) {
if (item is Map<String, dynamic>) {
announcements.add(Announcement.fromJson(item));
}
}
}
}
print('Announcements: $announcements');

List<Announcement> announcements = jsonData.map((item) {
return Announcement.fromJson(item);
}).toList();
print('Announcements: $announcements');
_announcementsController.add(announcements.toList());
} catch (error) {
print('Error loading announcements: $error');
}
} else if (userRole == 'admin') {
try {
http.Response jsonResponse =
await _departmentService.getAnnouncements();
print('API Response Status Code: ${jsonResponse.statusCode}');
print('API Response Body: ${jsonResponse.body}');
List<dynamic> jsonData = json.decode(jsonResponse.body);
List<Announcement> announcements = jsonData.map((item) {
return Announcement.fromJson(item);
}).toList();
print('Announcements: $announcements');

_announcementsController.add(announcements.toList());
_announcementsController.add(announcements.toList());
} catch (error) {
print('Error loading announcements: $error');
}
} else {
// Handle other cases or unknown roles
return;
}
} catch (error) {
print('Error loading announcements: $error');
}
Expand Down Expand Up @@ -76,7 +120,7 @@ class _BrowseAnnouncementScreenState extends State<BrowseAnnouncementScreen> {
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (userRole != null && userRole != 'student')
if (userRole == 'faculty' || userRole != 'student')
ElevatedButton(
onPressed: () {
Navigator.push(
Expand Down Expand Up @@ -164,15 +208,13 @@ class _BrowseAnnouncementScreenState extends State<BrowseAnnouncementScreen> {
.map(
(announcement) => DataRow(
cells: [
DataCell(
Text(announcement.ann_date)),
DataCell(
Text(announcement.maker_id)),
DataCell(
Text(announcement.programme)),
DataCell(Text(announcement.ann_date)),
DataCell(Text(announcement.maker_id)),
DataCell(Text(announcement.programme)),
DataCell(Text(announcement.batch)),
DataCell(Text(announcement.message)),
DataCell(Text(announcement.upload_announncement ?? '')),
DataCell(Text(
announcement.upload_announcement ?? '')),
],
),
)
Expand Down
90 changes: 47 additions & 43 deletions lib/screens/Department/Announcements/make_announcement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:fusion/Components/appBar.dart';
import 'package:fusion/Components/side_drawer.dart';
import 'package:file_picker/file_picker.dart';
import 'package:fusion/services/department_service.dart';

class MakeAnnouncementScreen extends StatefulWidget {
@override
Expand All @@ -10,31 +11,37 @@ class MakeAnnouncementScreen extends StatefulWidget {

class _MakeAnnouncementScreenState extends State<MakeAnnouncementScreen> {
List<Map<String, String>> announcements = [];

// Add a GlobalKey<FormState> to manage the form state
final _formKey = GlobalKey<FormState>();

// Define data for dropdowns with placeholder items
List<String?> programmeTypes = [null, 'B.Tech', 'M.Tech', 'Ph.D'];
List<String?> programmeTypes = [null, 'All', 'B.Tech', 'M.Tech', 'Ph.D'];
List<String?> departmentTypes = [null, 'All', 'CSE', 'ECE', 'ME', 'SM'];
List<String?> batches = [
null,
'All',
'First Year',
'Second Year',
'Third Year',
'Fourth Year'
];
List<String?> departmentTypes = [null, 'CSE', 'ECE', 'ME', 'SM'];

// Selected values for dropdowns
String? selectedProgrammeType;
String? selectedBatch;
String? selectedDepartmentType;

// Warning messages for required fields
String? programmeWarning;
String? departmentWarning;

String selectedFilePath = 'No file chosen';
TextEditingController announcementDetailsController = TextEditingController();

@override
void initState() {
super.initState();
announcementDetailsController = TextEditingController();
}

@override
void dispose() {
announcementDetailsController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -71,12 +78,6 @@ class _MakeAnnouncementScreenState extends State<MakeAnnouncementScreen> {
programmeWarning = null;
departmentWarning = null;
});
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => MakeAnnouncementScreen(),
// ),
// );
},
child: Text(
'Make Announcement',
Expand All @@ -87,8 +88,6 @@ class _MakeAnnouncementScreenState extends State<MakeAnnouncementScreen> {
),
),
),

// Form
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(20.0),
Expand Down Expand Up @@ -159,8 +158,7 @@ class _MakeAnnouncementScreenState extends State<MakeAnnouncementScreen> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.only(
left: 8), // Adjust the left margin as needed
margin: EdgeInsets.only(left: 8),
child: Text(
'Attach Files(pdf, jpeg, jpg, png):',
style: TextStyle(fontSize: 16),
Expand All @@ -172,7 +170,6 @@ class _MakeAnnouncementScreenState extends State<MakeAnnouncementScreen> {
children: [
ElevatedButton(
onPressed: () {
// Open custom file picker dialog
_showFilePickerDialog(context);
},
child: Text('Choose File'),
Expand All @@ -187,26 +184,8 @@ class _MakeAnnouncementScreenState extends State<MakeAnnouncementScreen> {
SizedBox(height: 16),
ElevatedButton(
onPressed: () {
// Validate the form before submission
if (_formKey.currentState!.validate()) {
// Form is valid, handle submission
if (selectedProgrammeType == null) {
setState(() {
programmeWarning = 'This field is required';
});
}
if (selectedDepartmentType == null) {
setState(() {
departmentWarning = 'This field is required';
});
}
if (selectedProgrammeType != null &&
selectedDepartmentType != null) {
print('Programme Type: $selectedProgrammeType');
print('Batch: $selectedBatch');
print('Department Type: $selectedDepartmentType');
print('Selected File: $selectedFilePath');
}
submitForm();
}
},
child: Text('Publish'),
Expand All @@ -222,7 +201,6 @@ class _MakeAnnouncementScreenState extends State<MakeAnnouncementScreen> {
);
}

// Custom file picker dialog
Future<void> _showFilePickerDialog(BuildContext context) async {
try {
FilePickerResult? result =
Expand All @@ -240,6 +218,33 @@ class _MakeAnnouncementScreenState extends State<MakeAnnouncementScreen> {
}
}

Future<void> submitForm() async {
if (selectedProgrammeType == null) {
setState(() {
programmeWarning = 'This field is required';
});
return;
}
if (selectedDepartmentType == null) {
setState(() {
departmentWarning = 'This field is required';
});
return;
}

Map<String, dynamic> announcementData = {
'maker_id': 'your_maker_id_value_here',
'programme': selectedProgrammeType!,
'batch': selectedBatch ?? '',
'department': selectedDepartmentType!,
'message': announcementDetailsController.text,
'upload_announncement': selectedFilePath,
};
await DepartmentService().createAnnouncement(announcementData).then((_) =>
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Announcement created successfully !"))));
}

Widget buildDropdown(String label, List<String?> items, String? selectedValue,
String placeholder, Function(String?) onChanged) {
return Column(
Expand All @@ -262,8 +267,7 @@ class _MakeAnnouncementScreenState extends State<MakeAnnouncementScreen> {
decoration: InputDecoration(
border: OutlineInputBorder(),
contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 3),
fillColor: Color.fromARGB(
255, 245, 242, 242), // Background color for dropdown
fillColor: Color.fromARGB(255, 245, 242, 242),
filled: true,
),
child: DropdownButtonHideUnderline(
Expand Down
Loading

0 comments on commit 31280cf

Please sign in to comment.