implementing categories submit

This commit is contained in:
Felix 2019-08-12 12:09:04 +01:00
parent a7db1c528b
commit fc2add5636
4 changed files with 24 additions and 67 deletions

View file

@ -4,35 +4,7 @@ import 'package:http/http.dart' as http;
import 'package:local_spend/common/functions/get_token.dart'; import 'package:local_spend/common/functions/get_token.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class Category { Future<List<String>> getCategories() async {
String name;
String index;
Category({
this.name,
this.index,
});
}
Future<List<DropdownMenuItem<String>>> getCategoriesList() async {
//TODO: Return a list of [String, String] where {1} is categoryName and {2} is categoryValue for request
var categoriesList = List<DropdownMenuItem>();
var categories = await getCategories();
categories.forEach((thisCategory) {
var thisMap = new DropdownMenuItem(
child: new Text(thisCategory.name),
value: thisCategory.index,
);
categoriesList.add(thisMap);
});
return categoriesList;
}
Future<List<Category>> getCategories() async { // confusing name
const url = "https://dev.peartrade.org/api/search/category"; const url = "https://dev.peartrade.org/api/search/category";
var token; var token;
@ -53,7 +25,7 @@ Future<List<Category>> getCategories() async { // confusing name
if (response.statusCode == 200) { if (response.statusCode == 200) {
//request successful //request successful
List<Category> categories = new List<Category>(); List<String> categories = new List<String>();
Map responseMap = json.decode(response.body); Map responseMap = json.decode(response.body);
var categoriesJSON = responseMap['categories']; var categoriesJSON = responseMap['categories'];
@ -70,8 +42,7 @@ Future<List<Category>> getCategories() async { // confusing name
if (categoriesJSON[i.toString()] != null) { if (categoriesJSON[i.toString()] != null) {
// print("Iteration " + i.toString()); // print("Iteration " + i.toString());
// print(categoriesJSON[i.toString()]); // print(categoriesJSON[i.toString()]);
categories.add(new Category( categories.add(categoriesJSON[i.toString()]);
name: categoriesJSON[i.toString()], index: i.toString()));
// print(categories.last); // print(categories.last);
i++; i++;
} else { } else {

View file

@ -31,8 +31,7 @@ Future<LoginModel> submitReceiptAPI(
'transaction_type' : "3", 'transaction_type' : "3",
'transaction_value': receipt.amount, 'transaction_value': receipt.amount,
'purchase_time': receipt.time, 'purchase_time': receipt.time,
// 'category': receipt.category, 'category': receipt.category,
// 'category': receipt.category,
'essential': receipt.essential, 'essential': receipt.essential,
'organisation_name': receipt.organisationName, 'organisation_name': receipt.organisationName,
'recurring': receipt.recurring, 'recurring': receipt.recurring,

View file

@ -1,25 +0,0 @@
import 'package:flutter/material.dart';
import 'package:local_spend/common/apifunctions/categories.dart';
import 'dart:async';
Future<List<DropdownMenuItem>> getDropDownItems(String type) async {
List<DropdownMenuItem<String>> items = new List<DropdownMenuItem<String>>();
// if (type == "categories") {
var categories = await getCategories();
categories.forEach((thisValue) {
items.add(
new DropdownMenuItem(
child: new Text(thisValue.name),
value: thisValue.index,
key: Key(thisValue.index),
),
);
});
print("oof");
return items;
// }
}

View file

@ -1,10 +1,12 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:local_spend/common/platform/platform_scaffold.dart'; import 'package:local_spend/common/platform/platform_scaffold.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'dart:core';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:local_spend/common/apifunctions/find_organisations.dart'; import 'package:local_spend/common/apifunctions/find_organisations.dart';
import 'package:local_spend/common/widgets/organisations_dialog.dart'; import 'package:local_spend/common/widgets/organisations_dialog.dart';
import 'package:local_spend/common/apifunctions/submit_receipt_api.dart'; import 'package:local_spend/common/apifunctions/submit_receipt_api.dart';
import 'package:local_spend/common/apifunctions/categories.dart';
class Transaction { class Transaction {
DateTime date; DateTime date;
@ -41,6 +43,10 @@ class ReceiptPage2State extends State<ReceiptPage2> {
"Uncategorised", "Uncategorised",
); );
Future<List<String>> getCats() async {
return await getCategories();
}
_submitReceipt(Transaction transaction) { _submitReceipt(Transaction transaction) {
DateTime dt = new DateTime.now(); DateTime dt = new DateTime.now();
// sample transaction: // sample transaction:
@ -64,6 +70,7 @@ class ReceiptPage2State extends State<ReceiptPage2> {
receipt.recurring = ""; receipt.recurring = "";
} }
print("Category: " + transaction.category);
receipt.category = transaction.category; receipt.category = transaction.category;
receipt.amount = transaction.amount.text.toString(); receipt.amount = transaction.amount.text.toString();
receipt.time = DateFormat("yyyy-MM-dd'T'hh:mm':00.000+01:00'").format(transaction.date).toString(); receipt.time = DateFormat("yyyy-MM-dd'T'hh:mm':00.000+01:00'").format(transaction.date).toString();
@ -73,10 +80,20 @@ class ReceiptPage2State extends State<ReceiptPage2> {
} }
List<String> _sampleRecurringOptions = new List<String>(7); List<String> _sampleRecurringOptions = new List<String>(7);
List<String> _sampleCategories = new List<String>(4);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Future<List<String>> _futureCats = getCats();
List<String> _categories = new List<String>();
_categories.add("Fetching categories...");
_futureCats.then((value) {
_categories = null;
_categories = value;
setState(() {});
});
_sampleRecurringOptions[0] = "None"; _sampleRecurringOptions[0] = "None";
_sampleRecurringOptions[1] = "Daily"; _sampleRecurringOptions[1] = "Daily";
_sampleRecurringOptions[2] = "Weekly"; _sampleRecurringOptions[2] = "Weekly";
@ -85,11 +102,6 @@ class ReceiptPage2State extends State<ReceiptPage2> {
_sampleRecurringOptions[5] = "Quarterly"; _sampleRecurringOptions[5] = "Quarterly";
_sampleRecurringOptions[6] = "Yearly"; // these will be difficult to fetch from server as they are coded into the site's rather than fetched _sampleRecurringOptions[6] = "Yearly"; // these will be difficult to fetch from server as they are coded into the site's rather than fetched
_sampleCategories[0] = "Uncategorised";
_sampleCategories[1] = "Cheese";
_sampleCategories[2] = "Fish";
_sampleCategories[3] = "Music";
return PlatformScaffold( return PlatformScaffold(
appBar: AppBar( appBar: AppBar(
@ -313,9 +325,9 @@ class ReceiptPage2State extends State<ReceiptPage2> {
height: MediaQuery.of(context).copyWith().size.height / 3, height: MediaQuery.of(context).copyWith().size.height / 3,
child: CupertinoPicker( child: CupertinoPicker(
backgroundColor: Colors.white, backgroundColor: Colors.white,
children: _sampleCategories.map((thisOption) => Text(thisOption)).toList(), children: _categories.map((thisOption) => Text(thisOption)).toList(),
onSelectedItemChanged: ((newValue) { onSelectedItemChanged: ((newValue) {
transaction.category = _sampleCategories[newValue]; transaction.category = _categories[newValue];
setState(() {}); setState(() {});
}), }),
itemExtent: 32, itemExtent: 32,