profile data can now be displayed when logged in
This commit is contained in:
parent
57d52c3fd7
commit
a4103ab11e
@ -13,7 +13,7 @@ enum ApiService {
|
||||
|
||||
Future<String?> request(BuildContext context, ApiService service, String method,
|
||||
String path, Object? body) async {
|
||||
final messenger = ScaffoldMessenger.of(context);
|
||||
final messenger = ScaffoldMessenger.of(context);
|
||||
|
||||
final host = switch (service) {
|
||||
ApiService.auth => const String.fromEnvironment('AUTH_SERVICE_HOST'),
|
||||
|
@ -29,17 +29,14 @@ class _LoginPageState extends State<LoginPage> {
|
||||
|
||||
// Assuming token is a JSON string
|
||||
Map<String, dynamic> json = jsonDecode(token);
|
||||
User jsonUser = User.fromJson(json);
|
||||
Login jsonUser = Login.fromJson(json);
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.setString('token', jsonUser.token);
|
||||
prefs.setString('id', jsonUser.id);
|
||||
|
||||
|
||||
|
||||
setState(()
|
||||
{user = jsonUser;
|
||||
loggedIn == true;
|
||||
});
|
||||
{loggedIn == true;});
|
||||
|
||||
|
||||
if (mounted) {
|
||||
|
@ -7,18 +7,30 @@ class Favorite {
|
||||
Favorite(this.id, this.userId, this.lat, this.lng);
|
||||
}
|
||||
|
||||
class User {
|
||||
class Login {
|
||||
String token;
|
||||
String id;
|
||||
|
||||
Login(this.token, this.id);
|
||||
|
||||
factory Login.fromJson(Map<String, dynamic> json) {
|
||||
return Login(
|
||||
json['token'],
|
||||
json['id'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class User {
|
||||
String id;
|
||||
String email;
|
||||
String username;
|
||||
DateTime createdAt;
|
||||
|
||||
User(this.token, this.id, this.email, this.username, this.createdAt);
|
||||
User( this.id, this.email, this.username, this.createdAt);
|
||||
|
||||
factory User.fromJson(Map<String, dynamic> json) {
|
||||
return User(
|
||||
json['token'],
|
||||
json['id'],
|
||||
json['email'],
|
||||
json['username'],
|
||||
|
@ -1,45 +1,63 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mobile/base/variables.dart';
|
||||
import 'package:mobile/models.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'base/sidemenu.dart';
|
||||
import 'package:mobile/base/variables.dart';
|
||||
|
||||
|
||||
|
||||
import 'api.dart' as api;
|
||||
|
||||
class ProfilePage extends StatefulWidget {
|
||||
|
||||
const ProfilePage({super.key});
|
||||
//const ProfilePage({super.key, required this.id});
|
||||
|
||||
|
||||
@override
|
||||
State<ProfilePage> createState() => _ProfilePageState();
|
||||
}
|
||||
|
||||
class _ProfilePageState extends State<ProfilePage> {
|
||||
late User userData;
|
||||
User? userData;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
// Check if the user is logged in when the page is entered
|
||||
if (loggedIn == false) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Please log in')));
|
||||
Navigator.pushReplacementNamed(context, '/login');
|
||||
});
|
||||
}
|
||||
|
||||
setState(() {
|
||||
userData = user!;
|
||||
});
|
||||
getProfile();
|
||||
}
|
||||
|
||||
Future<void> getProfile() async {
|
||||
if (!loggedIn) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Please log in')),
|
||||
);
|
||||
Navigator.pushReplacementNamed(context, '/login');
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (user != null) {
|
||||
setState(() {
|
||||
userData = user!;
|
||||
});
|
||||
} else {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
String? id = prefs.getString('id');
|
||||
|
||||
if (id != null) {
|
||||
final response = await api
|
||||
.request(context, api.ApiService.auth, 'GET', '/api/users/$id', null);
|
||||
|
||||
if (response == null) return;
|
||||
|
||||
Map<String, dynamic> json = jsonDecode(response);
|
||||
User jsonUser = User.fromJson(json);
|
||||
|
||||
setState(() {
|
||||
userData = jsonUser;
|
||||
user = jsonUser;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@ -58,33 +76,41 @@ class _ProfilePageState extends State<ProfilePage> {
|
||||
),
|
||||
),
|
||||
Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.account_circle,
|
||||
size: 100,
|
||||
color: Colors.grey,
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
const Text(
|
||||
'Username',
|
||||
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
const Text(
|
||||
'email@example.com',
|
||||
style: TextStyle(fontSize: 16, color: Colors.grey),
|
||||
),
|
||||
const SizedBox(height: 50),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
// Add your edit action here
|
||||
},
|
||||
child: const Text('Edit'),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: userData == null
|
||||
? const CircularProgressIndicator()
|
||||
: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.account_circle,
|
||||
size: 100,
|
||||
color: Colors.grey,
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
Text(
|
||||
userData!.username,
|
||||
style: const TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Text(
|
||||
userData!.email,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 50),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
// Add your edit action here
|
||||
},
|
||||
child: const Text('Edit'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
Loading…
Reference in New Issue
Block a user