-
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 branch 'main' of https://github.com/CtrI-Alt-Del/stocker
- Loading branch information
Showing
7 changed files
with
193 additions
and
21 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
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
91 changes: 91 additions & 0 deletions
91
apps/web/src/ui/components/commons/employee-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,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> | ||
) | ||
} |
54 changes: 54 additions & 0 deletions
54
apps/web/src/ui/components/commons/employee-select/use-employee-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,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 | ||
} | ||
} |
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