Move user update and business update to pages folder and update then to be stand alone screens. delete profile update page and rename pprofileOfficeUpdate to profileBusinessUpdate

This commit is contained in:
2024-08-06 11:51:45 +02:00
parent 60a853ed31
commit 8f49814e37
4 changed files with 195 additions and 173 deletions

View File

@@ -0,0 +1,109 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:patient_manager/components/mihAppBar.dart';
import 'package:patient_manager/components/myTextInput.dart';
import 'package:patient_manager/components/mybutton.dart';
import 'package:patient_manager/main.dart';
import 'package:patient_manager/objects/appUser.dart';
class ProfileBusinessUpdate extends StatefulWidget {
final AppUser signedInUser;
//final String userEmail;
const ProfileBusinessUpdate({
super.key,
required this.signedInUser,
});
@override
State<ProfileBusinessUpdate> createState() => _ProfileBusinessUpdateState();
}
class _ProfileBusinessUpdateState extends State<ProfileBusinessUpdate> {
final FocusNode _focusNode = FocusNode();
final tempController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const MIHAppBar(barTitle: "Update Profile"),
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Center(
child: KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: (event) async {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.enter) {
//submitForm();
}
},
child: Column(
children: [
const Text(
"Personal profile:",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 25,
),
),
const SizedBox(height: 15.0),
MyTextField(
controller: tempController,
hintText: "Username",
editable: true,
required: true,
),
const SizedBox(height: 10.0),
MyTextField(
controller: tempController,
hintText: "First Name",
editable: true,
required: true,
),
const SizedBox(height: 10.0),
MyTextField(
controller: tempController,
hintText: "Last Name",
editable: true,
required: true,
),
const SizedBox(height: 10.0),
const Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text(
"Activate Business Account",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
SizedBox(
width: 25,
),
],
),
const SizedBox(height: 10.0),
SizedBox(
width: 500.0,
height: 100.0,
child: MyButton(
buttonText: "Update",
buttonColor:
MzanziInnovationHub.of(context)!.theme.secondaryColor(),
textColor:
MzanziInnovationHub.of(context)!.theme.primaryColor(),
onTap: () {
//submitForm();
},
),
),
],
),
),
),
),
);
}
}

View File

@@ -1,76 +0,0 @@
import 'package:flutter/material.dart';
import 'package:patient_manager/components/mihAppBar.dart';
import 'package:patient_manager/components/profileUserUpdate.dart';
import 'package:patient_manager/objects/appUser.dart';
class ProfileUpdate extends StatefulWidget {
final AppUser signedInUser;
//final String userEmail;
const ProfileUpdate({
super.key,
//required this.userEmail,
required this.signedInUser,
});
@override
State<ProfileUpdate> createState() => _ProfileUpdateState();
}
class _ProfileUpdateState extends State<ProfileUpdate> {
//int _selectedIndex = 0;
@override
void initState() {
// _widgetOptions = <Widget>[
// //Center(child: Text("User profile")),
// ProfileUserUpdate(
// signedInUser: widget.signedInUser,
// ),
// ProfileOfficeUpdate(
// signedInUser: widget.signedInUser,
// ),
// ];
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const MIHAppBar(barTitle: "Update Profile"),
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Center(
child: ProfileUserUpdate(
signedInUser: widget.signedInUser,
),
),
),
// bottomNavigationBar: GNav(
// //hoverColor: Colors.lightBlueAccent,
// color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
// iconSize: 35.0,
// activeColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
// tabBackgroundColor:
// MzanziInnovationHub.of(context)!.theme.secondaryColor(),
// //gap: 20,
// //padding: EdgeInsets.all(15),
// tabs: const [
// GButton(
// icon: Icons.perm_identity,
// text: "User Profile",
// ),
// GButton(
// icon: Icons.business,
// text: "Office Profile",
// ),
// ],
// selectedIndex: _selectedIndex,
// onTabChange: (index) {
// setState(() {
// _selectedIndex = index;
// });
// },
// ),
);
}
}

View File

@@ -0,0 +1,250 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:patient_manager/components/mihAppBar.dart';
import 'package:patient_manager/components/myErrorMessage.dart';
import 'package:patient_manager/components/mySuccessMessage.dart';
import 'package:patient_manager/components/myTextInput.dart';
import 'package:patient_manager/components/mybutton.dart';
import 'package:patient_manager/env/env.dart';
import 'package:patient_manager/main.dart';
import 'package:patient_manager/objects/appUser.dart';
import 'package:supertokens_flutter/http.dart' as http;
class ProfileUserUpdate extends StatefulWidget {
final AppUser signedInUser;
//final String userEmail;
const ProfileUserUpdate({
super.key,
required this.signedInUser,
});
@override
State<ProfileUserUpdate> createState() => _ProfileUserUpdateState();
}
class _ProfileUserUpdateState extends State<ProfileUserUpdate> {
final usernameController = TextEditingController();
final fnameController = TextEditingController();
final lnameController = TextEditingController();
late bool businessUser;
final FocusNode _focusNode = FocusNode();
bool isFieldsFilled() {
if (fnameController.text.isEmpty ||
lnameController.text.isEmpty ||
usernameController.text.isEmpty) {
return false;
} else {
return true;
}
}
Future<void> updateUserApiCall() async {
//print("Here1");
//userEmail = getLoginUserEmail() as String;
//print(userEmail);
//print("Here2");
//await getOfficeIdByUser(docOfficeIdApiUrl + userEmail);
//print(futureDocOfficeId.toString());
//print("Here3");
var profileType;
if (businessUser) {
profileType = "business";
} else {
profileType = "personal";
}
print("is username valid ${isUsernameValid(usernameController.text)}");
if (isUsernameValid(usernameController.text) == false) {
usernamePopUp();
} else {
var response = await http.put(
Uri.parse("${AppEnviroment.baseApiUrl}/user/update/"),
headers: <String, String>{
"Content-Type": "application/json; charset=UTF-8"
},
body: jsonEncode(<String, dynamic>{
"idusers": widget.signedInUser.idUser,
"username": usernameController.text,
"fnam": fnameController.text,
"lname": lnameController.text,
"type": profileType,
}),
);
//print("Here4");
//print(response.statusCode);
if (response.statusCode == 200) {
Navigator.of(context)
.popAndPushNamed('/', arguments: widget.signedInUser);
String message =
"${widget.signedInUser.email}'s information has been updated successfully!";
successPopUp(message);
} else {
internetConnectionPopUp();
}
}
}
bool isBusinessUser() {
if (widget.signedInUser.type == "personal") {
return false;
} else {
return true;
}
}
void internetConnectionPopUp() {
showDialog(
context: context,
builder: (context) {
return const MyErrorMessage(errorType: "Internet Connection");
},
);
}
void successPopUp(String message) {
showDialog(
context: context,
builder: (context) {
return MySuccessMessage(
successType: "Success",
successMessage: message,
);
},
);
}
void usernamePopUp() {
showDialog(
context: context,
builder: (context) {
return const MyErrorMessage(errorType: "Invalid Username");
},
);
}
bool isUsernameValid(String username) {
return RegExp(r'^(?=[a-zA-Z0-9._]{8,20}$)(?!.*[_.]{2})[^_.].*[^_.]$')
.hasMatch(username);
}
void submitForm() {
if (isFieldsFilled()) {
updateUserApiCall();
} else {
showDialog(
context: context,
builder: (context) {
return const MyErrorMessage(errorType: "Input Error");
},
);
}
}
@override
void initState() {
setState(() {
fnameController.text = widget.signedInUser.fname;
lnameController.text = widget.signedInUser.lname;
usernameController.text = widget.signedInUser.username;
businessUser = isBusinessUser();
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const MIHAppBar(barTitle: "Update Profile"),
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Center(
child: KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: (event) async {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.enter) {
submitForm();
}
},
child: Column(
children: [
const Text(
"Personal profile:",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 25,
),
),
const SizedBox(height: 15.0),
MyTextField(
controller: usernameController,
hintText: "Username",
editable: true,
required: true,
),
const SizedBox(height: 10.0),
MyTextField(
controller: fnameController,
hintText: "First Name",
editable: true,
required: true,
),
const SizedBox(height: 10.0),
MyTextField(
controller: lnameController,
hintText: "Last Name",
editable: true,
required: true,
),
const SizedBox(height: 10.0),
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text(
"Activate Business Account",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
const SizedBox(
width: 25,
),
Switch(
value: businessUser,
onChanged: (bool value) {
setState(() {
businessUser = value;
});
},
),
],
),
const SizedBox(height: 10.0),
SizedBox(
width: 500.0,
height: 100.0,
child: MyButton(
buttonText: "Update",
buttonColor:
MzanziInnovationHub.of(context)!.theme.secondaryColor(),
textColor:
MzanziInnovationHub.of(context)!.theme.primaryColor(),
onTap: () {
submitForm();
},
),
),
],
),
),
),
),
);
}
}