-
Notifications
You must be signed in to change notification settings - Fork 120
Update order status in dashboard card #16442
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
Open
RafaelKayumov
wants to merge
3
commits into
trunk
Choose a base branch
from
WOOMOB-47-update-order-status-in-dashboard-card
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import Foundation | ||
| import CoreData | ||
| import Yosemite | ||
| import protocol Storage.StorageManagerType | ||
| import protocol WooFoundation.Analytics | ||
|
|
@@ -89,6 +90,8 @@ final class LastOrdersDashboardCardViewModel: ObservableObject { | |
| @Published private(set) var rows: [LastOrderDashboardRowViewModel] = [] | ||
| @Published private(set) var allStatuses: [OrderStatusRow] = [] | ||
|
|
||
| private var orderEntityListeners: [Int64: EntityListener<Order>] = [:] | ||
|
|
||
| var status: String { | ||
| selectedOrderStatus?.description ?? Localization.anyStatusCase | ||
| } | ||
|
|
@@ -127,12 +130,15 @@ final class LastOrdersDashboardCardViewModel: ObservableObject { | |
| syncingData = true | ||
| syncingError = nil | ||
| rows = [] | ||
| tearDownOrderEntityListeners() | ||
|
|
||
| do { | ||
| async let orders = loadLast3Orders(for: selectedOrderStatus) | ||
| try? await loadOrderStatuses() | ||
| rows = try await orders | ||
| let fetchedOrders = try await orders | ||
| rows = fetchedOrders | ||
| .map { LastOrderDashboardRowViewModel(order: $0) } | ||
| setupOrderEntityListeners(for: fetchedOrders) | ||
| analytics.track(event: .DynamicDashboard.cardLoadingCompleted(type: .lastOrders)) | ||
| } catch { | ||
| syncingError = error | ||
|
|
@@ -241,6 +247,48 @@ private extension LastOrdersDashboardCardViewModel { | |
| } | ||
| } | ||
|
|
||
| // MARK: Orders observing | ||
| // | ||
| private extension LastOrdersDashboardCardViewModel { | ||
| func setupOrderEntityListeners(for orders: [Order]) { | ||
| tearDownOrderEntityListeners() | ||
| guard let viewContext = storageManager.viewStorage as? NSManagedObjectContext else { | ||
| return | ||
| } | ||
|
|
||
| for order in orders { | ||
| let listener = EntityListener(viewContext: viewContext, readOnlyEntity: order) | ||
| listener.onUpsert = { updatedOrder in | ||
| Task { @MainActor [weak self] in | ||
| self?.updateRow(with: updatedOrder) | ||
| } | ||
| } | ||
| listener.onDelete = { | ||
| Task { @MainActor [weak self] in | ||
| self?.removeRow(with: order.orderID) | ||
| } | ||
| } | ||
| orderEntityListeners[order.orderID] = listener | ||
| } | ||
| } | ||
|
|
||
| func tearDownOrderEntityListeners() { | ||
| orderEntityListeners.removeAll() | ||
| } | ||
|
|
||
| func updateRow(with order: Order) { | ||
| guard let index = rows.firstIndex(where: { $0.id == order.orderID }) else { | ||
| return | ||
| } | ||
| rows[index] = LastOrderDashboardRowViewModel(order: order) | ||
| } | ||
|
|
||
| func removeRow(with orderID: Int64) { | ||
| rows.removeAll { $0.id == orderID } | ||
| orderEntityListeners[orderID] = nil | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment as above - we should reload the card to get 3 last orders. If all 3 orders are trashed, the card will be left with no item even though there might be more. |
||
| } | ||
| } | ||
|
|
||
| // MARK: Constants | ||
| // | ||
| private extension LastOrdersDashboardCardViewModel { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check only for relevant updates? Changes like custom fields and customer notes should not affect the row on the card. Maybe check for changes in status, customer name, and cost - basically things that are displayed on the card only?
Also, should we reload the card to get the latest orders if status is changed? Otherwise we'll show orders with mismatched status like this:
Simulator.Screen.Recording.-.iPhone.17.-.2025-12-11.at.09.33.03.mov