2019-07-08 10:53:10 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:local_spend/common/functions/get_token.dart';
|
|
|
|
|
|
|
|
class Organisation {
|
2019-08-07 12:15:15 +00:00
|
|
|
Organisation(
|
2019-08-12 10:37:59 +00:00
|
|
|
this.id,
|
|
|
|
this.name,
|
|
|
|
this.postcode,
|
|
|
|
this.streetName,
|
|
|
|
this.town,
|
2019-08-21 13:53:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
var id = 0;
|
|
|
|
var name = "";
|
|
|
|
var postcode = "";
|
|
|
|
var streetName = ""; //street_name
|
|
|
|
var town = "";
|
2019-07-08 10:53:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:32:52 +00:00
|
|
|
class Organisations {
|
|
|
|
List<Organisation> getTestData() {
|
2019-08-08 14:25:43 +00:00
|
|
|
var numItems = 10;
|
2019-08-07 14:32:52 +00:00
|
|
|
var itemsList = new List<Organisation>();
|
|
|
|
|
|
|
|
for (int i = 0; i < numItems; i++) {
|
2019-08-21 13:53:52 +00:00
|
|
|
itemsList.add(new Organisation(i, "Payee " + (i + 1).toString(),
|
|
|
|
"tee hee hee", "yeet street", "Robloxia"));
|
2019-08-07 14:32:52 +00:00
|
|
|
}
|
2019-07-08 14:03:11 +00:00
|
|
|
|
2019-08-07 14:32:52 +00:00
|
|
|
return itemsList;
|
2019-07-08 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:32:52 +00:00
|
|
|
List<Organisation> _jsonToOrganisations(String json) {
|
|
|
|
Map decoded = jsonDecode(json);
|
|
|
|
List<dynamic> validated = decoded['validated'];
|
|
|
|
List<Map> organisationsMaps = new List<Map>();
|
|
|
|
validated.forEach((element) => organisationsMaps.add(element));
|
|
|
|
List<Organisation> organisations = new List<Organisation>();
|
|
|
|
|
|
|
|
for (var i = 0; i < organisationsMaps.length; i++) {
|
|
|
|
final params = organisationsMaps[i].values.toList();
|
|
|
|
|
|
|
|
var newOrganisation = new Organisation(
|
|
|
|
params[0].toInt(),
|
|
|
|
params[1].toString(),
|
|
|
|
params[2].toString(), // oof
|
|
|
|
params[3].toString(), // this could be improved...
|
|
|
|
params[4].toString(),
|
|
|
|
);
|
|
|
|
|
|
|
|
organisations.add(newOrganisation);
|
|
|
|
}
|
2019-07-08 10:53:10 +00:00
|
|
|
|
2019-08-07 14:32:52 +00:00
|
|
|
return organisations;
|
2019-07-08 10:53:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:32:52 +00:00
|
|
|
Future<List<Organisation>> findOrganisations(String search) async {
|
2019-08-20 12:54:45 +00:00
|
|
|
final url = "https://dev.localspend.co.uk/api/search";
|
2019-08-07 14:32:52 +00:00
|
|
|
var token;
|
|
|
|
|
|
|
|
await getToken().then((result) {
|
|
|
|
token = result;
|
|
|
|
});
|
2019-07-16 10:28:36 +00:00
|
|
|
|
2019-08-07 14:32:52 +00:00
|
|
|
Map<String, String> body = {
|
2019-08-21 13:53:52 +00:00
|
|
|
"search_name": search,
|
|
|
|
"session_key": token,
|
2019-08-07 14:32:52 +00:00
|
|
|
};
|
|
|
|
|
2019-08-21 13:53:52 +00:00
|
|
|
final response = await http.post(
|
2019-08-07 14:32:52 +00:00
|
|
|
url,
|
|
|
|
body: json.encode(body),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
//request successful
|
|
|
|
return _jsonToOrganisations(response.body);
|
|
|
|
} else {
|
|
|
|
// not successful
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 13:53:52 +00:00
|
|
|
}
|