Skip to content

Commit 112187b

Browse files
authored
Merge pull request #1394 from guardian/pf/react-hooks-linting
Add react hooks lint rules, fix errors and some warnings
2 parents f2353e8 + af2bed6 commit 112187b

File tree

5 files changed

+290
-39
lines changed

5 files changed

+290
-39
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ module.exports = {
99
jsx: true
1010
}
1111
},
12-
plugins: ['react', 'prettier'],
12+
plugins: ['react', 'react-hooks', 'prettier'],
1313
rules: {
1414
'react/prop-types': 'off',
15+
'react-hooks/rules-of-hooks': 'error',
16+
'react-hooks/exhaustive-deps': 'warn',
1517
'jsx-quotes': ['error', 'prefer-double'],
1618
'prefer-const': 'error',
1719
semi: ['warn', 'always'],

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"eslint-config-prettier": "9.1.0",
5555
"eslint-plugin-prettier": "5.2.1",
5656
"eslint-plugin-react": "7.35.0",
57+
"eslint-plugin-react-hooks": "7.0.0",
5758
"hoconjs": "github:yellowblood/hocon-js",
5859
"jest": "29.7.0",
5960
"jest-environment-jsdom": "^30.1.2",
Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import React, {useEffect, useState} from 'react';
21
import PropTypes from 'prop-types';
2+
import React, { useEffect, useState } from 'react';
33
import { Tab, TabPanel } from 'react-tabs';
4-
import {getPlutoItemById} from "../../../services/PlutoApi";
5-
import PlutoProjectLink from "../../../components/Pluto/PlutoProjectLink";
4+
import PlutoProjectLink from '../../../components/Pluto/PlutoProjectLink';
5+
import { getPlutoItemById } from '../../../services/PlutoApi';
66

77
export class PlutoTab extends React.Component {
88
static tabsRole = Tab.tabsRole;
99

1010
render() {
11-
return (
12-
<Tab {...this.props}>
13-
Pluto
14-
</Tab>
15-
);
11+
return <Tab {...this.props}>Pluto</Tab>;
1612
}
1713
}
1814

@@ -29,35 +25,46 @@ export class PlutoTabPanel extends React.Component {
2925
return (
3026
<TabPanel {...rest}>
3127
<div className="form__group">
32-
{
33-
video.plutoData &&
34-
<PlutoProjectLink projectId={video.plutoData.projectId}/>
35-
}
28+
{video.plutoData && (
29+
<PlutoProjectLink projectId={video.plutoData.projectId} />
30+
)}
3631

3732
<header className="video__detailbox__header">Commission</header>
38-
<ReadOnlyPlutoItem id={video.plutoData && video.plutoData.commissionId} itemType="commission" />
33+
<ReadOnlyPlutoItem
34+
id={video.plutoData?.commissionId}
35+
itemType="commissions"
36+
/>
3937

4038
<header className="video__detailbox__header">Project</header>
41-
<ReadOnlyPlutoItem id={video.plutoData && video.plutoData.projectId} itemType="project" />
42-
39+
<ReadOnlyPlutoItem
40+
id={video.plutoData?.projectId}
41+
itemType="projects"
42+
/>
4343
</div>
4444
</TabPanel>
4545
);
4646
}
4747
}
4848

49-
const ReadOnlyPlutoItem = ({id, itemType}) => {
49+
/**
50+
*
51+
* @param {{id: string, itemType: import('../../../services/PlutoApi').PlutoItemType}} param0
52+
* @returns
53+
*/
54+
const ReadOnlyPlutoItem = ({ id, itemType }) => {
55+
const [title, setTitle] = useState(id ? 'Loading...' : '');
5056

51-
const [ title, setTitle ] = useState(id ? "Loading..." : "");
52-
53-
if(id) {
54-
useEffect(() => {
55-
getPlutoItemById(id, itemType).then(data => setTitle(data.title)).catch(e => {
56-
const errorMessage = `Failed to lookup ${itemType} with ID '${id}'`;
57-
console.error(errorMessage, e);
58-
setTitle(errorMessage);
59-
});}, []);
60-
}
57+
useEffect(() => {
58+
if (id) {
59+
getPlutoItemById(id, itemType)
60+
.then(data => setTitle(data.title))
61+
.catch(e => {
62+
const errorMessage = `Failed to lookup ${itemType} with ID '${id}'`;
63+
console.error(errorMessage, e);
64+
setTitle(errorMessage);
65+
});
66+
}
67+
}, [id, itemType]);
6168

6269
return <p className="details-list__field">{title}</p>;
6370
};

public/video-ui/src/services/PlutoApi.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { apiRequest } from './apiRequest';
22

3-
type PlutoItemType = 'commissions' | 'projects'
3+
type PlutoItemType = 'commissions' | 'projects';
44

55
export type PlutoCommission = {
6-
id: string,
7-
title: string
8-
}
6+
id: string;
7+
title: string;
8+
};
99

1010
export type PlutoProject = {
11-
id: string,
12-
title: string,
13-
status: string,
14-
commissionId: string,
15-
commissionTitle: string, //TODO remove this once migrated
16-
productionOffice: string
17-
}
11+
id: string;
12+
title: string;
13+
status: string;
14+
commissionId: string;
15+
commissionTitle: string; //TODO remove this once migrated
16+
productionOffice: string;
17+
};
1818

1919
export function getPlutoCommissions() {
2020
return apiRequest<PlutoCommission[]>({
@@ -30,7 +30,7 @@ export function getPlutoProjects({ commissionId }: { commissionId: string }) {
3030

3131
export function getPlutoItemById(id: string, itemType: PlutoItemType) {
3232
return apiRequest<PlutoProject | PlutoCommission>({
33-
url: `/api/pluto/${itemType}s/${id}`
33+
url: `/api/pluto/${itemType}/${id}`
3434
});
3535
}
3636

0 commit comments

Comments
 (0)