Skip to content

Commit

Permalink
LUN-400: Adjustments for add image
Browse files Browse the repository at this point in the history
  • Loading branch information
ztefanie committed Oct 11, 2022
1 parent 52893d7 commit dafd644
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/components/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const Dropdown = <T extends ValueType>({
fontFamily: theme.fonts.contentFontRegular,
paddingHorizontal: wp('2%'),
}}
listMode='SCROLLVIEW'
/>
{showErrorValidation && (
<ErrorContainer>{errorMessage.length > 0 && <ContentError>{errorMessage}</ContentError>}</ErrorContainer>
Expand Down
1 change: 1 addition & 0 deletions src/components/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Container = styled(GenericListItemContainer)<{
feedback: EXERCISE_FEEDBACK
}>`
min-height: ${hp('12%')}px;
width: 100%;
justify-content: center;
flex-direction: column;
border-top-width: 1px;
Expand Down
64 changes: 30 additions & 34 deletions src/routes/process-user-vocabulary/UserVocabularyProcessScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { TitleWithSpacing } from '../../components/Title'
import { ContentError } from '../../components/text/Content'
import { HintText } from '../../components/text/Hint'
import { ARTICLES, BUTTONS_THEME, getArticleWithLabel } from '../../constants/data'
import { Images } from '../../constants/endpoints'
import { RoutesParams } from '../../navigation/NavigationTypes'
import { addUserDocument, getNextUserVocabularyId, incrementNextUserVocabularyId } from '../../services/AsyncStorage'
import { getLabels } from '../../services/helpers'
Expand Down Expand Up @@ -85,45 +84,42 @@ const UserVocabularyProcessScreen = ({ navigation }: UserVocabularyProcessScreen
return
}

const id = await getNextUserVocabularyId()
await incrementNextUserVocabularyId()

const imagePaths: Images = []
images.forEach(image => {
const index = images.indexOf(image)
const path = `${DocumentDirectoryPath}/image-${id}-${index}.txt`
writeFile(path, image, 'utf8')
.then(() => imagePaths.push({ id: index, image: path }))
.catch(reportError)
})

await addUserDocument({
id,
word,
article: ARTICLES[articleId],
document_image: imagePaths,
audio: '',
alternatives: [],
})

navigation.navigate('UserVocabularyList', { headerBackLabel: getLabels().general.back })

setWord('')
setArticleId(null)
setImages([])
try {
const id = await getNextUserVocabularyId()
await incrementNextUserVocabularyId()

const imagePaths = await Promise.all(
images.map(async (image, index) => {
const path = `${DocumentDirectoryPath}/image-${id}-${index}.txt`
await writeFile(path, image, 'utf8')
return { id: index, image: path }
})
)

await addUserDocument({
id,
word,
article: ARTICLES[articleId],
document_image: imagePaths,
audio: '',
alternatives: [],
})

navigation.navigate('UserVocabularyList', { headerBackLabel: getLabels().general.back })

setWord('')
setArticleId(null)
setImages([])
} catch (e) {
reportError(e)
}
}

const pushImage = (uri: string): void => setImages(old => [...old, uri])
const deleteImage = (uri: string): void => setImages(images => images.filter(image => image !== uri))

if (showImageSelectionOverlay) {
return (
<ImageSelectionOverlay
setVisible={setShowImageSelectionOverlay}
pushImage={pushImage}
numberOfImages={images.length}
/>
)
return <ImageSelectionOverlay setVisible={setShowImageSelectionOverlay} pushImage={pushImage} />
}

// TODO add Keyboard handling for input fields LUN-424
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,18 @@ const Container = styled.View`

interface ImageSelectionOverlayProps {
setVisible: (visible: boolean) => void
numberOfImages: number
pushImage: (imageUri: string) => void
}

const ImageSelectionOverlay = ({ setVisible, pushImage, numberOfImages }: ImageSelectionOverlayProps): ReactElement => {
const ImageSelectionOverlay = ({ setVisible, pushImage }: ImageSelectionOverlayProps): ReactElement => {
const camera = useRef<RNCamera>(null)

const takeImage = async () => {
if (camera.current) {
const options = { quality: 0.5, base64: true }
const data = await camera.current.takePictureAsync(options)
setVisible(false)
if (numberOfImages < 3) {
pushImage(data.uri)
}
pushImage(data.uri)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ jest.mock('react-native-camera', () => ({

describe('ImageSelectionOverlay', () => {
it('should render shutter and gallery icon', () => {
const { getByTestId } = render(
<ImageSelectionOverlay setVisible={jest.fn()} numberOfImages={0} pushImage={jest.fn()} />
)
const { getByTestId } = render(<ImageSelectionOverlay setVisible={jest.fn()} pushImage={jest.fn()} />)
expect(getByTestId('take-image-icon')).toBeDefined()
expect(getByTestId('gallery-icon')).toBeDefined()
})
Expand Down

0 comments on commit dafd644

Please sign in to comment.