Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
feat: enforce sku selection page to hide already selected items
Browse files Browse the repository at this point in the history
  • Loading branch information
pfferrari committed Feb 28, 2024
1 parent 84644d0 commit 4e08529
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
17 changes: 8 additions & 9 deletions packages/app/src/components/SkuListForm/SkuListForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ export function SkuListForm({
return watchedFormManual ? 0 : 1
}, [watchedFormManual])

// const selectedItemsCodes = useMemo(() => {
// const excludedCodes = [] as string[]
// watchedFormItems?.forEach((sku) => {
// excludedCodes.push(sku.sku_code)
// })
// return excludedCodes.join(',') ?? ''
// }, [watchedFormItems])
// console.log(selectedItemsCodes)
const selectedItemsCodes = useMemo(() => {
const excludedCodes = [] as string[]
watchedFormItems?.forEach((sku) => {
excludedCodes.push(sku.sku_code)
})
return excludedCodes
}, [watchedFormItems])

useEffect(() => {
if (resource != null) {
Expand Down Expand Up @@ -164,7 +163,7 @@ export function SkuListForm({
padding='4'
fullWidth
onClick={() => {
showAddItemOverlay('')
showAddItemOverlay(selectedItemsCodes)
}}
/>
</Spacer>
Expand Down
59 changes: 37 additions & 22 deletions packages/app/src/hooks/useAddItemOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,54 @@ import { useState } from 'react'
import { navigate, useSearch } from 'wouter/use-browser-location'

interface OverlayHook {
show: (excludedIds?: string) => void
show: (excludedCodes?: string[]) => void
Overlay: React.FC<{ onConfirm: (resource: Sku) => void }>
}

export function useAddItemOverlay(): OverlayHook {
const { Overlay: OverlayElement, open, close } = useOverlay()
const [excludedIds, setExcludedIds] = useState<string>('')

const instructions: FiltersInstructions = [
{
label: 'Search',
type: 'textSearch',
sdk: {
predicate: ['name', 'code'].join('_or_') + '_cont'
},
render: {
component: 'searchBar'
}
}
]
const [excludedCodes, setExcludedCodes] = useState<string[]>([])

return {
show: (excludedIds) => {
console.log(excludedIds)
if (excludedIds != null) {
setExcludedIds(excludedIds)
show: (excludedCodes) => {
if (excludedCodes != null) {
setExcludedCodes(excludedCodes)
}
open()
},
Overlay: ({ onConfirm }) => {
// filters: { code_not_in: excludedCodes }

const instructions: FiltersInstructions = [
{
label: 'Already selected items',
type: 'options',
sdk: {
predicate: 'code_not_in',
defaultOptions: excludedCodes
},
render: {
component: 'inputToggleButton',
props: {
mode: 'single',
options: excludedCodes.map((code) => {
return { value: code, label: code }
})
}
}
},
{
label: 'Search',
type: 'textSearch',
sdk: {
predicate: ['name', 'code'].join('_or_') + '_cont'
},
render: {
component: 'searchBar'
}
}
]

const queryString = useSearch()
const { SearchWithNav, FilteredList, hasActiveFilter } =
useResourceFilters({
Expand Down Expand Up @@ -89,9 +107,6 @@ export function useAddItemOverlay(): OverlayHook {
<Card gap='none'>
<FilteredList
type='skus'
query={{
filters: { id_eq: excludedIds }
}}
ItemTemplate={(props) => (
<ListItemSku
onSelect={(resource) => {
Expand Down
3 changes: 0 additions & 3 deletions packages/app/src/hooks/useUpdateSkuList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export function useUpdateSkuList(): UpdateSkuListHook {
id: itemNeedsUpdate.id,
quantity: item.quantity
})
console.log('updated sku list item', item)
}
const itemIsAlreadyExisting = updatedSkuList.sku_list_items?.find(
(skuListItem) => skuListItem.sku_code === item.sku_code
Expand All @@ -57,7 +56,6 @@ export function useUpdateSkuList(): UpdateSkuListHook {
sdkClient
)
await sdkClient.sku_list_items.create(skuListItem)
console.log('created sku list item', skuListItem)
}
})
)
Expand All @@ -70,7 +68,6 @@ export function useUpdateSkuList(): UpdateSkuListHook {
)
if (itemIsInNewListItems == null) {
await sdkClient.sku_list_items.delete(oldItem.id)
console.log('deleted sku list item', oldItem.id)
}
})
)
Expand Down

0 comments on commit 4e08529

Please sign in to comment.