-
-
Notifications
You must be signed in to change notification settings - Fork 389
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 #3127 from goratt12/2409-validate-collaborator-use…
…rnames feat(research): select and validate research collaborators usernames
- Loading branch information
Showing
7 changed files
with
136 additions
and
3 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
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,36 @@ | ||
import type { IUserPP } from 'src/models' | ||
import type { UserStore } from 'src/stores/User/user.store' | ||
|
||
const USER_RESULTS_LIMIT = 20 | ||
|
||
export interface IOption { | ||
value: string | ||
label: string | ||
} | ||
|
||
export const loadUserNameOptions = async ( | ||
userStore: UserStore, | ||
defaultOptions: string[] | undefined, | ||
inputValue: string, | ||
) => { | ||
// if there is no user input, use defaultOptions | ||
if (inputValue == '') { | ||
const selectOptions: IOption[] = defaultOptions?.length | ||
? defaultOptions | ||
.filter((user) => user != '') | ||
.map((user) => ({ | ||
value: user, | ||
label: user, | ||
})) | ||
: [] | ||
return selectOptions | ||
} else { | ||
const usersStartingWithInput: IUserPP[] = | ||
await userStore.getUsersStartingWith(inputValue, USER_RESULTS_LIMIT) | ||
const selectOptions: IOption[] = usersStartingWithInput.map((user) => ({ | ||
value: user.userName, | ||
label: user.userName, | ||
})) | ||
return selectOptions | ||
} | ||
} |
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,59 @@ | ||
import { useEffect, useState } from 'react' | ||
import { observer } from 'mobx-react' | ||
import { Select } from 'oa-components' | ||
import { useCommonStores } from 'src/index' | ||
|
||
import { loadUserNameOptions } from './LoadUserNameOptions' | ||
|
||
import type { IOption } from './LoadUserNameOptions' | ||
|
||
interface IProps { | ||
input: { | ||
value: string[] | ||
onChange: (v: string[]) => void | ||
} | ||
defaultOptions?: string[] | ||
placeholder?: string | ||
isForm?: boolean | ||
} | ||
|
||
export const UserNameSelect = observer((props: IProps) => { | ||
const { defaultOptions, input, isForm, placeholder } = props | ||
const { userStore } = useCommonStores().stores | ||
const [inputValue, setInputValue] = useState('') | ||
const [options, setOptions] = useState<IOption[]>([]) | ||
|
||
const loadOptions = async (inputVal: string) => { | ||
const selectOptions = await loadUserNameOptions( | ||
userStore, | ||
defaultOptions, | ||
inputVal, | ||
) | ||
setOptions(selectOptions) | ||
} | ||
|
||
// Effect to load options whenever the input value changes | ||
useEffect(() => { | ||
loadOptions(inputValue) | ||
}, [inputValue]) | ||
|
||
const value = input.value?.length | ||
? input.value.map((user) => ({ | ||
value: user, | ||
label: user, | ||
})) | ||
: [] | ||
|
||
return ( | ||
<Select | ||
variant={isForm ? 'form' : undefined} | ||
options={options} | ||
placeholder={placeholder} | ||
value={value} | ||
onChange={(v: IOption[]) => input.onChange(v.map((user) => user.value))} | ||
onInputChange={setInputValue} | ||
isClearable={true} | ||
isMulti={true} | ||
/> | ||
) | ||
}) |
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