|
| 1 | +import 'package:dipantau_desktop_client/core/util/enum/sign_up_method.dart'; |
| 2 | +import 'package:dipantau_desktop_client/core/util/helper.dart'; |
| 3 | +import 'package:dipantau_desktop_client/core/util/string_extension.dart'; |
| 4 | +import 'package:dipantau_desktop_client/core/util/widget_helper.dart'; |
| 5 | +import 'package:dipantau_desktop_client/feature/data/model/kv_setting/kv_setting_body.dart'; |
| 6 | +import 'package:dipantau_desktop_client/feature/presentation/bloc/setting/setting_bloc.dart'; |
| 7 | +import 'package:dipantau_desktop_client/feature/presentation/widget/widget_custom_circular_progress_indicator.dart'; |
| 8 | +import 'package:dipantau_desktop_client/feature/presentation/widget/widget_error.dart'; |
| 9 | +import 'package:dipantau_desktop_client/feature/presentation/widget/widget_primary_button.dart'; |
| 10 | +import 'package:dipantau_desktop_client/injection_container.dart'; |
| 11 | +import 'package:easy_localization/easy_localization.dart'; |
| 12 | +import 'package:flutter/material.dart'; |
| 13 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 14 | +import 'package:go_router/go_router.dart'; |
| 15 | + |
| 16 | +class UserRegistrationSettingPage extends StatefulWidget { |
| 17 | + static const routePath = '/user-registration-setting'; |
| 18 | + static const routeName = 'user-registration-setting'; |
| 19 | + |
| 20 | + const UserRegistrationSettingPage({Key? key}) : super(key: key); |
| 21 | + |
| 22 | + @override |
| 23 | + State<UserRegistrationSettingPage> createState() => _UserRegistrationSettingPageState(); |
| 24 | +} |
| 25 | + |
| 26 | +class _UserRegistrationSettingPageState extends State<UserRegistrationSettingPage> { |
| 27 | + final settingBloc = sl<SettingBloc>(); |
| 28 | + final helper = sl<Helper>(); |
| 29 | + final widgetHelper = WidgetHelper(); |
| 30 | + |
| 31 | + var isLoadingButton = false; |
| 32 | + var isPreparingSuccess = false; |
| 33 | + SignUpMethod? signUpMethod; |
| 34 | + |
| 35 | + @override |
| 36 | + void setState(VoidCallback fn) { |
| 37 | + if (mounted) { |
| 38 | + super.setState(fn); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @override |
| 43 | + void initState() { |
| 44 | + doLoadData(); |
| 45 | + super.initState(); |
| 46 | + } |
| 47 | + |
| 48 | + void doLoadData() { |
| 49 | + settingBloc.add(LoadKvSettingEvent()); |
| 50 | + } |
| 51 | + |
| 52 | + @override |
| 53 | + Widget build(BuildContext context) { |
| 54 | + return IgnorePointer( |
| 55 | + ignoring: isLoadingButton, |
| 56 | + child: BlocProvider<SettingBloc>( |
| 57 | + create: (context) => settingBloc, |
| 58 | + child: BlocListener<SettingBloc, SettingState>( |
| 59 | + listener: (context, state) { |
| 60 | + setState(() { |
| 61 | + isLoadingButton = state is LoadingButtonSettingState; |
| 62 | + }); |
| 63 | + if (state is FailureSettingState) { |
| 64 | + final errorMessage = state.errorMessage.convertErrorMessageToHumanMessage(); |
| 65 | + if (errorMessage.contains('401')) { |
| 66 | + widgetHelper.showDialog401(context); |
| 67 | + return; |
| 68 | + } |
| 69 | + } else if (state is SuccessLoadKvSettingState) { |
| 70 | + isPreparingSuccess = true; |
| 71 | + final strSignUpMethod = state.response?.signUpMethod ?? ''; |
| 72 | + signUpMethod = SignUpMethodExtension.parseString(strSignUpMethod); |
| 73 | + signUpMethod ??= SignUpMethod.auto; |
| 74 | + } else if (state is SuccessUpdateKvSettingState) { |
| 75 | + widgetHelper.showSnackBar( |
| 76 | + context, |
| 77 | + 'user_registration_workflow_successfully_updated'.tr(), |
| 78 | + ); |
| 79 | + context.pop(); |
| 80 | + } |
| 81 | + }, |
| 82 | + child: Scaffold( |
| 83 | + appBar: AppBar( |
| 84 | + title: Text( |
| 85 | + 'user_registration_workflow'.tr(), |
| 86 | + ), |
| 87 | + centerTitle: false, |
| 88 | + ), |
| 89 | + body: Padding( |
| 90 | + padding: EdgeInsets.all(helper.getDefaultPaddingLayout), |
| 91 | + child: BlocBuilder<SettingBloc, SettingState>( |
| 92 | + buildWhen: (previousState, currentState) { |
| 93 | + return currentState is LoadingCenterSettingState || |
| 94 | + currentState is FailureSettingState || |
| 95 | + currentState is SuccessLoadKvSettingState; |
| 96 | + }, |
| 97 | + builder: (context, state) { |
| 98 | + if (state is FailureSettingState) { |
| 99 | + final errorMessage = state.errorMessage.convertErrorMessageToHumanMessage(); |
| 100 | + return WidgetError( |
| 101 | + title: 'oops'.tr(), |
| 102 | + message: errorMessage.hideResponseCode(), |
| 103 | + onTryAgain: doLoadData, |
| 104 | + ); |
| 105 | + } else if (state is LoadingCenterSettingState) { |
| 106 | + return const WidgetCustomCircularProgressIndicator(); |
| 107 | + } |
| 108 | + return !isPreparingSuccess ? Container() : buildWidgetForm(); |
| 109 | + }, |
| 110 | + ), |
| 111 | + ), |
| 112 | + ), |
| 113 | + ), |
| 114 | + ), |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + Widget buildWidgetForm() { |
| 119 | + return Column( |
| 120 | + children: [ |
| 121 | + ListTile( |
| 122 | + leading: Radio( |
| 123 | + value: SignUpMethod.auto, |
| 124 | + groupValue: signUpMethod, |
| 125 | + onChanged: (value) { |
| 126 | + setState(() { |
| 127 | + signUpMethod = value; |
| 128 | + }); |
| 129 | + }, |
| 130 | + ), |
| 131 | + title: Text('auto_approval'.tr()), |
| 132 | + titleAlignment: ListTileTitleAlignment.top, |
| 133 | + subtitle: Text( |
| 134 | + 'description_auto_approval'.tr(), |
| 135 | + style: const TextStyle( |
| 136 | + color: Colors.grey, |
| 137 | + ), |
| 138 | + ), |
| 139 | + onTap: () { |
| 140 | + setState(() { |
| 141 | + signUpMethod = SignUpMethod.auto; |
| 142 | + }); |
| 143 | + }, |
| 144 | + ), |
| 145 | + ListTile( |
| 146 | + leading: Radio( |
| 147 | + value: SignUpMethod.manual, |
| 148 | + groupValue: signUpMethod, |
| 149 | + onChanged: (value) { |
| 150 | + setState(() { |
| 151 | + signUpMethod = value; |
| 152 | + }); |
| 153 | + }, |
| 154 | + ), |
| 155 | + title: Text('manual_approval'.tr()), |
| 156 | + titleAlignment: ListTileTitleAlignment.top, |
| 157 | + subtitle: Text( |
| 158 | + 'description_manual_approval'.tr(), |
| 159 | + style: const TextStyle( |
| 160 | + color: Colors.grey, |
| 161 | + ), |
| 162 | + ), |
| 163 | + onTap: () { |
| 164 | + setState(() { |
| 165 | + signUpMethod = SignUpMethod.manual; |
| 166 | + }); |
| 167 | + }, |
| 168 | + ), |
| 169 | + Expanded( |
| 170 | + child: Container(), |
| 171 | + ), |
| 172 | + buildWidgetButtonSave(), |
| 173 | + ], |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + Widget buildWidgetButtonSave() { |
| 178 | + return SizedBox( |
| 179 | + width: double.infinity, |
| 180 | + child: WidgetPrimaryButton( |
| 181 | + onPressed: saveData, |
| 182 | + isLoading: isLoadingButton, |
| 183 | + child: Text( |
| 184 | + 'save'.tr(), |
| 185 | + ), |
| 186 | + ), |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + void saveData() { |
| 191 | + String strSignUpMethod; |
| 192 | + if (signUpMethod != null) { |
| 193 | + strSignUpMethod = signUpMethod!.toValue(); |
| 194 | + } else { |
| 195 | + widgetHelper.showSnackBar( |
| 196 | + context, |
| 197 | + 'please_choose_user_registration_workflow'.tr(), |
| 198 | + ); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + settingBloc.add( |
| 203 | + UpdateKvSettingEvent( |
| 204 | + body: KvSettingBody( |
| 205 | + discordChannelId: null, |
| 206 | + signUpMethod: strSignUpMethod, |
| 207 | + ), |
| 208 | + ), |
| 209 | + ); |
| 210 | + } |
| 211 | +} |
0 commit comments