-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d1e1e0
commit 28e35fa
Showing
6 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# carrier_nest_flutter | ||
# Carrier Nest Flutter App | ||
|
||
A new Flutter project. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const apiUrl = 'http://localhost:3000/api'; | ||
|
||
class JSONResponse<T> { | ||
final T data; | ||
final List<Error> errors; | ||
|
||
JSONResponse({required this.data, required this.errors}); | ||
} | ||
|
||
class PaginationMetadata { | ||
final int total; | ||
final int currentOffset; | ||
final int currentLimit; | ||
final PaginationPointer? prev; | ||
final PaginationPointer? next; | ||
|
||
PaginationMetadata({ | ||
required this.total, | ||
required this.currentOffset, | ||
required this.currentLimit, | ||
this.prev, | ||
this.next, | ||
}); | ||
} | ||
|
||
class PaginationPointer { | ||
final int offset; | ||
final int limit; | ||
|
||
PaginationPointer({ | ||
required this.offset, | ||
required this.limit, | ||
}); | ||
} | ||
|
||
class Sort { | ||
final String? key; | ||
final String? order; | ||
|
||
Sort({this.key, this.order}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class ExpandedLoad { | ||
// Your fields and constructors here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Dart file. Contains code that loads data from the server. | ||
|
||
import 'package:carrier_nest_flutter/constants.dart'; | ||
import 'package:carrier_nest_flutter/models.dart'; | ||
import 'dart:convert'; | ||
import 'dart:async'; | ||
import 'package:http/http.dart' as http; | ||
|
||
class Loads { | ||
static Future<Map<String, dynamic>> getLoadsExpanded({ | ||
Sort? sort, | ||
String? customerId, | ||
String? driverId, | ||
int? limit, | ||
int? offset, | ||
bool? currentOnly, | ||
String? expand, | ||
}) async { | ||
final Map<String, String> params = { | ||
'expand': expand ?? 'customer,shipper,receiver', | ||
if (sort?.key != null) 'sortBy': sort!.key!, | ||
if (sort?.order != null) 'sortDir': sort!.order!, | ||
if (customerId != null) 'customerId': customerId, | ||
if (driverId != null) 'driverId': driverId, | ||
if (limit != null) 'limit': limit.toString(), | ||
if (offset != null) 'offset': offset.toString(), | ||
if (currentOnly != null) 'currentOnly': currentOnly ? '1' : '0', | ||
}; | ||
|
||
final Uri uri = Uri.parse('$apiUrl/loads').replace(queryParameters: params); | ||
final http.Response response = await http.get(uri); | ||
|
||
if (response.statusCode == 200) { | ||
final Map<String, dynamic> jsonResponse = jsonDecode(response.body); | ||
final List<dynamic> errors = | ||
jsonResponse['errors']; // Assuming errors are a list | ||
|
||
if (errors.isNotEmpty) { | ||
throw Exception(errors.map((e) => e.toString()).join(', ')); | ||
} | ||
|
||
final List<ExpandedLoad> loads = []; | ||
final PaginationMetadata metadata = PaginationMetadata(); | ||
|
||
return {'loads': loads, 'metadata': metadata}; | ||
} else { | ||
throw Exception('Failed to load data'); | ||
} | ||
} | ||
|
||
static Future<ExpandedLoad> getLoadById(String id, | ||
{String? driverId, bool expandCarrier = false}) async { | ||
String expand = 'customer,shipper,receiver,stops,invoice,driver,documents'; | ||
if (expandCarrier) { | ||
expand += ',carrier'; | ||
} | ||
|
||
final Map<String, String> params = { | ||
'expand': expand, | ||
if (driverId != null) 'driverId': driverId, | ||
}; | ||
|
||
final Uri uri = | ||
Uri.parse('$apiUrl/loads/$id').replace(queryParameters: params); | ||
final http.Response response = await http.get(uri); | ||
|
||
if (response.statusCode == 200) { | ||
final Map<String, dynamic> jsonResponse = jsonDecode(response.body); | ||
final List<dynamic> errors = | ||
jsonResponse['errors']; // Assuming errors are a list | ||
|
||
if (errors.isNotEmpty) { | ||
throw Exception(errors.map((e) => e.toString()).join(', ')); | ||
} | ||
|
||
final ExpandedLoad load = ExpandedLoad(); | ||
return load; | ||
} else { | ||
throw Exception('Failed to load data'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters