Skip to content

Commit 6105017

Browse files
committed
added react-hook rules to eslint
1 parent 6796e40 commit 6105017

File tree

17 files changed

+189
-177
lines changed

17 files changed

+189
-177
lines changed

packages/client/eslint.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ module.exports = [
4040
'plugin:@typescript-eslint/recommended',
4141
'plugin:import/recommended',
4242
'plugin:prettier/recommended',
43-
'plugin:jsx-a11y/recommended'
43+
'plugin:jsx-a11y/recommended',
44+
'plugin:react-hooks/recommended'
4445
),
4546
{
4647
plugins: {

packages/client/src/v2-events/components/DownloadButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export function DownloadButton({
100100
const assignmentStatus = getAssignmentStatus(event, authentication.sub)
101101

102102
const eventDocument = getEvent.findFromCache(event.id)
103-
const isAssignMutationFetching = actions.assignment.assign.isAssigning(
103+
const isAssignMutationFetching = actions.assignment.assign.useIsAssigning(
104104
event.id
105105
)
106106

packages/client/src/v2-events/components/forms/FormFieldGenerator/FormFieldGenerator.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export const FormFieldGenerator: React.FC<FormFieldGeneratorProps> = React.memo(
135135
{(formikProps) => {
136136
const { touched } = formikProps
137137

138+
// eslint-disable-next-line react-hooks/rules-of-hooks
138139
useEffect(() => {
139140
/**
140141
* Because 'enableReinitialize' prop is set to 'true' above, whenever initialValue changes,

packages/client/src/v2-events/features/drafts/useDrafts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export function useDrafts() {
182182
const localDraft = localDraftStore((drafts) => drafts.draft)
183183
const createDraft = useCreateDraft()
184184

185-
function getAllRemoteDrafts(
185+
function useRemoteDrafts(
186186
additionalOptions: QueryOptions<typeof trpc.event.draft.list> = {}
187187
): Draft[] {
188188
// Skip the queryFn defined by tRPC and use the one defined above
@@ -238,12 +238,12 @@ export function useDrafts() {
238238
status: localDraft.action.status
239239
})
240240
},
241-
getAllRemoteDrafts,
241+
useRemoteDrafts,
242242
getRemoteDraftByEventId: function useDraftList(
243243
eventId: string,
244244
additionalOptions: QueryOptions<typeof trpc.event.draft.list> = {}
245245
): Draft | undefined {
246-
const eventDrafts = getAllRemoteDrafts(additionalOptions).filter(
246+
const eventDrafts = useRemoteDrafts(additionalOptions).filter(
247247
(draft) => draft.eventId === eventId
248248
)
249249

packages/client/src/v2-events/features/events/Search/AdvancedSearch.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import { useEventConfigurations } from '@client/v2-events/features/events/useEventConfiguration'
2626
import { TabSearch } from './TabSearch'
2727
import {
28-
checkScopeForEventSearch,
28+
useEventSearchScope,
2929
parseFieldSearchParams,
3030
deserializeSearchParams
3131
} from './utils'
@@ -55,6 +55,7 @@ export function AdvancedSearch() {
5555
deserializeSearchParams(location.search)
5656
)
5757

58+
const checkScopeForEventSearch = useEventSearchScope()
5859
const advancedSearchEvents = allEvents.filter(
5960
(event) =>
6061
event.advancedSearch.length > 0 && checkScopeForEventSearch(event.id)
@@ -89,6 +90,16 @@ export function AdvancedSearch() {
8990

9091
const [activeTabId, setActiveTabId] = useState<string>(selectedTabId)
9192

93+
const handleFormChange = useCallback(
94+
(updatedForm: Record<string, FieldValue>) => {
95+
setFormValuesByTabId((prev) => ({
96+
...prev,
97+
[activeTabId]: updatedForm
98+
}))
99+
},
100+
[activeTabId]
101+
)
102+
92103
const currentEvent = allEvents.find((e) => e.id === activeTabId)
93104
if (!currentEvent) {
94105
return null
@@ -101,16 +112,6 @@ export function AdvancedSearch() {
101112
setActiveTabId(tabId)
102113
}
103114

104-
const handleFormChange = useCallback(
105-
(updatedForm: Record<string, FieldValue>) => {
106-
setFormValuesByTabId((prev) => ({
107-
...prev,
108-
[activeTabId]: updatedForm
109-
}))
110-
},
111-
[activeTabId]
112-
)
113-
114115
return (
115116
<Content
116117
size={ContentSize.SMALL}

packages/client/src/v2-events/features/events/Search/SearchResult.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ export const SearchResultComponent = ({
233233
)
234234
}
235235
const { getOutbox } = useEvents()
236-
const { getAllRemoteDrafts } = useDrafts()
236+
const { useRemoteDrafts } = useDrafts()
237237
const outbox = getOutbox()
238-
const drafts = getAllRemoteDrafts()
238+
const drafts = useRemoteDrafts()
239239

240240
const [sortedCol, setSortedCol] = useState<
241241
(typeof COLUMNS)[keyof typeof COLUMNS]

packages/client/src/v2-events/features/events/Search/utils.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -744,15 +744,18 @@ export function buildQuickSearchQuery(
744744
/**
745745
* @returns a boolean indicating whether the current user has the scope to search for an event
746746
*/
747-
export function checkScopeForEventSearch(eventId: string) {
747+
export function useEventSearchScope() {
748748
const scopes = useSelector(getScope)
749-
const searchScopes = findScope(scopes ?? [], 'search')
749+
const checkScopeForEventSearch = (eventId: string) => {
750+
const searchScopes = findScope(scopes ?? [], 'search')
750751

751-
const isEventSearchAllowed =
752-
searchScopes &&
753-
Object.keys(searchScopes.options).some((id) => eventId === id)
752+
const isEventSearchAllowed =
753+
searchScopes &&
754+
Object.keys(searchScopes.options).some((id) => eventId === id)
754755

755-
return isEventSearchAllowed
756+
return isEventSearchAllowed
757+
}
758+
return checkScopeForEventSearch
756759
}
757760

758761
function serializeValue(value: unknown) {

packages/client/src/v2-events/features/events/actions/dedup/ReviewDuplicate.tsx

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import styled from 'styled-components'
1717
import { useNavigate } from 'react-router-dom'
1818
import {
1919
ActionType,
20+
EventDocument,
2021
EventIndex,
2122
getActionReview,
2223
getCurrentEventState,
@@ -123,33 +124,11 @@ const TopBar = styled.div`
123124
position: sticky;
124125
z-index: 1;
125126
`
126-
127-
function ReviewDuplicate() {
128-
const { eventId } = useTypedParams(ROUTES.V2.EVENTS.DECLARE.REVIEW)
129-
130-
const validatorContext = useValidatorContext()
131-
127+
function ReviewDuplicateLoaded({ event }: { event: EventDocument }) {
132128
const intl = useIntl()
133-
const navigate = useNavigate()
134-
const events = useEvents()
135-
const event = events.getEvent.findFromCache(eventId).data
136-
137-
useEffect(() => {
138-
if (!event) {
139-
// eslint-disable-next-line no-console
140-
console.warn(
141-
`Event with id ${eventId} not found in cache. Redirecting to overview.`
142-
)
143-
return navigate(ROUTES.V2.EVENTS.OVERVIEW.buildPath({ eventId }))
144-
}
145-
}, [event, eventId, navigate])
146-
147-
if (!event) {
148-
return <div />
149-
}
150-
129+
const validatorContext = useValidatorContext()
151130
const [selectedTab, selectTab] = useState<string>(event.trackingId)
152-
131+
const { formatMessage } = useIntlFormatMessageWithFlattenedParams()
153132
const { eventConfiguration: configuration } = useEventConfiguration(
154133
event.type
155134
)
@@ -183,8 +162,6 @@ function ReviewDuplicate() {
183162
]
184163

185164
const { title, fields } = getActionReview(configuration, ActionType.READ)
186-
const { formatMessage } = useIntlFormatMessageWithFlattenedParams()
187-
188165
const formConfig = getDeclaration(configuration)
189166

190167
return (
@@ -232,4 +209,27 @@ function ReviewDuplicate() {
232209
)
233210
}
234211

212+
function ReviewDuplicate() {
213+
const navigate = useNavigate()
214+
const events = useEvents()
215+
const { eventId } = useTypedParams(ROUTES.V2.EVENTS.DECLARE.REVIEW)
216+
const { data: event } = events.getEvent.findFromCache(eventId)
217+
218+
useEffect(() => {
219+
if (!event) {
220+
// eslint-disable-next-line no-console
221+
console.warn(
222+
`Event with id ${eventId} not found in cache. Redirecting to overview.`
223+
)
224+
return navigate(ROUTES.V2.EVENTS.OVERVIEW.buildPath({ eventId }))
225+
}
226+
}, [event, eventId, navigate])
227+
228+
if (!event) {
229+
return <div />
230+
}
231+
232+
return <ReviewDuplicateLoaded event={event} />
233+
}
234+
235235
export const ReviewDuplicateIndex = withSuspense(ReviewDuplicate)

packages/client/src/v2-events/features/events/actions/validate/Review.tsx

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
getActionAnnotation,
2323
getDeclaration,
2424
getActionReview,
25-
InherentFlags
25+
InherentFlags,
26+
EventDocument
2627
} from '@opencrvs/commons/client'
2728
import { ROUTES } from '@client/v2-events/routes'
2829
import { useEvents } from '@client/v2-events/features/events/useEvents/useEvents'
@@ -48,57 +49,38 @@ import { useReviewActionConfig } from './useReviewActionConfig'
4849
*
4950
* Preview of event to be validated.
5051
*/
51-
export function Review() {
52+
function ReviewContent({ event }: { event: EventDocument }) {
53+
const navigate = useNavigate()
54+
const events = useEvents()
55+
const drafts = useDrafts()
5256
const { eventId } = useTypedParams(ROUTES.V2.EVENTS.VALIDATE)
5357
const [{ workqueue: slug }] = useTypedSearchParams(
5458
ROUTES.V2.EVENTS.VALIDATE.REVIEW
5559
)
56-
const events = useEvents()
57-
const drafts = useDrafts()
5860
const [modal, openModal] = useModal()
59-
const navigate = useNavigate()
6061
const { closeActionView } = useEventFormNavigation()
61-
const validatorContext = useValidatorContext()
62-
63-
const event = events.getEvent.findFromCache(eventId).data
64-
65-
useEffect(() => {
66-
if (!event) {
67-
// eslint-disable-next-line no-console
68-
console.warn(
69-
`Event with id ${eventId} not found in cache. Redirecting to overview.`
70-
)
71-
return navigate(ROUTES.V2.EVENTS.OVERVIEW.buildPath({ eventId }))
72-
}
73-
}, [event, eventId, navigate])
74-
75-
if (!event) {
76-
return <div />
77-
}
7862

7963
const { setAnnotation, getAnnotation } = useActionAnnotation()
8064

8165
const { saveAndExitModal, handleSaveAndExit } = useSaveAndExitModal()
66+
const { formatMessage } = useIntlFormatMessageWithFlattenedParams()
67+
const getFormValues = useEventFormData((state) => state.getFormValues)
8268

8369
const previousAnnotation = getActionAnnotation({
8470
event,
8571
actionType: ActionType.VALIDATE
8672
})
8773

88-
const annotation = getAnnotation(previousAnnotation)
89-
9074
const { eventConfiguration: config } = useEventConfiguration(event.type)
9175

9276
const formConfig = getDeclaration(config)
77+
const form = getFormValues()
78+
const annotation = getAnnotation(previousAnnotation)
9379
const reviewConfig = getActionReview(config, ActionType.VALIDATE)
94-
const { formatMessage } = useIntlFormatMessageWithFlattenedParams()
95-
96-
const getFormValues = useEventFormData((state) => state.getFormValues)
97-
9880
const currentEventState = getCurrentEventState(event, config)
99-
const previousFormValues = currentEventState.declaration
100-
const form = getFormValues()
81+
const validatorContext = useValidatorContext()
10182

83+
const previousFormValues = currentEventState.declaration
10284
const reviewActionConfiguration = useReviewActionConfig({
10385
formConfig,
10486
declaration: form,
@@ -245,3 +227,24 @@ export function Review() {
245227
</FormLayout>
246228
)
247229
}
230+
export function Review() {
231+
const navigate = useNavigate()
232+
const events = useEvents()
233+
const { eventId } = useTypedParams(ROUTES.V2.EVENTS.VALIDATE)
234+
const event = events.getEvent.findFromCache(eventId).data
235+
useEffect(() => {
236+
if (!event) {
237+
// eslint-disable-next-line no-console
238+
console.warn(
239+
`Event with id ${eventId} not found in cache. Redirecting to overview.`
240+
)
241+
return navigate(ROUTES.V2.EVENTS.OVERVIEW.buildPath({ eventId }))
242+
}
243+
}, [event, eventId, navigate])
244+
245+
if (!event) {
246+
return <div />
247+
}
248+
249+
return <ReviewContent event={event} />
250+
}

packages/client/src/v2-events/features/events/components/SearchToolbar.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ export const SearchToolbar = () => {
119119

120120
const scopes = useSelector(getScope) ?? []
121121

122+
useEffect(() => {
123+
// Clear the search term when navigating away from the search results page
124+
if (location.pathname !== ROUTES.V2.SEARCH.buildPath({})) {
125+
setSearchTerm(undefined)
126+
}
127+
}, [location.pathname])
128+
122129
const hasSearchScope = scopes.some((scope) => scope.startsWith('search'))
123130
if (!hasSearchScope) {
124131
return null
@@ -145,13 +152,6 @@ export const SearchToolbar = () => {
145152
navigate(`${searchUrl}?${serializedParams}`)
146153
}
147154

148-
useEffect(() => {
149-
// Clear the search term when navigating away from the search results page
150-
if (location.pathname !== ROUTES.V2.SEARCH.buildPath({})) {
151-
setSearchTerm(undefined)
152-
}
153-
}, [location.pathname])
154-
155155
return (
156156
<SearchBox className={'search-tool'}>
157157
<Wrapper onSubmit={onSearch}>

0 commit comments

Comments
 (0)