103 lines
2.3 KiB
Dart
103 lines
2.3 KiB
Dart
import 'package:mobx/mobx.dart';
|
|||
|
|
import '../../../dashboard/data/models/doctor_info_model.dart';
|
||
|
|
import '../../data/services/config_service.dart';
|
||
|
|
|
||
|
|
part 'config_store.g.dart';
|
||
|
|
|
||
|
|
// ignore: library_private_types_in_public_api
|
||
|
|
class ConfigStore = _ConfigStoreBase with _$ConfigStore;
|
||
|
|
|
||
|
|
abstract class _ConfigStoreBase with Store {
|
||
|
|
final ConfigService service;
|
||
|
|
|
||
|
|
_ConfigStoreBase({ConfigService? configService})
|
||
|
|
: service = configService ?? ConfigService();
|
||
|
|
|
||
|
|
@observable
|
||
|
|
int? id;
|
||
|
|
|
||
|
|
@observable
|
||
|
|
bool isLoading = false;
|
||
|
|
|
||
|
|
@observable
|
||
|
|
bool isSaving = false;
|
||
|
|
|
||
|
|
@observable
|
||
|
|
String? errorMessage;
|
||
|
|
|
||
|
|
@observable
|
||
|
|
String? successMessage;
|
||
|
|
|
||
|
|
@observable
|
||
|
|
String nombreDoctor = '';
|
||
|
|
|
||
|
|
@observable
|
||
|
|
String especialidad = '';
|
||
|
|
|
||
|
|
@observable
|
||
|
|
String descripEspecialidad = '';
|
||
|
|
|
||
|
|
@observable
|
||
|
|
String descripGeneral = '';
|
||
|
|
|
||
|
|
@computed
|
||
|
|
bool get isValid => nombreDoctor.trim().isNotEmpty && especialidad.trim().isNotEmpty;
|
||
|
|
|
||
|
|
@action
|
||
|
|
void initFromModel(DoctorInfoModel model) {
|
||
|
|
id = model.id;
|
||
|
|
nombreDoctor = model.nombreDoctor;
|
||
|
|
especialidad = model.especialidad;
|
||
|
|
descripEspecialidad = model.descripEspecialidad;
|
||
|
|
descripGeneral = model.descripGeneral;
|
||
|
|
errorMessage = null;
|
||
|
|
successMessage = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
@action
|
||
|
|
Future<void> loadConfig() async {
|
||
|
|
isLoading = true;
|
||
|
|
errorMessage = null;
|
||
|
|
try {
|
||
|
|
final config = await service.getConfig();
|
||
|
|
initFromModel(config);
|
||
|
|
} catch (e) {
|
||
|
|
errorMessage = e.toString();
|
||
|
|
} finally {
|
||
|
|
isLoading = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@action
|
||
|
|
Future<DoctorInfoModel?> saveConfig() async {
|
||
|
|
if (!isValid) {
|
||
|
|
errorMessage = 'El nombre del doctor y la especialidad son obligatorios.';
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
isSaving = true;
|
||
|
|
errorMessage = null;
|
||
|
|
successMessage = null;
|
||
|
|
|
||
|
|
try {
|
||
|
|
final modelToSave = DoctorInfoModel(
|
||
|
|
id: id,
|
||
|
|
nombreDoctor: nombreDoctor.trim(),
|
||
|
|
especialidad: especialidad.trim(),
|
||
|
|
descripEspecialidad: descripEspecialidad.trim(),
|
||
|
|
descripGeneral: descripGeneral.trim(),
|
||
|
|
);
|
||
|
|
|
||
|
|
final updated = await service.updateConfig(modelToSave);
|
||
|
|
initFromModel(updated);
|
||
|
|
successMessage = 'Configuración guardada exitosamente';
|
||
|
|
return updated;
|
||
|
|
} catch (e) {
|
||
|
|
errorMessage = e.toString().replaceAll('Exception: ', '');
|
||
|
|
return null;
|
||
|
|
} finally {
|
||
|
|
isSaving = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|