Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: adjust proposal creation #375

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ui/proposals/creation/components/dao_selection_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class _DaoSelectionViewState extends State<DaoSelectionView> {
daos = GetIt.I.get<ProposalsBloc>().daos;
final ProposalCreationBloc proposalCreationBloc =
context.read<ProposalCreationBloc>();
if (proposalCreationBloc.state.proposal?.dao == null) {
if (proposalCreationBloc.state.proposal.dao == null) {
selectedDaoIndexNotifier = ValueNotifier<int>(0);
proposalCreationBloc.add(
ProposalCreationEvent.updateProposal(
Expand All @@ -35,7 +35,7 @@ class _DaoSelectionViewState extends State<DaoSelectionView> {
);
} else {
selectedDaoIndexNotifier = ValueNotifier<int>(
daos.indexOf(proposalCreationBloc.state.proposal!.dao!));
daos.indexOf(proposalCreationBloc.state.proposal.dao!));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class _OutcomeSelectionViewState extends State<OutcomeSelectionView> {
void initState() {
super.initState();
final ProposalCreationBloc proposalCreationBloc = context.read<ProposalCreationBloc>();
if (proposalCreationBloc.state.proposal?.type == null) {
if (proposalCreationBloc.state.proposal.type == null) {
selectedTypeIndexNotifier = ValueNotifier<int>(0);
context.read<ProposalCreationBloc>().add(
ProposalCreationEvent.updateProposal(
Expand All @@ -51,7 +51,7 @@ class _OutcomeSelectionViewState extends State<OutcomeSelectionView> {
);
} else {
selectedTypeIndexNotifier = ValueNotifier<int>(
outcomeTypes.indexWhere((outcome) => outcome.type.label == proposalCreationBloc.state.proposal?.type.name));
outcomeTypes.indexWhere((outcome) => outcome.type.label == proposalCreationBloc.state.proposal.type.name));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_quill/flutter_quill.dart';
import 'package:flutter_quill/quill_delta.dart';
import 'package:get/get_utils/src/extensions/context_extensions.dart';
import 'package:hypha_wallet/design/hypha_colors.dart';
import 'package:hypha_wallet/design/themes/extensions/theme_extension_provider.dart';
Expand All @@ -27,6 +28,13 @@ class _ProposalContentViewState extends State<ProposalContentView> {
void initState() {
super.initState();

_titleController.text = context.read<ProposalCreationBloc>().state.proposal.title ?? '';
final String? details = context.read<ProposalCreationBloc>().state.proposal.details;
if (details!= null) {
final List<dynamic> jsonData = jsonDecode(details);
_quillController.document = Document.fromDelta(Delta.fromJson(jsonData));
}

_titleController.addListener(() {
context.read<ProposalCreationBloc>().add(ProposalCreationEvent.updateProposal({'title': _titleController.text.isEmpty ? null : _titleController.text}));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class ProposalReviewView extends StatelessWidget {
child: IntrinsicHeight(
child: BlocBuilder<ProposalCreationBloc, ProposalCreationState>(
builder: (context, state) {
final List<dynamic> jsonData = jsonDecode(state.proposal!.details!);
final List<dynamic> jsonData = jsonDecode(state.proposal.details!);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

details! is still an issue - but I don't want to hold up the PR over this.

I think it's always defined.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All proposal fields start as null except for type, which defaults to policy. As fields are progressively filled across views, they are all non-null by the review view.

final Document document = Document.fromDelta(Delta.fromJson(jsonData));

return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 20),
ProposalHeader(
state.proposal!.dao,
state.proposal.dao,
text: 'Builders',
),
const Padding(
Expand All @@ -53,7 +53,7 @@ class ProposalReviewView extends StatelessWidget {
Padding(
padding: const EdgeInsets.only(top: 10, bottom: 20),
child: Text(
state.proposal!.title!,
state.proposal.title!,
style: context.hyphaTextTheme.mediumTitles,
),
),
Expand Down
27 changes: 9 additions & 18 deletions lib/ui/proposals/creation/interactor/proposal_creation_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,22 @@ class ProposalCreationBloc extends Bloc<ProposalCreationEvent, ProposalCreationS
final PublishProposalUseCase _publishProposalUseCase;
final ErrorHandlerManager _errorHandlerManager;

ProposalCreationBloc(this.daos, this._publishProposalUseCase, this._errorHandlerManager) : super(ProposalCreationState(proposal: ProposalCreationModel())) {
on<_Initialize>(_initialize);
ProposalCreationBloc(this.daos, this._publishProposalUseCase, this._errorHandlerManager) : super(ProposalCreationState.initial()) {
on<_UpdateCurrentView>(_updateCurrentView);
on<_UpdateProposal>(_updateProposal);
on<_PublishProposal>(_publishProposal);
on<_ClearPageCommand>((_, emit) => emit(state.copyWith(command: null)));

_pageController = PageController(initialPage: daos.length > 1 ? 0 : 1);
if (daos.length == 1) {
add(ProposalCreationEvent.updateProposal({'dao': daos.first}));
}
}

late final PageController _pageController;

PageController get pageController => _pageController;

void _initialize(_Initialize event, Emitter<ProposalCreationState> emit) {

if (daos.length == 1) {
emit(state.copyWith(proposal: state.proposal!.copyWith({'dao': daos.first})));
}
}

void _updateCurrentView(_UpdateCurrentView event, Emitter<ProposalCreationState> emit) {
if (event.nextViewIndex == -1) {
emit(state.copyWith(command: const PageCommand.navigateBackToProposals()));
Expand All @@ -50,15 +45,15 @@ class ProposalCreationBloc extends Bloc<ProposalCreationEvent, ProposalCreationS
navigate(emit, event.nextViewIndex);
break;
case 2:
if (state.proposal!.title != null && state.proposal!.details != null) {
if (state.proposal.title != null && state.proposal.details != null) {
navigate(emit, event.nextViewIndex);
}
break;
// TODO(Zied): refactor this
/* case 3:
navigate(
emit,
state.proposal!.type == OutcomeType.agreement.label
state.proposal.type == OutcomeType.agreement.label
? event.nextViewIndex + 1
: event.nextViewIndex);
break; */
Expand All @@ -77,19 +72,15 @@ class ProposalCreationBloc extends Bloc<ProposalCreationEvent, ProposalCreationS
);
}

void _updateProposalFields(updates, emit) {
final ProposalCreationModel proposal = state.proposal!.copyWith(updates);
emit(state.copyWith(proposal: proposal));
}

void _updateProposal(_UpdateProposal event, Emitter<ProposalCreationState> emit) {
_updateProposalFields(event.updates, emit);
final ProposalCreationModel proposal = state.proposal.copyWith(event.updates);
emit(state.copyWith(proposal: proposal));
}

Future<void> _publishProposal(_PublishProposal event, Emitter<ProposalCreationState> emit) async {
emit(state.copyWith(pageState: PageState.loading));

final Result<String, HyphaError> result = await _publishProposalUseCase.run(state.proposal!);
final Result<String, HyphaError> result = await _publishProposalUseCase.run(state.proposal);

if (result.isValue) {
emit(state.copyWith(pageState: PageState.success, command: const PageCommand.navigateToSuccessPage()));
Expand Down
Loading
Loading