Skip to content

Commit

Permalink
refactor: set translations use case
Browse files Browse the repository at this point in the history
  • Loading branch information
avrcoelho committed Oct 15, 2022
1 parent dbea84c commit fb36920
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/presentation/components/Notification/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const Notification = ({
const buttonCloseStyleIndex = `buttonClose${theme}` as 'buttonClosecolored';
const buttonCloseTextStyleIndex =
`buttonCloseText${theme}` as 'buttonCloseTextcolored';
const textStyle = `text${theme}` as 'textlight';

return (
<PanGestureHandler onGestureEvent={onGestureEvent}>
Expand Down Expand Up @@ -129,18 +130,15 @@ export const Notification = ({
<Text
ellipsizeMode="tail"
numberOfLines={titleMaxLines}
style={[
styles.title,
styles[typeAndTheme] || customStyle.title,
]}
style={[styles.title, styles[textStyle] || customStyle.title]}
>
{title}
</Text>
)}
<Text
ellipsizeMode="tail"
numberOfLines={textMaxLines}
style={[styles.text, styles[typeAndTheme] || customStyle.text]}
style={[styles.text, styles[textStyle] || customStyle.text]}
>
{text}
</Text>
Expand Down
9 changes: 9 additions & 0 deletions src/presentation/components/Notification/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ export const styles = StyleSheet.create({
fontSize: 16,
lineHeight: 20,
},
textcolored: {
color: Colors.White,
},
textdark: {
color: Colors.White,
},
textlight: {
color: Colors.Grey,
},

buttonClose: {
width: 16,
Expand Down
12 changes: 10 additions & 2 deletions src/presentation/components/Notification/useController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { getAnimation } from '../../utils/getAnimation';
import { useNotificationWidth } from '../../hooks/useNotificationWidth';
import { useLimitToRemove } from '../../hooks/useLimitToRemove';
import { useIsMounted } from '../../hooks/useIsMounted';

type UseControllerHookProps = {
dragDirection: NotificationDragDirection;
Expand Down Expand Up @@ -87,6 +88,7 @@ export const useController: UseControllerHook = ({
const transitionDirection = dragDirection.toUpperCase() as 'X' | 'Y';
const positionOnScreen = useSharedValue(0);
const [isPaused, toggleIsPaused] = useToggle(false);
const timerRef = useRef<NodeJS.Timeout>();

const onTogglePause = useCallback((): void => {
'worklet';
Expand Down Expand Up @@ -192,6 +194,13 @@ export const useController: UseControllerHook = ({

const [animationEnteringFinish, toggleAnimationEnteringFinish] =
useToggle(false);
const isMounted = useIsMounted();
const onToggleAnimationEnteringFinish = useCallback(() => {
if (isMounted) {
toggleAnimationEnteringFinish();
}
}, [toggleAnimationEnteringFinish, isMounted]);

const animatedStyle = useAnimatedStyle(() => {
const translateIndex = `translate${transitionDirection}` as 'translateX';
return animationEnteringFinish
Expand All @@ -214,12 +223,11 @@ export const useController: UseControllerHook = ({
'worklet';

if (isFinished) {
runOnJS(toggleAnimationEnteringFinish)();
runOnJS(onToggleAnimationEnteringFinish)();
}
};

const delayDecrement = useRef(delay / DELAY);
const timerRef = useRef<NodeJS.Timeout>();

const onExecuteTimer = useCallback((): void => {
timerRef.current = setInterval(() => {
Expand Down
19 changes: 19 additions & 0 deletions src/presentation/hooks/__tests__/useIsMounted.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { renderHook } from '@testing-library/react-hooks';

import { useIsMounted } from '../useIsMounted';

describe('useIsMounted hook', () => {
it('should be able to return mounted value', async () => {
const { result, rerender } = renderHook(useIsMounted);
rerender();

expect(result.current).toBeTruthy();
});

it('should be able to return unmounted value', async () => {
const { result, unmount } = renderHook(useIsMounted);
unmount();

expect(result.current).toBeFalsy();
});
});
15 changes: 15 additions & 0 deletions src/presentation/hooks/useIsMounted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect, useRef } from 'react';

export const useIsMounted = (): boolean => {
const isMount = useRef(false);

useEffect(() => {
isMount.current = true;

return () => {
isMount.current = false;
};
});

return isMount.current;
};
9 changes: 5 additions & 4 deletions storybook/stories/Notification/Notification.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable import/no-extraneous-dependencies */
import { storiesOf } from '@storybook/react-native';
import React from 'react';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { storiesOf } from '@storybook/react-native';

import { NotificationContainer, useNotification } from '../../../src/main';

Expand All @@ -24,7 +25,7 @@ const App = (): JSX.Element => {
};

return (
<>
<GestureHandlerRootView style={{ flex: 1 }}>
<View style={styles.container}>
<TouchableOpacity
style={{ marginTop: 100 }}
Expand All @@ -34,8 +35,8 @@ const App = (): JSX.Element => {
</TouchableOpacity>
</View>

<NotificationContainer theme="light" />
</>
<NotificationContainer theme="light" closeOnPress />
</GestureHandlerRootView>
);
};

Expand Down

0 comments on commit fb36920

Please sign in to comment.