88 lines
2.3 KiB
Dart
88 lines
2.3 KiB
Dart
import 'package:equatable/equatable.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../data/models/automation_job_model.dart';
|
|
import '../../data/apis/automation_jobs_api.dart';
|
|
|
|
class JobDetailState extends Equatable {
|
|
final AutomationJobModel? job;
|
|
final bool isLoading;
|
|
final bool isSaving;
|
|
final String? error;
|
|
|
|
const JobDetailState({
|
|
this.job,
|
|
this.isLoading = false,
|
|
this.isSaving = false,
|
|
this.error,
|
|
});
|
|
|
|
JobDetailState copyWith({
|
|
AutomationJobModel? job,
|
|
bool? isLoading,
|
|
bool? isSaving,
|
|
String? error,
|
|
}) {
|
|
return JobDetailState(
|
|
job: job ?? this.job,
|
|
isLoading: isLoading ?? this.isLoading,
|
|
isSaving: isSaving ?? this.isSaving,
|
|
error: error,
|
|
);
|
|
}
|
|
|
|
@override
|
|
List<Object?> get props => [job, isLoading, isSaving, error];
|
|
}
|
|
|
|
class JobDetailCubit extends Cubit<JobDetailState> {
|
|
final AutomationJobsApi _api;
|
|
|
|
JobDetailCubit(this._api) : super(JobDetailState());
|
|
|
|
Future<void> loadJob(String id) async {
|
|
emit(state.copyWith(isLoading: true));
|
|
try {
|
|
final job = await _api.get(id);
|
|
emit(state.copyWith(job: job, isLoading: false));
|
|
} catch (e) {
|
|
emit(state.copyWith(isLoading: false, error: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<bool> updateJob(String id, AutomationJobUpdateRequest request) async {
|
|
emit(state.copyWith(isSaving: true));
|
|
try {
|
|
final job = await _api.update(id, request);
|
|
emit(state.copyWith(job: job, isSaving: false));
|
|
return true;
|
|
} catch (e) {
|
|
emit(state.copyWith(isSaving: false, error: e.toString()));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> deleteJob(String id) async {
|
|
emit(state.copyWith(isSaving: true, error: null));
|
|
try {
|
|
await _api.delete(id);
|
|
emit(state.copyWith(isSaving: false));
|
|
return true;
|
|
} catch (e) {
|
|
emit(state.copyWith(isSaving: false, error: e.toString()));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> createJob(AutomationJobCreateRequest request) async {
|
|
emit(state.copyWith(isSaving: true, error: null));
|
|
try {
|
|
final job = await _api.create(request);
|
|
emit(state.copyWith(job: job, isSaving: false));
|
|
return true;
|
|
} catch (e) {
|
|
emit(state.copyWith(isSaving: false, error: e.toString()));
|
|
return false;
|
|
}
|
|
}
|
|
}
|