Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Network/#54] 프로필 수정 #57

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions outfoot/lib/api/profile_edit_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:dio/dio.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:outfoot/models/profile_edit_model.dart';

class ProfileEditApi {
final Dio _dio = Dio();

/// 프로필 수정 API 요청
Future<ProfileEditResponse> updateProfile({
required String nickname,
required String myIntro,
required String email,
required String token,
}) async {
final String? baseUrl = dotenv.env['BASE_URL'];

try {
print("Request URL: $baseUrl/my");

final response = await _dio.put(
'$baseUrl/my',
data: {
'nickname': nickname,
'myIntro': myIntro,
'email': email,
},
options: Options(
headers: {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
},
),
);

if (response.statusCode == 200) {
return ProfileEditResponse.fromJson(response.data);
} else {
throw Exception('프로필 수정 실패: ${response.data}');
}
} catch (e) {
throw Exception('오류 발생: $e');
}
}
}
44 changes: 44 additions & 0 deletions outfoot/lib/models/profile_edit_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class ProfileEditResponse {
final bool success;
final ProfileData? response;
final String? message;

ProfileEditResponse({
required this.success,
this.response,
this.message,
});

factory ProfileEditResponse.fromJson(Map<String, dynamic> json) {
return ProfileEditResponse(
success: json['success'] ?? false,
response: json['response'] != null
? ProfileData.fromJson(json['response'])
: null,
message: json['message'],
);
}
}

class ProfileData {
final String nickname;
final String myIntro;
final String email;
final String? imageUrl;

ProfileData({
required this.nickname,
required this.myIntro,
required this.email,
this.imageUrl,
});

factory ProfileData.fromJson(Map<String, dynamic> json) {
return ProfileData(
nickname: json['nickname'],
myIntro: json['myIntro'],
email: json['email'],
imageUrl: json['imageUrl'],
);
}
}
65 changes: 58 additions & 7 deletions outfoot/lib/screens/edit_profile_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:outfoot/colors/colors.dart';
import 'package:outfoot/widgets/dashed_line_painter.dart';
import 'package:outfoot/api/profile_edit_api.dart';
import 'package:outfoot/models/profile_edit_model.dart';

class EditProfile extends StatelessWidget {
class EditProfile extends StatefulWidget {
const EditProfile({super.key});

@override
_EditProfileState createState() => _EditProfileState();
}

class _EditProfileState extends State<EditProfile> {
final ProfileEditApi _profileEditApi = ProfileEditApi();
final TextEditingController _nicknameController = TextEditingController();
final TextEditingController _introController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
bool _isLoading = false;

TextStyle _textStyle(double fontSize, FontWeight fontWeight, Color color,
double letterSpacing) {
return TextStyle(
Expand Down Expand Up @@ -37,6 +50,40 @@ class EditProfile extends StatelessWidget {
);
}

Future<void> _updateProfile() async {

setState(() {
_isLoading = true;
});

try {
final response = await _profileEditApi.updateProfile(
nickname: _nicknameController.text,
myIntro: _introController.text,
email: _emailController.text,
token: 'user-access-token',
);

if (response.success) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('프로필이 성공적으로 수정되었습니다!')),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('수정 실패: ${response.message}')),
);
}
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('오류 발생: $e')),
);
} finally {
setState(() {
_isLoading = false;
});
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -231,14 +278,18 @@ class EditProfile extends StatelessWidget {
height: 45.994,
decoration: _boxDecoration(mainBrownColor),
child: Center(
child: Text(
'변경하기',
textAlign: TextAlign.center,
style: _textStyle(
14.0, FontWeight.w600, lightMainColor, -0.28),
),
child: TextButton(
onPressed: _isLoading ? null : _updateProfile, // 로딩 중 버튼 비활성화
child: _isLoading
? CircularProgressIndicator(color: Colors.white) // 로딩 표시 추가
: Text(
'변경하기',
textAlign: TextAlign.center,
style: _textStyle(14.0, FontWeight.w600, lightMainColor, -0.28),
),
),
),
),
],
),
),
Expand Down