Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnPetros committed Nov 30, 2024
2 parents 9d62f98 + 37156fc commit 613918b
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 21 deletions.
5 changes: 3 additions & 2 deletions apps/web/src/api/services/inventory-movements-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ export const InventoryMovementsService = (
return await apiClient.post('/inventory-movements/outbound', movement.dto)
},

async listInventoryMovements({ page, productId,movementType }) {
async listInventoryMovements({ page, productId, movementType, responsibleId }) {
apiClient.setParam('page', String(page))
apiClient.setParam('productId', productId || '')
apiClient.setParam('movementType',String(movementType))
apiClient.setParam('responsibleId', String(responsibleId))
apiClient.setParam('movementType', String(movementType))
return await apiClient.get<PaginationResponse<InventoryMovementDto>>(
'/inventory-movements',
)
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/api/services/users-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export const UsersService = (apiClient: IApiClient): IUsersService => {
},

async listUsers({ page, name, role }) {
apiClient.setParam('name', String(name))
if (name) {
apiClient.setParam('name', String(name))
}
apiClient.setParam('role', String(role))
apiClient.setParam('page', String(page))
return await apiClient.get<PaginationResponse<UserDto>>('/users')
Expand Down
91 changes: 91 additions & 0 deletions apps/web/src/ui/components/commons/employee-select/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Button, Pagination, Spinner } from '@nextui-org/react'
import { useEmployeeSelect } from './use-employee-select'
import { Dialog } from '../dialog'
import { Icon } from '../icon'
import { Select } from '../select'

type EmployeeSelectProps = {
onSelectChange: (employeeId: string) => void
}

export const EmployeeSelect = ({ onSelectChange }: EmployeeSelectProps) => {
const {
employees,
isFetching,
page,
totalPages,
handleEmployeeIdchange,
handleEmployeePageChange,
selectedEmployeeName,
handleEmployeeNamechange,
} = useEmployeeSelect(onSelectChange)

return isFetching ? (
<Spinner size='sm' className='w-full h-full mx-auto' />
) : (
<div className='space-y-2 flex flex-row gap-4 items-center w-full'>
<Dialog
title='Selecione o Funcionário'
size='2xl'
trigger={
<Select className='min-w-48'>
{selectedEmployeeName ? selectedEmployeeName : 'Selecione o funcionario'}
</Select>
}
>
{(closeDrawer) =>
employees.length === 0 ? (
<p className='text-center text-zinc-600 font-semibold my-12'>
Nenhum funcionário encontrado
</p>
) : (
<>
<div className='space-y-4'>
{employees.map((employee) => (
<div
key={employee.id}
className='flex items-center justify-between p-4 border border-zinc-300 rounded-lg'
>
<span className='font-medium text-zinc-800'>{employee.name}</span>
<Button
className='bg-transparent hover:bg-primary hover:text-white duration-1000 border-zinc-400 h-10 min-w-10'
onClick={() => {
handleEmployeeNamechange(employee.name)

handleEmployeeIdchange(employee.id as string)
closeDrawer()
}}
>
<Icon name='plus' size={18} />
</Button>
</div>
))}
</div>
{totalPages > 1 && (
<Pagination
page={page}
total={totalPages}
onChange={handleEmployeePageChange}
showControls
className="pb-8 pt-8"
/>
)}
</>
)
}
</Dialog>
{selectedEmployeeName && (
<button
type='button'
onClick={() => {
handleEmployeeNamechange('')
handleEmployeeIdchange('')
}}
className='flex justify-center items-center gap-2 text-sm text-gray-400'
>
Remover Filtro <Icon name='close' className='size-4' />
</button>
)}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useApi, useCache, useToast, useUrlParamNumber } from '@/ui/hooks'
import { useState } from 'react'
import { useAuthContext } from '../../contexts/auth-context'
import { CACHE } from '@/constants'
import { User } from '@stocker/core/entities'

export function useEmployeeSelect(onSelectChange: (employeeId: string) => void) {
const [employeeId, setEmployeeId] = useState<string>()
const [selectedEmployeeName,setSelectedEmployeeName] = useState<string>()
const { usersService } = useApi()
const { company } = useAuthContext()
const { showError } = useToast()
const [page, setPage] = useUrlParamNumber('employeePage', 1)
function handleEmployeeIdchange(employeeId: string) {
setEmployeeId(employeeId)
onSelectChange(employeeId)
}
function handleEmployeeNamechange(name:string){
setSelectedEmployeeName(name)
}
async function fetchEmployees() {
if (!company) return
const response = await usersService.listUsers({
page,
companyId: company.id,
})
if (response.isFailure) {
showError(response.errorMessage)
return
}
return response.body
}
const { data, refetch, isFetching } = useCache({
fetcher: fetchEmployees,
key: CACHE.users.key,
dependencies: [page],
})
function handlePagechange(page:number){
setPage(page)
}
console.log(data)
const employees = data ? data.items : []
const itemsCount = data ? data.itemsCount : 0
return {
isFetching,
totalPages: Math.ceil(itemsCount / 10),
page,
employees,
handleEmployeeIdchange,
handleEmployeePageChange: handlePagechange,
handleEmployeeNamechange,
selectedEmployeeName
}
}
47 changes: 33 additions & 14 deletions apps/web/src/ui/components/pages/inventory-movements/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,47 @@
import { Select, SelectItem } from '@nextui-org/react'
import { InventoryMovementsTable } from './inventory-movements-table'
import { useInventoryMovementPage } from './use-inventory-moviments-page'
import { EmployeeSelect } from '../../commons/employee-select'

export const InventoryMovementsPage = () => {
const { isFetching, page, movements, totalPages, handlePageChange,handleMovementTypeSearchChange,movementTypeSearch } =
useInventoryMovementPage()
const {
handleEmployeeIdSerachChange,
employeeIdSearch,
isFetching,
page,
movements,
totalPages,
handlePageChange,
handleMovementTypeSearchChange,
movementTypeSearch,
} = useInventoryMovementPage()

return (
<>
<div className='flex space-y-2 flex-col'>
<div className='flex justify-between'>
<div className='flex-1 max-w-96 space-y-2'>
<div className='flex-1 space-y-2'>
<h1 className='text-3xl font-black'>Lançamentos</h1>
<Select defaultSelectedKeys={['']} value={movementTypeSearch} onChange={(e) => handleMovementTypeSearchChange(e.target.value)}>
<SelectItem key='' value=''>
Todos
</SelectItem>
<SelectItem key='inbound' value='inbound'>
Entrada
</SelectItem>
<SelectItem key='outbound' value='outbound'>
Saida
</SelectItem>
</Select>
<div className='flex md:flex-row flex=col items-center gap-4 w-full'>
<Select
className='max-w-60'
size='lg'
defaultSelectedKeys={['']}
value={movementTypeSearch}
onChange={(e) => handleMovementTypeSearchChange(e.target.value)}
>
<SelectItem key='' value=''>
Todos
</SelectItem>
<SelectItem key='inbound' value='inbound'>
Entrada
</SelectItem>
<SelectItem key='outbound' value='outbound'>
Saida
</SelectItem>
</Select>
<EmployeeSelect onSelectChange={handleEmployeeIdSerachChange} />
</div>
</div>
</div>
<InventoryMovementsTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export function useInventoryMovementPage() {
const { showError } = useToast()
const [page, setPage] = useUrlParamNumber('page', 1)
const [movementTypeSearch, setMovementTypeSearch] = useUrlParamString('type')
const [employeeIdSearch,setEmployeIdSearch] = useUrlParamString('employeeId')

async function fetchInventoryMovements() {
const response = await inventoryMovementService.listInventoryMovements({
movementType: movementTypeSearch as InventoryMovementType,
responsibleId: employeeIdSearch,
page,
})
if (response.isFailure) {
Expand All @@ -35,15 +37,16 @@ export function useInventoryMovementPage() {
function handlePageChange(page: number) {
setPage(page)
}

function handleEmployeeIdSerachChange(employeeId:string){
setEmployeIdSearch(employeeId)
}
const { data, isFetching } = useCache({
fetcher: fetchInventoryMovements,
key: CACHE.productInventoryMovements.key,
dependencies: [page, movementTypeSearch],
dependencies: [page, movementTypeSearch,employeeIdSearch],
})
const movements = data ? data.items.map(InventoryMovement.create) : []
const itemsCount = data ? data.itemsCount : 0
console.log(movements)

return {
page,
Expand All @@ -54,5 +57,7 @@ export function useInventoryMovementPage() {
movementTypeSearch,
handleMovementTypeSearchChange,
handlePageChange,
handleEmployeeIdSerachChange,
employeeIdSearch
}
}
2 changes: 1 addition & 1 deletion apps/web/src/ui/components/pages/stocks/use-stocks-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function useStocksPage() {
}

function handleSearchChange(value: string) {
setFilterByNameValue(value ?? '')
setFilterByNameValue(value)
}

return {
Expand Down

0 comments on commit 613918b

Please sign in to comment.