Skip to content

Commit

Permalink
Merge pull request #9 from vtex-apps/feature/org-autocomplete
Browse files Browse the repository at this point in the history
[B2BORG-114] Autocomplete organization selector
  • Loading branch information
Arthur Bond authored May 27, 2022
2 parents 9797120 + b77cf5a commit 6a401e5
Show file tree
Hide file tree
Showing 10 changed files with 692 additions and 228 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Autocomplete input to select an organization
- Button to remove a user's B2B info
- Heading and border for B2B info section

## [0.0.8] - 2022-02-18

### Added
Expand All @@ -18,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [0.0.7] - 2022-01-10

### Removed

- Can Impersonate
- Console logs

Expand All @@ -26,10 +33,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [0.0.5] - 2021-12-28

### Added

- Allow cleanup Organization selection

### Fixed

- Cost Center not cleaning up after organization is changed

## [0.0.4] - 2021-11-10

## [0.0.3] - 2021-11-03
Expand Down
4 changes: 3 additions & 1 deletion messages/context.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"admin/storefront-permissions.b2bInfo.title": "admin/storefront-permissions.b2bInfo.title",
"admin/storefront-permissions.button.cancel": "admin/storefront-permissions.button.cancel",
"admin/storefront-permissions.button.delete": "admin/storefront-permissions.button.delete",
"admin/storefront-permissions.button.save": "admin/storefront-permissions.button.save",
Expand All @@ -19,5 +20,6 @@
"admin/storefront-permissions.tab.users.name.label": "admin/storefront-permissions.tab.users.name.label",
"admin/storefront-permissions.tab.users.success": "admin/storefront-permissions.tab.users.success",
"admin/storefront-permissions.title": "admin/storefront-permissions.title",
"admin/storefront-permissions.alert-pick": "admin/storefront-permissions.alert-pick"
"admin/storefront-permissions.alert-pick": "admin/storefront-permissions.alert-pick",
"admin/storefront-permissions.searchOrganizations": "admin/storefront-permissions.searchOrganizations"
}
6 changes: 4 additions & 2 deletions messages/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"admin/storefront-permissions.b2bInfo.title": "B2B Information",
"admin/storefront-permissions.button.cancel": "Cancel",
"admin/storefront-permissions.button.delete": "Delete",
"admin/storefront-permissions.button.delete": "Remove B2B Info",
"admin/storefront-permissions.button.save": "Save",
"admin/storefront-permissions.description": "Roles and User management",
"admin/storefront-permissions.navigation.keywords": "b2b, Roles, Profiles",
Expand All @@ -19,5 +20,6 @@
"admin/storefront-permissions.tab.users.name.label": "Name",
"admin/storefront-permissions.tab.users.success": "B2B info saved",
"admin/storefront-permissions.title": "Storefront Permissions",
"admin/storefront-permissions.alert-pick": "You need to pick a cost center before you can save changes"
"admin/storefront-permissions.alert-pick": "You need to pick a cost center before you can save changes",
"admin/storefront-permissions.searchOrganizations": "Search organizations..."
}
104 changes: 104 additions & 0 deletions react/components/OrganizationsAutocomplete.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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_ORGANIZATIONS from '../queries/listOrganizations.gql'
import GET_ORGANIZATION_BY_ID from '../queries/getOrganization.graphql'

const initialState = {
status: ['active', 'on-hold', 'inactive'],
search: '',
page: 1,
pageSize: 25,
sortOrder: 'ASC',
sortedBy: 'name',
}

interface Props {
onChange: (value: { value: string | null; label: string }) => void
organizationId: string
}

const OrganizationsAutocomplete = ({ onChange, organizationId }: Props) => {
const { formatMessage } = useIntl()
const [term, setTerm] = useState('')
const [hasChanged, setHasChanged] = useState(false)
const [values, setValues] = useState([] as any)
const { data, loading, refetch } = useQuery(GET_ORGANIZATIONS, {
variables: initialState,
ssr: false,
notifyOnNetworkStatusChange: true,
})

const { data: organization } = useQuery(GET_ORGANIZATION_BY_ID, {
variables: { id: organizationId },
ssr: false,
fetchPolicy: 'network-only',
notifyOnNetworkStatusChange: true,
skip: !organizationId,
})

const options = {
onSelect: (value: any) => onChange(value),
loading,
value: values,
}

const onClear = () => {
if (!hasChanged) return
setTerm('')
onChange({ value: null, label: '' })
}

useEffect(() => {
if (!organization) {
return
}

const { name, id } = organization.getOrganizationById

setTerm(name)
setHasChanged(true)
onChange({ value: id, label: name })
}, [organization])

useEffect(() => {
if (data?.getOrganizations?.data) {
setValues(
data.getOrganizations.data.map((item: any) => {
return {
value: item.id,
label: item.name,
}
})
)
}
}, [data])

useEffect(() => {
if (term && term.length > 2) {
setHasChanged(true)
refetch({
...initialState,
search: term,
})
} else if (term === '') {
onClear()
}
}, [term])

const input = {
onChange: (_term: string) => {
setTerm(_term)
},
onClear,
placeholder: formatMessage(messages.searchOrganizations),
value: term,
}

return <AutocompleteInput input={input} options={options} />
}

export default OrganizationsAutocomplete
Loading

0 comments on commit 6a401e5

Please sign in to comment.