-
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.
Feature/app 13 allow users to add multiple authors to reports (#152)
Co-authored-by: Anna Pokorska <[email protected]> Co-authored-by: Osneil Drakes <[email protected]>
- Loading branch information
1 parent
7cc869c
commit 6e4514e
Showing
18 changed files
with
1,234 additions
and
1,401 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,117 @@ | ||
import React, { useState } from 'react' | ||
import { | ||
Box, | ||
Input, | ||
Tag, | ||
TagLabel, | ||
TagCloseButton, | ||
VStack, | ||
HStack, | ||
FormLabel, | ||
FormControl, | ||
FormHelperText, | ||
FormErrorMessage, | ||
} from '@chakra-ui/react' | ||
import { Controller, Control, FieldValues, Path } from 'react-hook-form' | ||
|
||
type TProps<T extends FieldValues> = { | ||
name: Path<T> | ||
control: Control<T> | ||
type?: 'text' | 'number' | ||
label?: string | ||
isRequired?: boolean | ||
showHelperText?: boolean | ||
isDisabled?: boolean | ||
} | ||
|
||
export const MultiValueInput = <T extends FieldValues>({ | ||
name, | ||
control, | ||
type = 'text', | ||
label, | ||
showHelperText, | ||
isDisabled, | ||
}: TProps<T>) => { | ||
const [inputValue, setInputValue] = useState('') | ||
|
||
return ( | ||
<Controller | ||
name={name} | ||
control={control} | ||
render={({ field, fieldState: { error } }) => { | ||
const handleAddValue = () => { | ||
const currentValues = (field.value as string[]) || [] | ||
if (inputValue.trim() && !currentValues.includes(inputValue.trim())) { | ||
const newValues = [...currentValues, inputValue.trim()] | ||
field.onChange(newValues) | ||
setInputValue('') | ||
} | ||
} | ||
|
||
const handleRemoveValue = (valueToRemove: string) => { | ||
const currentValues = field.value || [] | ||
const newValues = currentValues.filter( | ||
(value) => value !== valueToRemove, | ||
) | ||
field.onChange(newValues) | ||
} | ||
|
||
const handleKeyDown = (e: React.KeyboardEvent) => { | ||
if (e.key === 'Enter') { | ||
e.preventDefault() | ||
handleAddValue() | ||
} | ||
} | ||
|
||
return ( | ||
<FormControl | ||
isInvalid={!!error} | ||
isReadOnly={isDisabled} | ||
isDisabled={isDisabled} | ||
isRequired={field.value?.length === 0} | ||
> | ||
{label && <FormLabel>{label}</FormLabel>} | ||
<FormHelperText mb={2}> | ||
You are able to add multiple values | ||
</FormHelperText> | ||
<Box> | ||
<VStack spacing={4} align='stretch'> | ||
<HStack> | ||
<Input | ||
{...field} // This destructured object contains the value | ||
name={name} | ||
type={type} | ||
bg='white' | ||
onChange={(e) => setInputValue(e.target.value)} | ||
onKeyDown={handleKeyDown} | ||
placeholder='Type a value and press Enter' | ||
value={inputValue} | ||
/> | ||
</HStack> | ||
<HStack wrap='wrap' spacing={2}> | ||
{(field.value || []).map((value: string, index: number) => ( | ||
<Tag | ||
key={index} | ||
size='lg' | ||
colorScheme='gray' | ||
borderRadius='full' | ||
> | ||
<TagLabel>{value}</TagLabel> | ||
<TagCloseButton | ||
onClick={() => handleRemoveValue(value)} | ||
/> | ||
</Tag> | ||
))} | ||
</HStack> | ||
</VStack> | ||
</Box> | ||
{showHelperText && isDisabled && ( | ||
<FormHelperText>You cannot edit this</FormHelperText> | ||
)} | ||
{error && <FormErrorMessage>{error.message}</FormErrorMessage>} | ||
</FormControl> | ||
) | ||
}} | ||
/> | ||
) | ||
} |
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
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,26 @@ | ||
import sign from 'jwt-encode' | ||
|
||
export const setupUser = ( | ||
organisationName: string = 'CPR', | ||
email: string = '[email protected]', | ||
isSuperuser: boolean = true, | ||
isAdmin: boolean = true, | ||
orgId: number = 1, | ||
) => { | ||
localStorage.setItem( | ||
'token', | ||
sign( | ||
{ | ||
email: email, | ||
is_superuser: isSuperuser, | ||
authorisation: { | ||
[organisationName]: { | ||
is_admin: isAdmin, | ||
}, | ||
org_id: orgId, | ||
}, | ||
}, | ||
'', | ||
), | ||
) | ||
} |
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,16 @@ | ||
import { cclwConfigMock, mcfConfigMock } from '@/tests/utilsTest/mocks' | ||
import { http, HttpResponse } from 'msw' | ||
import { jwtDecode } from 'jwt-decode' | ||
|
||
export const configHandlers = [ | ||
http.get('*/v1/config', (req) => { | ||
const authHeader = req.request.headers.get('authorization') | ||
const parsedAuthToken: Record<string, object> = jwtDecode(authHeader || '') | ||
const authorisation = parsedAuthToken?.authorisation || {} | ||
const org = Object.keys(authorisation)[0] | ||
if (org && ['GCF', 'GEF', 'AF', 'CIF'].includes(org)) { | ||
return HttpResponse.json({ ...mcfConfigMock }) | ||
} | ||
return HttpResponse.json({ ...cclwConfigMock }) | ||
}), | ||
] |
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
Oops, something went wrong.