Skip to content

Commit

Permalink
fix: edit profile error
Browse files Browse the repository at this point in the history
    - after data is updated, back button not working
    - fix metadata data type
  • Loading branch information
atnanahidiw committed Nov 10, 2023
1 parent f303124 commit 1eba2d4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:injectable/injectable.dart';
import 'package:intl/intl.dart';

import '../../../../services/firebase_service.dart';
import '../../../authentication/register/repositories/register_user_repo.dart';
import '../model/form_item.dart';

part 'user_register_event.dart';

part 'user_register_state.dart';

@injectable
Expand Down Expand Up @@ -197,7 +195,6 @@ class UserRegisterBloc extends Bloc<UserRegisterEvent, RegisterFormState> {
) async {
final auth = FirebaseAuth.instance;
final userId = auth.currentUser!.uid;
final dateFormat = DateFormat('yyyy-MM-dd HH:mm:ss');

if (state.formKey!.currentState!.validate()) {
final email = state.email.value;
Expand All @@ -218,7 +215,6 @@ class UserRegisterBloc extends Bloc<UserRegisterEvent, RegisterFormState> {
school: school,
province: province,
bebrasBiro: bebrasBiro,
createdAt: dateFormat.format(DateTime.now()),
);
emit(UserRegisterSuccessState());
} catch (e) {
Expand All @@ -228,12 +224,11 @@ class UserRegisterBloc extends Bloc<UserRegisterEvent, RegisterFormState> {
}

Future<void> _onFormUpdateSubmitted(
FormSubmitUpdateEvent event,
Emitter<RegisterFormState> emit,
) async {
FormSubmitUpdateEvent event,
Emitter<RegisterFormState> emit,
) async {
final auth = FirebaseAuth.instance;
final userId = auth.currentUser!.uid;
final dateFormat = DateFormat('yyyy-MM-dd HH:mm:ss');

if (state.formKey!.currentState!.validate()) {
final email = state.email.value;
Expand All @@ -254,7 +249,6 @@ class UserRegisterBloc extends Bloc<UserRegisterEvent, RegisterFormState> {
school: school,
province: province,
bebrasBiro: bebrasBiro,
updatedAt: dateFormat.format(DateTime.now()),
);
emit(UserRegisterSuccessState());
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:go_router/go_router.dart';

import '../../../../../core/bases/widgets/layout/bebras_scaffold.dart';
import '../../../../../core/constants/assets.dart';
import '../../../../../core/theme/font_theme.dart';
import '../../../../../services/di.dart';
import '../../bloc/user_register_bloc.dart';
import '../../model/form_item.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: lines_longer_than_80_chars

part of '_pages.dart';

class RegisterPage extends StatefulWidget {
Expand Down Expand Up @@ -32,16 +34,22 @@ class _RegisterPageState extends State<RegisterPage> {
listener: (context, state) {
if (state is UserRegisterSuccessState) {
if (widget.isUpdateProfile == 'true') {
context.go('/setting');
// refresh page
Navigator.pop(context);
Navigator.push(
context,
// ignore: inference_failure_on_instance_creation
MaterialPageRoute(
builder: (context) =>
const RegisterPage(isUpdateProfile: 'true'),
),
);

// add notification
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('Pembaruan data profil berhasil'),
action: SnackBarAction(
label: 'OK',
onPressed: () {
// Some code to undo the change.
},
),
const SnackBar(
content: Text('Pembaruan data profil berhasil'),
behavior: SnackBarBehavior.floating,
),
);
} else {
Expand Down Expand Up @@ -71,13 +79,11 @@ class _RegisterPageState extends State<RegisterPage> {
const SizedBox(
height: 40,
),
Text(
widget.isUpdateProfile == 'true'
? 'Perbarui Data'
: 'Detail Akun',
style: const TextStyle(
fontSize: 22,
fontWeight: FontWeight.w700,
Container(
padding: const EdgeInsets.all(8),
child: Text(
'Data Diri',
style: FontTheme.blackSubtitleBold(),
),
),
const SizedBox(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import 'package:injectable/injectable.dart';
import 'package:intl/intl.dart';

import '../model/registered_user.dart';

@Injectable()
class RegisterUserRepository {
final _firecloud = FirebaseFirestore.instance.collection('registered_user');
final dateFormat = DateFormat('yyyy-MM-dd HH:mm:ss');

Future<void> create({
required String userId,
Expand All @@ -18,9 +16,9 @@ class RegisterUserRepository {
required String school,
required String province,
required String bebrasBiro,
String? createdAt,
}) async {
try {
final now = DateTime.now();
await _firecloud.doc(userId).set(
{
'name': name,
Expand All @@ -29,7 +27,8 @@ class RegisterUserRepository {
'school': school,
'province': province,
'bebras_biro': bebrasBiro,
'created_at': createdAt,
'created_at': now,
'updated_at': now,
},
SetOptions(merge: true),
);
Expand All @@ -53,18 +52,15 @@ class RegisterUserRepository {
String? updatedAt,
}) async {
try {
await _firecloud.doc(userId).set(
{
'name': name,
'email': email,
'birth_date': birthDate,
'school': school,
'province': province,
'bebras_biro': bebrasBiro,
'updated_at': updatedAt,
},
SetOptions(merge: true),
);
await _firecloud.doc(userId).update({
'name': name,
'email': email,
'birth_date': birthDate,
'school': school,
'province': province,
'bebras_biro': bebrasBiro,
'updated_at': DateTime.now(),
});
} on FirebaseException catch (e) {
if (kDebugMode) {
print("Failed with error '${e.code}': '${e.message}'");
Expand Down

0 comments on commit 1eba2d4

Please sign in to comment.