Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Fix filter orders delete (#1162)
Browse files Browse the repository at this point in the history
* markedForDeleteion only filtered and currently shown

* show All Checked box based on filtered orders

* simplify classifiedOrders initialization

* simplify full state copy
  • Loading branch information
Velenir authored Jul 1, 2020
1 parent 1e336c0 commit 52c0ca0
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/components/OrdersWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const OrdersWidget: React.FC<Props> = ({ isWidget = false }) => {
const { networkId, isConnected } = useWalletConnection()

// allOrders and markedForDeletion, split by tab
const [classifiedOrders, setClassifiedOrders] = useSafeState<FilteredOrdersState>(emptyState())
const [classifiedOrders, setClassifiedOrders] = useSafeState<FilteredOrdersState>(emptyState)
const [selectedTab, setSelectedTab] = useSafeState<OrderTabs>('active')

// Subscribe to trade events
Expand Down Expand Up @@ -212,10 +212,8 @@ const OrdersWidget: React.FC<Props> = ({ isWidget = false }) => {
if (selectedTab === 'fills') return

setClassifiedOrders(curr => {
const state = emptyState()

// copy full state
Object.keys(curr).forEach(tab => (state[tab] = curr[tab]))
const state = { ...curr }

// copy markedForDeletion set
const newSet = new Set(curr[selectedTab].markedForDeletion)
Expand All @@ -235,19 +233,22 @@ const OrdersWidget: React.FC<Props> = ({ isWidget = false }) => {
if (selectedTab === 'fills') return

setClassifiedOrders(curr => {
const state = emptyState()

// copy full state
Object.keys(curr).forEach(tab => (state[tab] = curr[tab]))
const state = { ...curr }

// filteredOrders are selectedTab specific,
// so it's ok to use them directly
// without classifiedOrders[selectedTab]
state[selectedTab].markedForDeletion = checked
? new Set(classifiedOrders[selectedTab].orders.map(order => order.id))
? new Set(filteredAndSortedOrders.concat(filteredAndSortedPendingOrders).map(order => order.id))
: new Set()
// on deselect, better deselect all filtered and unfiltered
// to avoid cancelling not shown orders

return state
})
},
[classifiedOrders, selectedTab, setClassifiedOrders],
[filteredAndSortedOrders, filteredAndSortedPendingOrders, selectedTab, setClassifiedOrders],
)

const { deleteOrders, deleting } = useDeleteOrders()
Expand All @@ -265,10 +266,8 @@ const OrdersWidget: React.FC<Props> = ({ isWidget = false }) => {
// reset selections

setClassifiedOrders(curr => {
const state = emptyState()

// copy full state
Object.keys(curr).forEach(tab => (state[tab] = curr[tab]))
const state = { ...curr }

// remove checked orders
state[selectedTab].orders = curr[selectedTab].orders.filter(
Expand Down Expand Up @@ -406,7 +405,11 @@ const OrdersWidget: React.FC<Props> = ({ isWidget = false }) => {
<input
type="checkbox"
onChange={toggleSelectAll}
checked={markedForDeletion.size === displayedOrders.length}
checked={
markedForDeletion.size > 0 &&
markedForDeletion.size ===
filteredAndSortedPendingOrders.length + filteredAndSortedOrders.length
}
disabled={deleting}
/>
</th>
Expand Down

0 comments on commit 52c0ca0

Please sign in to comment.