added changes to login model and start of receipt
This commit is contained in:
parent
3af4997c01
commit
075a57278f
10 changed files with 122 additions and 79 deletions
|
@ -7,6 +7,7 @@ import 'package:local_spend/common/functions/save_current_login.dart';
|
|||
import 'package:local_spend/common/functions/show_dialog_single_button.dart';
|
||||
import 'package:local_spend/model/json/login_model.dart';
|
||||
import 'package:local_spend/config.dart';
|
||||
// debug
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
Future<LoginModel> requestLoginAPI(
|
||||
|
@ -32,14 +33,14 @@ Future<LoginModel> requestLoginAPI(
|
|||
final responseJson = json.decode(response.body);
|
||||
var user = new LoginModel.fromJson(responseJson);
|
||||
|
||||
saveCurrentLogin(responseJson);
|
||||
saveCurrentLogin(responseJson, body["email"]);
|
||||
Navigator.of(context).pushReplacementNamed('/HomePage');
|
||||
|
||||
return LoginModel.fromJson(responseJson);
|
||||
} else {
|
||||
final responseJson = json.decode(response.body);
|
||||
|
||||
saveCurrentLogin(responseJson);
|
||||
saveCurrentLogin(responseJson, body["email"]);
|
||||
showDialogSingleButton(
|
||||
context,
|
||||
"Unable to Login",
|
||||
|
|
47
lib/common/apifunctions/submit_receipt_api.dart
Normal file
47
lib/common/apifunctions/submit_receipt_api.dart
Normal file
|
@ -0,0 +1,47 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:local_spend/common/functions/save_current_login.dart';
|
||||
import 'package:local_spend/common/functions/show_dialog_single_button.dart';
|
||||
import 'package:local_spend/model/json/login_model.dart';
|
||||
import 'package:local_spend/config.dart';
|
||||
// debug
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
Future<LoginModel> submitReceiptAPI(
|
||||
BuildContext context, String amount, String time) async {
|
||||
//var apiUrl = ConfigWrapper.of(context).apiKey;
|
||||
final url = "https://dev.peartrade.org/api/login";
|
||||
|
||||
Map<String, String> body = {
|
||||
'transaction_value': amount,
|
||||
'purchase_time': time,
|
||||
};
|
||||
|
||||
debugPrint('$body');
|
||||
|
||||
final response = await http.post(
|
||||
url,
|
||||
body: json.encode(body),
|
||||
);
|
||||
|
||||
debugPrint(response.body);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final responseJson = json.decode(response.body);
|
||||
|
||||
return LoginModel.fromJson(responseJson);
|
||||
} else {
|
||||
final responseJson = json.decode(response.body);
|
||||
|
||||
|
||||
showDialogSingleButton(
|
||||
context,
|
||||
"Unable to Submit Receipt",
|
||||
"You may have supplied an invalid 'Email' / 'Password' combination. Please try again or email an administrator.",
|
||||
"OK");
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
import 'package:local_spend/model/json/login_model.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
saveCurrentLogin(Map responseJson) async {
|
||||
saveCurrentLogin(Map responseJson, loginEmail) async {
|
||||
SharedPreferences preferences = await SharedPreferences.getInstance();
|
||||
|
||||
var user;
|
||||
|
@ -13,12 +13,9 @@ saveCurrentLogin(Map responseJson) async {
|
|||
var token = (responseJson != null && responseJson.isNotEmpty)
|
||||
? LoginModel.fromJson(responseJson).token
|
||||
: "";
|
||||
var email = (responseJson != null && responseJson.isNotEmpty)
|
||||
? LoginModel.fromJson(responseJson).email
|
||||
var email = (loginEmail != null)
|
||||
? loginEmail
|
||||
: "";
|
||||
var pk = (responseJson != null && responseJson.isNotEmpty)
|
||||
? LoginModel.fromJson(responseJson).userId
|
||||
: 0;
|
||||
|
||||
await preferences.setString(
|
||||
'LastUser', (user != null && user.length > 0) ? user : "");
|
||||
|
@ -26,5 +23,4 @@ saveCurrentLogin(Map responseJson) async {
|
|||
'LastToken', (token != null && token.length > 0) ? token : "");
|
||||
await preferences.setString(
|
||||
'LastEmail', (email != null && email.length > 0) ? email : "");
|
||||
await preferences.setInt('LastUserId', (pk != null && pk > 0) ? pk : 0);
|
||||
}
|
||||
|
|
|
@ -6,5 +6,4 @@ saveLogout() async {
|
|||
await preferences.setString('LastUser', "");
|
||||
await preferences.setString('LastToken', "");
|
||||
await preferences.setString('LastEmail', "");
|
||||
await preferences.setInt('LastUserId', 0);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:local_spend/common/apifunctions/request_logout_api.dart';
|
||||
import 'package:local_spend/common/functions/get_token.dart';
|
||||
// debug
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class BasicDrawer extends StatefulWidget {
|
||||
@override
|
||||
|
@ -8,6 +11,9 @@ class BasicDrawer extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _BasicDrawerState extends State<BasicDrawer> {
|
||||
var token;
|
||||
// TODO: add getter with getToken to check logged in
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Drawer(
|
||||
|
@ -21,9 +27,10 @@ class _BasicDrawerState extends State<BasicDrawer> {
|
|||
style: TextStyle(color: Colors.black, fontSize: 20.0),
|
||||
),
|
||||
onTap: () {
|
||||
requestLogoutAPI(context);
|
||||
debugPrint('$token');
|
||||
Navigator.of(context).pushNamed('/ReceiptPage');
|
||||
},
|
||||
// enabled: token != null && token.isNotEmpty,
|
||||
),
|
||||
ListTile(
|
||||
title: Text(
|
||||
|
|
|
@ -1,21 +1,15 @@
|
|||
class LoginModel {
|
||||
final String userName;
|
||||
final String token;
|
||||
final String email;
|
||||
final int userId;
|
||||
|
||||
LoginModel(this.userName, this.token, this.email, this.userId);
|
||||
LoginModel(this.userName, this.token);
|
||||
|
||||
LoginModel.fromJson(Map<String, dynamic> json)
|
||||
: userName = json['name'],
|
||||
token = json['token'],
|
||||
email = json['email'],
|
||||
userId = json['pk'];
|
||||
: userName = json['display_name'],
|
||||
token = json['session_key'];
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'name': userName,
|
||||
'token': token,
|
||||
'email': email,
|
||||
'pk': userId,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,12 +2,13 @@ import 'dart:async';
|
|||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:local_spend/common/apifunctions/request_login_api.dart';
|
||||
import 'package:local_spend/common/apifunctions/submit_receipt_api.dart';
|
||||
import 'package:local_spend/common/functions/show_dialog_single_button.dart';
|
||||
import 'package:local_spend/common/platform/platform_scaffold.dart';
|
||||
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:datetime_picker_formfield/datetime_picker_formfield.dart';
|
||||
|
||||
const URL = "https://flutter.io/";
|
||||
|
||||
|
@ -19,9 +20,12 @@ class ReceiptPage extends StatefulWidget {
|
|||
}
|
||||
|
||||
class ReceiptPageState extends State<ReceiptPage> {
|
||||
final TextEditingController _emailController = TextEditingController();
|
||||
final TextEditingController _passwordController = TextEditingController();
|
||||
String _welcomeString = "";
|
||||
final TextEditingController _timeController = TextEditingController();
|
||||
final TextEditingController _amountController = TextEditingController();
|
||||
final TextEditingController _essentialController = TextEditingController();
|
||||
final TextEditingController _recurringController = TextEditingController();
|
||||
final TextEditingController _typeController = TextEditingController();
|
||||
final TextEditingController _orgController = TextEditingController();
|
||||
|
||||
Future launchURL(String url) async {
|
||||
if (await canLaunch(url)) {
|
||||
|
@ -38,7 +42,7 @@ class ReceiptPageState extends State<ReceiptPage> {
|
|||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_saveCurrentRoute("/LoginPage");
|
||||
_saveCurrentRoute("/ReceiptPageState");
|
||||
}
|
||||
|
||||
_saveCurrentRoute(String lastRoute) async {
|
||||
|
@ -62,7 +66,7 @@ class ReceiptPageState extends State<ReceiptPage> {
|
|||
drawer: BasicDrawer(),
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
"LOGIN",
|
||||
"Submit Receipt",
|
||||
style: TextStyle(
|
||||
fontSize: 30.0,
|
||||
color: Colors.black,
|
||||
|
@ -83,37 +87,12 @@ class ReceiptPageState extends State<ReceiptPage> {
|
|||
child: Padding(
|
||||
padding: EdgeInsets.fromLTRB(0.0, 40.0, 0.0, 15.0),
|
||||
child: Text(
|
||||
"Local Loop",
|
||||
style: TextStyle(fontSize: 40.0, color: Colors.black),
|
||||
"Required Fields in bold",
|
||||
style: TextStyle(fontSize: 20.0, color: Colors.black),
|
||||
),
|
||||
)),
|
||||
Padding(
|
||||
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 78.0),
|
||||
child: RichText(
|
||||
text: TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text:
|
||||
'This is the logon page.',
|
||||
style: new TextStyle(
|
||||
fontSize: 20.0,
|
||||
color: Colors.black,
|
||||
),
|
||||
),
|
||||
TextSpan(
|
||||
text:
|
||||
' It is currently in development.',
|
||||
style: new TextStyle(
|
||||
fontSize: 20.0,
|
||||
color: Colors.black,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
"Email",
|
||||
"Time of Transaction",
|
||||
style: TextStyle(
|
||||
fontSize: 18.0,
|
||||
color: Colors.black,
|
||||
|
@ -123,7 +102,7 @@ class ReceiptPageState extends State<ReceiptPage> {
|
|||
Padding(
|
||||
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0),
|
||||
child: TextField(
|
||||
controller: _emailController,
|
||||
controller: _timeController,
|
||||
decoration: InputDecoration(
|
||||
hintText: "Use your login email",
|
||||
),
|
||||
|
@ -137,7 +116,7 @@ class ReceiptPageState extends State<ReceiptPage> {
|
|||
Padding(
|
||||
padding: EdgeInsets.fromLTRB(0.0, 35.0, 0.0, 0.0),
|
||||
child: Text(
|
||||
"Password",
|
||||
"Amount",
|
||||
style: TextStyle(
|
||||
fontSize: 18.0,
|
||||
color: Colors.black,
|
||||
|
@ -148,7 +127,7 @@ class ReceiptPageState extends State<ReceiptPage> {
|
|||
Padding(
|
||||
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0),
|
||||
child: TextField(
|
||||
controller: _passwordController,
|
||||
controller: _amountController,
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Your password, keep it secret, keep it safe.',
|
||||
),
|
||||
|
@ -167,8 +146,8 @@ class ReceiptPageState extends State<ReceiptPage> {
|
|||
child: RaisedButton(
|
||||
onPressed: () {
|
||||
SystemChannels.textInput.invokeMethod('TextInput.hide');
|
||||
requestLoginAPI(context, _emailController.text,
|
||||
_passwordController.text);
|
||||
submitReceiptAPI(context, _amountController.text,
|
||||
_timeController.text);
|
||||
},
|
||||
child: Text("LOGIN",
|
||||
style:
|
||||
|
|
Reference in a new issue