-
Notifications
You must be signed in to change notification settings - Fork 2
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 #31 from Josmar-jr/fix/implement-cost-center-autoc…
…omplete Fix/implement cost center autocomplete
- Loading branch information
Showing
7 changed files
with
190 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { useQuery } from 'react-apollo' | ||
import { AutocompleteInput } from 'vtex.styleguide' | ||
import { useIntl } from 'react-intl' | ||
|
||
import { messages } from './customers-admin' | ||
import GET_COST_CENTER_BY_ORG from '../queries/costCentersByOrg.gql' | ||
import { SEARCH_TERM_DELAY_MS } from '../constants/debounceDelay' | ||
|
||
interface Props { | ||
onChange: (value: { value: string | null; label: string }) => void | ||
organizationId?: string | ||
} | ||
|
||
const initialState = { | ||
search: '', | ||
page: 1, | ||
pageSize: 25, | ||
sortOrder: 'ASC', | ||
sortedBy: 'name', | ||
} | ||
|
||
const CostCenterAutocomplete = ({ onChange, organizationId }: Props) => { | ||
const { formatMessage } = useIntl() | ||
const [costCenterTextInput, setCostCenterTextInput] = useState('') | ||
const [debouncedSearchTerm, setDebouncedSearchTerm] = | ||
useState(costCenterTextInput) | ||
|
||
const { data, loading, refetch } = useQuery(GET_COST_CENTER_BY_ORG, { | ||
variables: { | ||
...initialState, | ||
id: organizationId, | ||
}, | ||
ssr: false, | ||
notifyOnNetworkStatusChange: true, | ||
skip: !organizationId, | ||
}) | ||
|
||
const onClear = () => { | ||
setCostCenterTextInput('') | ||
onChange({ value: null, label: '' }) | ||
} | ||
|
||
useEffect(() => { | ||
const handler = setTimeout(() => { | ||
setDebouncedSearchTerm(costCenterTextInput) | ||
}, SEARCH_TERM_DELAY_MS) // 500ms delay | ||
|
||
return () => { | ||
clearTimeout(handler) | ||
} | ||
}, [costCenterTextInput]) | ||
|
||
useEffect(() => { | ||
if (debouncedSearchTerm) { | ||
refetch({ | ||
...initialState, | ||
id: organizationId, | ||
search: debouncedSearchTerm, | ||
}) | ||
} else if (debouncedSearchTerm === '') { | ||
onClear() | ||
} | ||
}, [debouncedSearchTerm]) | ||
|
||
const options = { | ||
maxHeight: 200, | ||
onSelect: onChange, | ||
loading, | ||
value: data?.getCostCentersByOrganizationId?.data?.map( | ||
(costCenter: { id: string; name: string }) => ({ | ||
value: costCenter.id, | ||
label: costCenter.name, | ||
}) | ||
), | ||
} | ||
|
||
const input = { | ||
onChange: (_term: string) => { | ||
setCostCenterTextInput(_term) | ||
}, | ||
onClear, | ||
placeholder: formatMessage(messages.costCenter), | ||
value: costCenterTextInput, | ||
} | ||
|
||
return <AutocompleteInput input={input} options={options} /> | ||
} | ||
|
||
export default CostCenterAutocomplete |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SEARCH_TERM_DELAY_MS = 500 |
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 |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
"vtex.render-runtime": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.render-runtime", | ||
"vtex.storefront-permissions": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.storefront-permissions", | ||
"vtex.storefront-permissions-components": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.storefront-permissions-components", | ||
"vtex.styleguide": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected].3/public/@types/vtex.styleguide" | ||
"vtex.styleguide": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected].13/public/@types/vtex.styleguide" | ||
}, | ||
"dependencies": { | ||
"faker": "^4.1.0", | ||
|
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 |
---|---|---|
|
@@ -500,9 +500,9 @@ [email protected]: | |
version "1.23.0" | ||
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.storefront-permissions#2a12e48a1b630d6d9cd64115572bcae55a7aa756" | ||
|
||
"vtex.styleguide@http://vtex.vtexassets.com/_v/public/typings/v1/[email protected].3/public/@types/vtex.styleguide": | ||
version "9.146.3" | ||
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected].3/public/@types/vtex.styleguide#05558160f29cd8f4aefe419844a4bd66e2b3fdbb" | ||
"vtex.styleguide@http://vtex.vtexassets.com/_v/public/typings/v1/[email protected].13/public/@types/vtex.styleguide": | ||
version "9.146.13" | ||
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected].13/public/@types/vtex.styleguide#f4ccbc54621bf5114ddd115b6032ae320f2eba55" | ||
|
||
zen-observable-ts@^0.8.21: | ||
version "0.8.21" | ||
|