Skip to content

Commit de1b5fb

Browse files
committed
Search users in teams DB with ILIKE expression
1 parent 6e8ebec commit de1b5fb

File tree

4 files changed

+52
-46
lines changed

4 files changed

+52
-46
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
NEXTAUTH_URL=http://127.0.0.1:3000
2-
DATABASE_URL=postgres://postgres:postgres@localhost:5433/osm-teams?sslmode=disable
2+
DATABASE_URL=postgres://wille:wille@localhost:5432/osm-teams?sslmode=disable

.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
NEXTAUTH_URL=http://127.0.0.1:3000
22
NEXTAUTH_SECRET=next-auth-cypress-secret
3-
DATABASE_URL=postgres://postgres:postgres@localhost:5434/osm-teams-test
3+
DATABASE_URL=postgres://wille:wille@localhost:5432/osm-teams-test
44
TESTING=true
55
LOG_LEVEL=silent
66
AUTH_URL=http://127.0.0.1:3000

src/components/add-member-form.js

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ListIcon,
1111
Link,
1212
Code,
13+
Text,
1314
} from '@chakra-ui/react'
1415
import { AtSignIcon, AddIcon } from '@chakra-ui/icons'
1516
import join from 'url-join'
@@ -84,14 +85,14 @@ export function AddMemberByUsernameForm({ onSubmit }) {
8485
if (res.status === 200) {
8586
const data = await res.json()
8687
if (data?.users.length) {
87-
setSearchResult(data.users[0])
88+
setSearchResult(data.users)
8889
setStatus('successSearch')
8990
} else {
90-
setSearchResult({})
91+
setSearchResult([])
9192
setStatus('noResults')
9293
}
9394
} else {
94-
setSearchResult({})
95+
setSearchResult([])
9596
setStatus('noResults')
9697
}
9798
}
@@ -102,7 +103,7 @@ export function AddMemberByUsernameForm({ onSubmit }) {
102103
await onSubmit({ osmId: uid, username })
103104
actions.setSubmitting(false)
104105
actions.resetForm({ username: '' })
105-
setSearchResult({})
106+
setSearchResult([])
106107
} catch (e) {
107108
logger.error(e)
108109
actions.setSubmitting(false)
@@ -153,45 +154,50 @@ export function AddMemberByUsernameForm({ onSubmit }) {
153154
</Button>
154155
</Flex>
155156
<Box display='flex' justifyContent='stretch' py={3} px={1}>
156-
{searchResult?.id && (
157-
<List spacing={5} fontSize='sm' width={'100%'}>
158-
<ListItem
159-
display='flex'
160-
alignItems='center'
161-
justifyContent='space-between'
162-
>
163-
<ListIcon as={AtSignIcon} color='brand.600' />
164-
<Link
165-
href={join(OSM_DOMAIN, '/user', searchResult.name)}
166-
isExternal
157+
<List spacing={5} fontSize='sm' width={'100%'}>
158+
{searchResult?.length &&
159+
searchResult.map((result) => (
160+
<ListItem
161+
key={result.id}
162+
display='flex'
163+
alignItems='center'
164+
justifyContent='space-between'
165+
marginTop='1rem'
167166
>
168-
{searchResult.name}
169-
</Link>
170-
<Code ml={2}>{searchResult.id}</Code>
171-
<Button
172-
ml='auto'
173-
textTransform='lowercase'
174-
onClick={async () =>
175-
submit(searchResult.id, searchResult.name, {
176-
setStatus,
177-
setSubmitting,
178-
resetForm,
179-
})
180-
}
181-
size='sm'
182-
isLoading={isSubmitting}
183-
loadingText='Adding'
184-
isDisabled={isSubmitting}
185-
leftIcon={<AddIcon />}
186-
>
187-
Add
188-
</Button>
189-
</ListItem>
190-
</List>
191-
)}
192-
{status === 'noResults' && (
193-
<Box fontSize='md'>No results found.</Box>
194-
)}
167+
<ListIcon as={AtSignIcon} color='brand.600' />
168+
<Link
169+
href={join(OSM_DOMAIN, '/user', result.name)}
170+
isExternal
171+
>
172+
{result.name}
173+
</Link>
174+
<Code ml={2}>{result.id}</Code>
175+
<Button
176+
ml='auto'
177+
textTransform='lowercase'
178+
onClick={async () =>
179+
submit(result.id, result.name, {
180+
setStatus,
181+
setSubmitting,
182+
resetForm,
183+
})
184+
}
185+
size='sm'
186+
isLoading={isSubmitting}
187+
loadingText='Adding'
188+
isDisabled={isSubmitting}
189+
leftIcon={<AddIcon />}
190+
>
191+
Add
192+
</Button>
193+
</ListItem>
194+
))}
195+
{status === 'noResults' && (
196+
<Text as='b'>
197+
No results found. Try typing the exact OSM username.
198+
</Text>
199+
)}
200+
</List>
195201
</Box>
196202
</>
197203
)

src/models/users.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const db = require('../lib/db')
99
**/
1010
async function list(options = {}) {
1111
// Apply search
12-
let query = db('osm_users')
12+
let query = await db('osm_users')
1313
.select('id', 'name')
14-
.where('name', options.username)
14+
.whereILike('name', `%${options.username}%`)
1515

1616
return query
1717
}

0 commit comments

Comments
 (0)