Skip to content

Commit

Permalink
Merge pull request IQSS#359 from IQSS/feature/347-eslint-plugin-react…
Browse files Browse the repository at this point in the history
…-hooks

347 Add eslint plugin react hooks
  • Loading branch information
GPortas authored May 22, 2024
2 parents 0a4d267 + 2454b3c commit b34292e
Show file tree
Hide file tree
Showing 20 changed files with 54 additions and 30 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"eslint-plugin-import": "2.27.5",
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-react": "7.32.2",
"eslint-plugin-react-hooks": "4.6.0",
"eslint-plugin-simple-import-sort": "10.0.0",
"eslint-plugin-storybook": "0.6.11",
"eslint-plugin-unused-imports": "2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/sections/collection/datasets-list/DatasetsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function DatasetsList({ datasetRepository, page, collectionId }: Datasets

useEffect(() => {
setIsLoading(isLoading)
}, [isLoading])
}, [isLoading, setIsLoading])

if (isLoading) {
return <DatasetsListSkeleton />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function DatasetsListWithInfiniteScroll({

useEffect(() => {
setIsLoading(isLoading)
}, [isLoading])
}, [isLoading, setIsLoading])

useEffect(() => {
const updatePaginationTotalItems = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/sections/create-dataset/useGetMetadataBlocksInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const useGetMetadataBlocksInfo = ({
}
}
void handleGetDatasetMetadataBlockFields()
}, [])
}, [collectionId, metadataBlockInfoRepository])

return {
metadataBlocks,
Expand Down
2 changes: 1 addition & 1 deletion src/sections/dataset/Dataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function Dataset({ fileRepository, created }: DatasetProps) {
}
useEffect(() => {
setIsLoading(isLoading)
}, [isLoading])
}, [isLoading, setIsLoading])

if (isLoading) {
return <DatasetSkeleton />
Expand Down
2 changes: 1 addition & 1 deletion src/sections/dataset/DatasetFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function DatasetWithSearchParams() {

useEffect(() => {
if (privateUrlToken) setAnonymizedView(true)
}, [privateUrlToken])
}, [privateUrlToken, setAnonymizedView])

if (privateUrlToken) {
return (
Expand Down
9 changes: 5 additions & 4 deletions src/sections/dataset/DatasetProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PropsWithChildren, useEffect, useState } from 'react'
import { PropsWithChildren, useCallback, useEffect, useState } from 'react'
import { DatasetContext } from './DatasetContext'
import { DatasetRepository } from '../../dataset/domain/repositories/DatasetRepository'
import { Dataset } from '../../dataset/domain/models/Dataset'
Expand All @@ -20,15 +20,16 @@ export function DatasetProvider({
}: PropsWithChildren<DatasetProviderProps>) {
const [dataset, setDataset] = useState<Dataset>()
const [isLoading, setIsLoading] = useState(true)
const getDataset = () => {

const getDataset = useCallback(() => {
if (searchParams.persistentId) {
return getDatasetByPersistentId(repository, searchParams.persistentId, searchParams.version)
}
if (searchParams.privateUrlToken) {
return getDatasetByPrivateUrlToken(repository, searchParams.privateUrlToken)
}
return Promise.resolve(undefined)
}
}, [repository, searchParams])

useEffect(() => {
setIsLoading(true)
Expand All @@ -42,7 +43,7 @@ export function DatasetProvider({
console.error('There was an error getting the dataset', error)
setIsLoading(false)
})
}, [repository, searchParams])
}, [repository, searchParams, getDataset])

return (
<DatasetContext.Provider value={{ dataset, isLoading }}>{children}</DatasetContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ interface DeaccessionDatasetButtonProps {
dataset: Dataset
}
export function DeaccessionDatasetButton({ dataset }: DeaccessionDatasetButtonProps) {
const { t } = useTranslation('dataset')

if (
!dataset.version.someDatasetVersionHasBeenReleased ||
!dataset.permissions.canPublishDataset
) {
return <></>
}

const { t } = useTranslation('dataset')
return (
<>
<DropdownSeparator />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ interface DeleteDatasetButtonProps {
dataset: Dataset
}
export function DeleteDatasetButton({ dataset }: DeleteDatasetButtonProps) {
const { t } = useTranslation('dataset')

if (
!dataset.permissions.canDeleteDataset ||
dataset.version.latestVersionPublishingStatus !== DatasetPublishingStatus.DRAFT
) {
return <></>
}

const { t } = useTranslation('dataset')
return (
<>
<DropdownSeparator />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ interface EditDatasetPermissionsMenuProps {
dataset: Dataset
}
export function EditDatasetPermissionsMenu({ dataset }: EditDatasetPermissionsMenuProps) {
if (
!dataset.permissions.canManageDatasetPermissions &&
!dataset.permissions.canManageFilesPermissions
) {
return <></>
}

const { t } = useTranslation('dataset')
const { getSettingByName } = useSettings()
const [hasPublicStore, setHasPublicStore] = useState<HasPublicStore>(false)
Expand All @@ -31,6 +24,13 @@ export function EditDatasetPermissionsMenu({ dataset }: EditDatasetPermissionsMe
})
}, [getSettingByName])

if (
!dataset.permissions.canManageDatasetPermissions &&
!dataset.permissions.canManageFilesPermissions
) {
return <></>
}

if (hasPublicStore) {
return (
<DropdownButtonItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ interface PublishDatasetMenuProps {

export function PublishDatasetMenu({ dataset }: PublishDatasetMenuProps) {
const { user } = useSession()
const { t } = useTranslation('dataset')
const { showModal } = useNotImplementedModal()

if (
!dataset.version.isLatest ||
dataset.version.publishingStatus !== DatasetPublishingStatus.DRAFT ||
Expand All @@ -20,12 +23,10 @@ export function PublishDatasetMenu({ dataset }: PublishDatasetMenuProps) {
return <></>
}

const { t } = useTranslation('dataset')
const handleSelect = () => {
// TODO - Implement upload files
showModal()
}
const { showModal } = useNotImplementedModal()

return (
<DropdownButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface SubmitForReviewButtonProps {

export function SubmitForReviewButton({ dataset }: SubmitForReviewButtonProps) {
const { user } = useSession()
const { t } = useTranslation('dataset')

if (
!dataset.version.isLatest ||
dataset.version.publishingStatus !== DatasetPublishingStatus.DRAFT ||
Expand All @@ -20,7 +22,6 @@ export function SubmitForReviewButton({ dataset }: SubmitForReviewButtonProps) {
return <></>
}

const { t } = useTranslation('dataset')
return (
<Button
variant="secondary"
Expand Down
6 changes: 3 additions & 3 deletions src/sections/dataset/dataset-files/files-table/FilesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ export function FilesTable({
)
const [visitedPagination, setVisitedPagination] = useState<FilePaginationInfo>(paginationInfo)
const [visitedFiles, setVisitedFiles] = useState<FileSelection>({})
const [previousCriteria, setPreviousCriteria] = useState<FileCriteria>(criteria)

useEffect(() => {
if (visitedPagination.page == paginationInfo.page) {
setVisitedFiles((visitedFiles) => ({ ...visitedFiles, ...fileSelection }))
}
setVisitedPagination(paginationInfo)
}, [fileSelection])
}, [fileSelection, paginationInfo, visitedPagination.page])

const [previousCriteria, setPreviousCriteria] = useState<FileCriteria>(criteria)
useEffect(() => {
if (previousCriteria != criteria) {
clearFileSelection()
}
setPreviousCriteria(criteria)
})
}, [criteria, previousCriteria, clearFileSelection])

if (isLoading) {
return <SpinnerSymbol />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ export function useFilesTable(files: FilePreview[], paginationInfo: FilePaginati
pageCount: paginationInfo.totalPages
})

const selectedRowsById = table.getSelectedRowModel().rowsById

useEffect(() => {
table.setPageSize(paginationInfo.pageSize)
table.setPageIndex(paginationInfo.page - 1)
}, [paginationInfo])
}, [paginationInfo, table])

useEffect(() => {
setCurrentPageSelectedRowModel(table.getSelectedRowModel().rowsById)
}, [table.getSelectedRowModel().rowsById])
setCurrentPageSelectedRowModel(selectedRowsById)
}, [selectedRowsById])

return {
table,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function ZipDownloadLimitMessage({
filesTotalDownloadSize
)
setFileSelectionTotalSizeInInBytes(totalSize)
}, [fileSelection])
}, [fileSelection, visitedFiles, filesTotalDownloadSize])

const showMessage =
zipDownloadLimitInBytes &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function DatasetMetadataFields({

useEffect(() => {
setMetadataBlockName(metadataBlockName)
}, [metadataBlockName])
}, [metadataBlockName, setMetadataBlockName])

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion src/sections/file/File.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function File({ repository, id, datasetVersionNumber }: FileProps) {

useEffect(() => {
setIsLoading(isLoading)
}, [isLoading])
}, [isLoading, setIsLoading])

if (isLoading) {
return <FileSkeleton />
Expand Down
2 changes: 2 additions & 0 deletions src/stories/dataset/dataset-alerts/DatasetAlert.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Story = StoryObj<typeof DatasetAlerts>
export const UpdateAlerts: Story = {
render: () => {
const dataset = DatasetMother.createRealistic()
// eslint-disable-next-line react-hooks/rules-of-hooks
const { addDatasetAlert } = useAlertContext()
allUpdateAlerts.forEach((alert) => addDatasetAlert(alert))
return (
Expand All @@ -44,6 +45,7 @@ const publishAlert = new Alert('warning', AlertMessageKey.PUBLISH_IN_PROGRESS)
export const PublishInProgress: Story = {
render: () => {
const dataset = DatasetMother.createRealistic()
// eslint-disable-next-line react-hooks/rules-of-hooks
const { addDatasetAlert } = useAlertContext()
addDatasetAlert(publishAlert)
return (
Expand Down

0 comments on commit b34292e

Please sign in to comment.