saveProject method

Future<void> saveProject()

Implementation

Future<void> saveProject() async {
  if (state.projectName.isEmpty) {
    emit(state.copyWith(errorMessage: "El nombre es obligatorio"));
    return;
  }

  emit(
      state.copyWith(isSaving: true, errorMessage: null, saveSuccess: false));

  final project = Project(
    id: state.projectId,
    name: state.projectName,
    client: state.client,
    reference: state.reference,
    ownerPhone: state.ownerPhone,
    ownerEmail: state.ownerEmail,
    createdAt: DateTime
        .now(), // Should preserve if editing? Mapping handles it usually
    updatedAt: DateTime.now(),
    root: state.root,
    budgetConfig: state.budgetConfig,
    electricalStandardId: state.electricalStandardId,
    supplyVoltage: state.supplyVoltage,
    installationUsage: state.installationUsage,
    expectedPower: state.expectedPower,
    powerFactor: state.powerFactor,
    requiresTechProject: state.requiresTechProject,
    isNewLink: state.isNewLink,
  );

  final result = await _repository.saveProject(project);

  result.fold(
    (failure) => emit(state.copyWith(
        isSaving: false, errorMessage: "Error al guardar el proyecto")),
    (id) => emit(
        state.copyWith(isSaving: false, saveSuccess: true, projectId: id)),
  );
}