diff --git a/lib/common/apifunctions/find_organisations.dart b/lib/common/apifunctions/find_organisations.dart new file mode 100644 index 0000000..c39afd7 --- /dev/null +++ b/lib/common/apifunctions/find_organisations.dart @@ -0,0 +1,56 @@ +import 'dart:convert'; +import 'dart:async'; +import 'package:http/http.dart' as http; +import 'package:local_spend/common/functions/get_token.dart'; + +class Organisation { + var id = 0; + var name = ""; + var postcode = ""; + var street_name = ""; + var town = ""; +} + +List jsonToOrganisations(String json) { + Map decoded = jsonDecode(json); + print(decoded); + + List validated = decoded['validated']; +// Map organisation = validated[0]; + + List organisations = new List(); + + validated.forEach((element) => organisations.add(element)); + +// print(organisations); +} + +Future> findOrganisations(String search) async { + final url = "https://dev.peartrade.org/api/search"; + var token; + + await getToken().then((result) { + token = result; + }); + + Map body = { + "search_name":search, + "session_key":token, + }; + + final response = await http.post ( + url, + body: json.encode(body), + ); + + print(response.body); + + if (response.statusCode == 200) { + //request successful + return jsonToOrganisations(response.body); + } else { + // not successful + return null; + } + +} \ No newline at end of file diff --git a/lib/pages/receipt_page.dart b/lib/pages/receipt_page.dart index 7e76e01..53f126e 100644 --- a/lib/pages/receipt_page.dart +++ b/lib/pages/receipt_page.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -10,9 +9,8 @@ import 'package:local_spend/common/widgets/basic_drawer.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:intl/intl.dart'; -import 'package:http/http.dart' as http; -import 'package:local_spend/common/functions/get_token.dart'; import 'package:datetime_picker_formfield/datetime_picker_formfield.dart'; +import 'package:local_spend/common/apifunctions/find_organisations.dart'; const URL = "https://flutter.io/"; const demonstration = false; @@ -123,39 +121,6 @@ class ReceiptPageState extends State { return "false"; } - Future findOrganisations(String search) async { - final url = "https://dev.peartrade.org/api/search"; - var token; - - await getToken().then((result) { - token = result; - }); - - Map body = { - "search_name":search, - "session_key":token, - }; - - debugPrint(token.toString() + " " + json.encode(body).toString()); - - final response = await http.post ( - url, - //{"search_name":"bo","session_key":"1C2AB5F0-A163-11E9-BE68-241789E69626"} - body: json.encode(body), - ); - - debugPrint(response.body); - debugPrint(response.statusCode.toString()); - - if (response.statusCode == 200) { - //request successful - return response.body; - } else { - return null; - } - - } - String formatDate(String date) { // return ""; // should be in format: @@ -302,7 +267,7 @@ class ReceiptPageState extends State { debugPrint("TODO: 'find organisation' dialog"); findOrganisations(_orgController.text); - + }, child: Text("Find", style: @@ -334,6 +299,7 @@ class ReceiptPageState extends State { onSubmitted: (_) { submitReceipt(_amountController.text, _timeController.text, _orgController.text); + // TODO: make sure organisation is valid // TODO: Add 'find organisation' button which displays a dialog to, well, find the organisation's address or manual entry }, ),