Skip to content

Commit

Permalink
2548: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
LeandraH committed Nov 26, 2024
1 parent befd67f commit 57427dd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions web/src/components/Feedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type FeedbackProps = {
contactMail: string
onCommentChanged: (comment: string) => void
onContactMailChanged: (contactMail: string) => void
onFeedbackChanged: (isPositiveFeedback: boolean | null) => void
onFeedbackChanged?: (isPositiveFeedback: boolean | null) => void
onSubmit: () => void
sendingStatus: SendingStatusType
noResults: boolean | undefined
Expand Down Expand Up @@ -94,7 +94,7 @@ const Feedback = ({
</InputSection>
</>
) : (
<FeedbackButtons isPositive={isPositiveFeedback} onRatingChange={onFeedbackChanged} />
onFeedbackChanged && <FeedbackButtons isPositive={isPositiveFeedback} onRatingChange={onFeedbackChanged} />

Check warning on line 97 in web/src/components/Feedback.tsx

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

Feedback increases in cyclomatic complexity from 12 to 13, threshold = 10. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
)}

<InputSection
Expand Down
7 changes: 4 additions & 3 deletions web/src/components/FeedbackContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type FeedbackContainerProps = {
noResults?: boolean
slug?: string
onSubmit?: () => void
isPositive: boolean | null
isPositiveRating: boolean | null
setIsPositiveRating?: (isPositiveFeedback: boolean | null) => void
}

export type SendingStatusType = 'idle' | 'sending' | 'failed' | 'successful'
Expand All @@ -29,9 +30,9 @@ export const FeedbackContainer = ({
slug,
onClose,
onSubmit,
isPositive,
isPositiveRating,
setIsPositiveRating,
}: FeedbackContainerProps): ReactElement => {
const [isPositiveRating, setIsPositiveRating] = useState<boolean | null>(isPositive)
const [comment, setComment] = useState<string>('')
const [contactMail, setContactMail] = useState<string>('')
const [sendingStatus, setSendingStatus] = useState<SendingStatusType>('idle')
Expand Down
9 changes: 5 additions & 4 deletions web/src/components/FeedbackToolbarItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const FeedbackToolbarItem = ({ route, slug }: FeedbackToolbarItemProps): ReactEl
const { cityCode, languageCode } = useCityContentParams()
const [isFeedbackOpen, setIsFeedbackOpen] = useState(false)
const [isSubmitted, setIsSubmitted] = useState(false)
const [isPositive, setIsPositive] = useState<boolean | null>(null)
const [isPositiveRating, setIsPositiveRating] = useState<boolean | null>(null)
const { t } = useTranslation('feedback')
const title = isSubmitted ? t('thanksHeadline') : t('headline')

Expand All @@ -34,7 +34,8 @@ const FeedbackToolbarItem = ({ route, slug }: FeedbackToolbarItemProps): ReactEl
cityCode={cityCode}
language={languageCode}
slug={slug}
isPositive={isPositive}
isPositiveRating={isPositiveRating}
setIsPositiveRating={setIsPositiveRating}
/>
</Modal>
)}
Expand All @@ -43,15 +44,15 @@ const FeedbackToolbarItem = ({ route, slug }: FeedbackToolbarItemProps): ReactEl
text={t('useful')}
onClick={() => {
setIsFeedbackOpen(true)
setIsPositive(true)
setIsPositiveRating(true)
}}
/>
<ToolbarItem
icon={SadSmileyIcon}
text={t('notUseful')}
onClick={() => {
setIsFeedbackOpen(true)
setIsPositive(false)
setIsPositiveRating(false)
}}
/>
</>
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/SearchFeedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const SearchFeedback = ({ cityCode, languageCode, query, noResults }: SearchFeed
routeType={SEARCH_ROUTE}
query={query}
noResults={noResults}
isPositive={null}
isPositiveRating={null}
/>
</Container>
)
Expand Down
10 changes: 4 additions & 6 deletions web/src/components/__tests__/FeedbackContainer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ describe('FeedbackContainer', () => {
language,
onClose: closeModal,
query,
isPositive: null,
isPositiveRating: null,
})

it('should display thanks message for modal', async () => {
const { getByRole, findByText } = renderWithTheme(<FeedbackContainer {...buildDefaultProps(CATEGORIES_ROUTE)} />)
const buttonRating = getByRole('button', {
name: 'feedback:useful',
})
fireEvent.click(buttonRating)
const { getByRole, findByText } = renderWithTheme(
<FeedbackContainer {...buildDefaultProps(CATEGORIES_ROUTE)} isPositiveRating />,
)

Check warning on line 43 in web/src/components/__tests__/FeedbackContainer.spec.tsx

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Code Duplication

The module contains 2 functions with similar structure: 'should display thanks message for modal','should display thanks message for search'. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.
const button = getByRole('button', {
name: 'feedback:send',
})
Expand Down

0 comments on commit 57427dd

Please sign in to comment.