getTimesheets method

Future<void> getTimesheets(
  1. String companyName,
  2. Function callBack
)

This function is for getting timesheets data for a given company

Implementation

Future<void> getTimesheets(String companyName, Function callBack) async {
  List<dynamic> timesheets = [];
  final filters = jsonEncode([
    ['company', '=', companyName],
  ]);
  final fields = [
    'name',
    'employee',
    'status',
    'start_date',
    'end_date',
    'total_hours',
  ];
  var headers = {'Cookie': cookieHeader, 'Accept': 'application/json'};
  var response = await http.get(
    Uri.parse(
      '$baseUrl${Constants.timesheetEndPoint}?filters=$filters&fields=${jsonEncode(fields)}',
    ),
    headers: headers,
  );
  if (response.statusCode == 200) {
    var bodyJson = jsonDecode(response.body);
    timesheets = bodyJson["data"];
  } else if (response.statusCode == 404) {
    MyAlertDialog().showMyAlertDialog(
      context,
      'Error: Resource not found (404)',
    );
  } else if (response.statusCode >= 400 && response.statusCode < 500) {
    MyAlertDialog().showMyAlertDialog(
      context,
      'Client Error: ${response.statusCode}',
    );
  } else if (response.statusCode >= 500 && response.statusCode < 600) {
    MyAlertDialog().showMyAlertDialog(
      context,
      'Server Error: ${response.statusCode}',
    );
  } else {
    MyAlertDialog().showMyAlertDialog(
      context,
      'Unknown Error: ${response.statusCode}',
    );
  }
  callBack(timesheets);
}