-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from CtrI-Alt-Del/supplierComponent
[Front]: ✨ Feat: Supplier select component in product form
- Loading branch information
Showing
7 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
81 changes: 81 additions & 0 deletions
81
apps/web/src/ui/components/commons/supplier-select/index.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Button, Pagination, Spinner } from '@nextui-org/react' | ||
import { Icon } from '@/ui/components/commons/icon' | ||
import { Select } from '@/ui/components/commons/select' | ||
import { Dialog } from '../dialog' | ||
import { useSupplierSelect } from './use-supplier-select' | ||
|
||
type SupplierSelectProps = { | ||
defeaultSupplierId?: string | ||
className?: string | ||
onSelectChange: (supplierId: string) => void | ||
} | ||
|
||
export const SupplierSelect = ({ | ||
className, | ||
defeaultSupplierId, | ||
onSelectChange, | ||
}: SupplierSelectProps) => { | ||
const { | ||
suppliers, | ||
isFetching, | ||
page, | ||
totalPages, | ||
selectedSupplierName, | ||
handleSupplierIdChange, | ||
handleSupplierPageChange, | ||
} = useSupplierSelect(onSelectChange, defeaultSupplierId) | ||
|
||
return isFetching ? ( | ||
<Spinner label='Carregando...' className='w-full h-full mx-auto' /> | ||
) : ( | ||
<div className='space-y-2'> | ||
<Dialog | ||
title='Selecione um fornecedor' | ||
size='2xl' | ||
trigger={ | ||
<Select className={className}> | ||
{selectedSupplierName ? selectedSupplierName : 'Selecione fornecedor'} | ||
</Select> | ||
} | ||
> | ||
{(closeDrawer) => | ||
suppliers.length === 0 ? ( | ||
<p className='text-center text-bg-zinc-600 font-semibold my-12'> | ||
Nenhum fornecedor registrado. | ||
</p> | ||
) : ( | ||
<> | ||
<div className='space-y-4'> | ||
{suppliers.map((supplier) => ( | ||
<div | ||
key={supplier.id} | ||
className='flex items-center justify-between p-4 border border-zinc-300 rounded-lg' | ||
> | ||
<span className='font-medium text-zinc-800'>{supplier.name}</span> | ||
<Button | ||
className='bg-transparent hover:bg-primary hover:text-white duration-1000 border-zinc-400 h-10 min-w-10' | ||
onClick={() => { | ||
handleSupplierIdChange(supplier.id) | ||
closeDrawer() | ||
}} | ||
> | ||
<Icon name='plus' size={18} /> | ||
</Button> | ||
</div> | ||
))} | ||
</div> | ||
{totalPages !== 1 && ( | ||
<Pagination | ||
page={page} | ||
total={totalPages} | ||
onChange={handleSupplierPageChange} | ||
showControls | ||
/> | ||
)} | ||
</> | ||
) | ||
} | ||
</Dialog> | ||
</div> | ||
) | ||
} |
85 changes: 85 additions & 0 deletions
85
apps/web/src/ui/components/commons/supplier-select/use-supplier-select.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { CACHE } from '@/constants' | ||
import { useApi, useCache, useToast, useUrlParamNumber } from '@/ui/hooks' | ||
import { Supplier } from '@stocker/core/entities' | ||
import { useEffect, useState } from 'react' | ||
import { useAuthContext } from '../../contexts/auth-context' | ||
|
||
export function useSupplierSelect( | ||
onSelectChange: (supplierId: string) => void, | ||
defeaultSelectedSupplierId?: string, | ||
) { | ||
const [supplierId, setSupplierId] = useState(defeaultSelectedSupplierId) | ||
const { suppliersService } = useApi() | ||
const { company } = useAuthContext() | ||
const { showError } = useToast() | ||
const [page, setPage] = useUrlParamNumber('supplierPage', 1) | ||
const [expandedItems, setExpandedItems] = useState<{ [key: string]: boolean }>({}) | ||
|
||
function handleSupplierIdChange(supplierId: string) { | ||
setSupplierId(supplierId) | ||
onSelectChange(supplierId) | ||
} | ||
|
||
async function fetchSupplier() { | ||
if (!supplierId) return | ||
|
||
const response = await suppliersService.getSupplier(supplierId) | ||
if (response.isFailure) { | ||
showError(response.errorMessage) | ||
return | ||
} | ||
return response.body | ||
} | ||
|
||
async function fetchSuppliers() { | ||
if (!company) return | ||
|
||
const response = await suppliersService.listSuppliers({ | ||
page, | ||
companyId: company.id, | ||
}) | ||
if (response.isFailure) { | ||
showError(response.errorMessage) | ||
return | ||
} | ||
return response.body | ||
} | ||
|
||
const { data: supplierData } = useCache({ | ||
fetcher: fetchSupplier, | ||
key: CACHE.supplier.key, | ||
dependencies: [supplierId], | ||
}) | ||
|
||
const { data: suppliersData, isFetching } = useCache({ | ||
fetcher: fetchSuppliers, | ||
key: CACHE.suppliers.key, | ||
dependencies: [page], | ||
}) | ||
|
||
function handlePageChange(page: number) { | ||
setPage(page) | ||
} | ||
|
||
function handleAccordionClick(id: string) { | ||
setExpandedItems((prev) => ({ | ||
...prev, | ||
[id]: !prev[id], | ||
})) | ||
} | ||
|
||
const suppliers = suppliersData ? suppliersData.items.map(Supplier.create) : [] | ||
const itemsCount = suppliersData ? suppliersData.itemsCount : 0 | ||
|
||
return { | ||
isFetching, | ||
totalPages: Math.ceil(itemsCount / 10), | ||
page, | ||
suppliers, | ||
selectedSupplierName: supplierData?.name, | ||
expandedItems, | ||
handleSupplierIdChange, | ||
handleAccordionClick, | ||
handleSupplierPageChange: handlePageChange, | ||
} | ||
} |
This file contains 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 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 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 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