This commit is contained in:
BakoL2323 2026-04-17 12:02:31 +07:00
parent 1ff6fc584e
commit 36af776ca3
2 changed files with 66 additions and 72 deletions

View File

@ -38,18 +38,17 @@ class _LoginPageState extends State<LoginPage> {
// =============================
// SIMPAN DATA USER
// =============================
Future<void> saveUserData(Map<String, dynamic> user) async {
Future<void> saveUserData(Map<String, dynamic> user) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('id', int.tryParse(user['id'].toString()) ?? 0);
await prefs.setInt('user_id', int.tryParse(user['id'].toString()) ?? 0); // FIX
await prefs.setString('name', user['name'] ?? '');
await prefs.setString('email', user['email'] ?? '');
await prefs.setString('address', user['address'] ?? '');
await prefs.setString('rtrw', user['rtrw'] ?? '');
await prefs.setString('role', user['role'] ?? '');
await prefs.setString('status', user['status'] ?? '');
}
}
// =============================
// LOGIN
// =============================

View File

@ -5,6 +5,7 @@ import 'package:http/http.dart' as http;
import 'package:image_picker/image_picker.dart';
import 'package:sijentik/component/app_theme.dart';
import 'package:sijentik/screens/auth/login.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ProfilePage extends StatefulWidget {
const ProfilePage({super.key});
@ -20,17 +21,30 @@ class _ProfilePageState extends State<ProfilePage> {
bool isLoading = true;
Future<int?> getUserId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getInt('user_id');
}
// ==========================
// GET PROFILE
// ==========================
Future getProfile() async {
try {
int? userId = await getUserId();
print("USER ID: $userId"); // debug
if (userId == null) {
setState(() => isLoading = false);
return;
}
final response = await http.get(
Uri.parse("http://192.168.0.118:8000/api/profile?user_id=4"),
Uri.parse("http://192.168.0.118:8000/api/profile?user_id=$userId"),
headers: {"Accept": "application/json"},
);
print(response.statusCode);
print(response.body);
if (response.statusCode == 200) {
@ -41,19 +55,13 @@ class _ProfilePageState extends State<ProfilePage> {
isLoading = false;
});
} else {
setState(() {
isLoading = false;
});
print("Error API");
setState(() => isLoading = false);
}
} catch (e) {
setState(() {
isLoading = false;
});
print("Error: $e");
setState(() => isLoading = false);
print(e);
}
}
}
// ==========================
// PICK IMAGE
// ==========================
@ -75,17 +83,14 @@ class _ProfilePageState extends State<ProfilePage> {
// ==========================
Future uploadPhoto() async {
try {
setState(() {
isLoading = true;
});
int? userId = await getUserId();
var request = http.MultipartRequest(
"POST",
Uri.parse("http://192.168.0.118:8000/api/upload-photo"),
);
request.headers['Accept'] = 'application/json';
request.fields['user_id'] = user?['id'].toString() ?? "0";
request.fields['user_id'] = userId.toString();
request.files.add(
await http.MultipartFile.fromPath(
@ -97,33 +102,23 @@ class _ProfilePageState extends State<ProfilePage> {
var streamedResponse = await request.send();
var response = await http.Response.fromStream(streamedResponse);
print(response.statusCode);
print(response.body);
if (response.statusCode == 200) {
setState(() {
_imageFile = null;
});
await getProfile(); // refresh
await getProfile();
} else {
setState(() {
isLoading = false;
});
print("Upload gagal");
}
} catch (e) {
setState(() {
isLoading = false;
});
print("Error upload: $e");
}
print(e);
}
}
@override
void initState() {
super.initState();
getProfile();
}
@override