Add convert quote to project action

This commit is contained in:
Hillel Coren 2022-08-29 14:00:50 +03:00
parent b5d9949fd6
commit 0427a9b7a5
3 changed files with 63 additions and 5 deletions

View File

@ -434,6 +434,25 @@ class ConvertQuotesToInvoicesFailure implements StopSaving {
final dynamic error; final dynamic error;
} }
class ConvertQuotesToProjects implements StartSaving {
ConvertQuotesToProjects(this.completer, this.quoteIds);
final List<String> quoteIds;
final Completer completer;
}
class ConvertQuotesToProjectsSuccess implements StopSaving {
ConvertQuotesToProjectsSuccess({this.quotes});
final List<InvoiceEntity> quotes;
}
class ConvertQuotesToProjectsFailure implements StopSaving {
ConvertQuotesToProjectsFailure(this.error);
final dynamic error;
}
class ApproveQuotes implements StartSaving { class ApproveQuotes implements StartSaving {
ApproveQuotes(this.completer, this.quoteIds); ApproveQuotes(this.completer, this.quoteIds);
@ -500,6 +519,11 @@ Future handleQuoteAction(
snackBarCompleter<Null>(context, localization.convertedQuote), snackBarCompleter<Null>(context, localization.convertedQuote),
quoteIds)); quoteIds));
break; break;
case EntityAction.convertToProject:
store.dispatch(ConvertQuotesToProjects(
snackBarCompleter<Null>(context, localization.convertedQuote),
quoteIds));
break;
case EntityAction.approve: case EntityAction.approve:
final message = quoteIds.length > 1 final message = quoteIds.length > 1
? localization.approvedQuotes ? localization.approvedQuotes

View File

@ -27,7 +27,8 @@ List<Middleware<AppState>> createStoreQuotesMiddleware([
final editQuote = _editQuote(); final editQuote = _editQuote();
final showEmailQuote = _showEmailQuote(); final showEmailQuote = _showEmailQuote();
final showPdfQuote = _showPdfQuote(); final showPdfQuote = _showPdfQuote();
final convertQuote = _convertQuote(repository); final convertQuotesToInvoices = _convertQuotesToInvoices(repository);
final convertQuotesToProjects = _convertQuotesToProjects(repository);
final approveQuote = _approveQuote(repository); final approveQuote = _approveQuote(repository);
final loadQuotes = _loadQuotes(repository); final loadQuotes = _loadQuotes(repository);
final loadQuote = _loadQuote(repository); final loadQuote = _loadQuote(repository);
@ -45,7 +46,8 @@ List<Middleware<AppState>> createStoreQuotesMiddleware([
TypedMiddleware<AppState, ViewQuoteList>(viewQuoteList), TypedMiddleware<AppState, ViewQuoteList>(viewQuoteList),
TypedMiddleware<AppState, ViewQuote>(viewQuote), TypedMiddleware<AppState, ViewQuote>(viewQuote),
TypedMiddleware<AppState, EditQuote>(editQuote), TypedMiddleware<AppState, EditQuote>(editQuote),
TypedMiddleware<AppState, ConvertQuotesToInvoices>(convertQuote), TypedMiddleware<AppState, ConvertQuotesToInvoices>(convertQuotesToInvoices),
TypedMiddleware<AppState, ConvertQuotesToProjects>(convertQuotesToProjects),
TypedMiddleware<AppState, ApproveQuotes>(approveQuote), TypedMiddleware<AppState, ApproveQuotes>(approveQuote),
TypedMiddleware<AppState, ShowEmailQuote>(showEmailQuote), TypedMiddleware<AppState, ShowEmailQuote>(showEmailQuote),
TypedMiddleware<AppState, ShowPdfQuote>(showPdfQuote), TypedMiddleware<AppState, ShowPdfQuote>(showPdfQuote),
@ -223,7 +225,7 @@ Middleware<AppState> _restoreQuote(QuoteRepository repository) {
}; };
} }
Middleware<AppState> _convertQuote(QuoteRepository repository) { Middleware<AppState> _convertQuotesToInvoices(QuoteRepository repository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) { return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as ConvertQuotesToInvoices; final action = dynamicAction as ConvertQuotesToInvoices;
repository repository
@ -243,6 +245,26 @@ Middleware<AppState> _convertQuote(QuoteRepository repository) {
}; };
} }
Middleware<AppState> _convertQuotesToProjects(QuoteRepository repository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as ConvertQuotesToProjects;
repository
.bulkAction(store.state.credentials, action.quoteIds,
EntityAction.convertToProject)
.then((quotes) {
store.dispatch(ConvertQuotesToProjectsSuccess(quotes: quotes));
store.dispatch(RefreshData());
action.completer.complete(null);
}).catchError((Object error) {
print(error);
store.dispatch(ConvertQuotesToProjectsFailure(error));
action.completer.completeError(error);
});
next(action);
};
}
Middleware<AppState> _approveQuote(QuoteRepository repository) { Middleware<AppState> _approveQuote(QuoteRepository repository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) { return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as ApproveQuotes; final action = dynamicAction as ApproveQuotes;

View File

@ -307,7 +307,9 @@ final quotesReducer = combineReducers<QuoteState>([
TypedReducer<QuoteState, DeleteQuotesSuccess>(_deleteQuoteSuccess), TypedReducer<QuoteState, DeleteQuotesSuccess>(_deleteQuoteSuccess),
TypedReducer<QuoteState, RestoreQuotesSuccess>(_restoreQuoteSuccess), TypedReducer<QuoteState, RestoreQuotesSuccess>(_restoreQuoteSuccess),
TypedReducer<QuoteState, ConvertQuotesToInvoicesSuccess>( TypedReducer<QuoteState, ConvertQuotesToInvoicesSuccess>(
_convertQuoteSuccess), _convertQuotesToInvoicesSuccess),
TypedReducer<QuoteState, ConvertQuotesToProjectsSuccess>(
_convertQuotesToProjectsSuccess),
]); ]);
QuoteState _markSentQuoteSuccess( QuoteState _markSentQuoteSuccess(
@ -351,7 +353,7 @@ QuoteState _emailQuoteSuccess(QuoteState quoteState, EmailQuoteSuccess action) {
return quoteState.rebuild((b) => b..map[action.quote.id] = action.quote); return quoteState.rebuild((b) => b..map[action.quote.id] = action.quote);
} }
QuoteState _convertQuoteSuccess( QuoteState _convertQuotesToInvoicesSuccess(
QuoteState quoteState, ConvertQuotesToInvoicesSuccess action) { QuoteState quoteState, ConvertQuotesToInvoicesSuccess action) {
final quoteMap = Map<String, InvoiceEntity>.fromIterable( final quoteMap = Map<String, InvoiceEntity>.fromIterable(
action.quotes, action.quotes,
@ -361,6 +363,16 @@ QuoteState _convertQuoteSuccess(
return quoteState.rebuild((b) => b..map.addAll(quoteMap)); return quoteState.rebuild((b) => b..map.addAll(quoteMap));
} }
QuoteState _convertQuotesToProjectsSuccess(
QuoteState quoteState, ConvertQuotesToProjectsSuccess action) {
final quoteMap = Map<String, InvoiceEntity>.fromIterable(
action.quotes,
key: (dynamic item) => item.id,
value: (dynamic item) => item,
);
return quoteState.rebuild((b) => b..map.addAll(quoteMap));
}
QuoteState _addQuote(QuoteState quoteState, AddQuoteSuccess action) { QuoteState _addQuote(QuoteState quoteState, AddQuoteSuccess action) {
return quoteState.rebuild((b) => b return quoteState.rebuild((b) => b
..map[action.quote.id] = action.quote ..map[action.quote.id] = action.quote