Navigation Menu

Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricedb committed Jun 10, 2019
1 parent dac90ce commit 7889ab8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
50 changes: 29 additions & 21 deletions src/fetchData.ts
Expand Up @@ -13,12 +13,14 @@ const fetchData = async <T>(

let rsp: Response | null = null;
try {
setState((oldState: FetchState<T>) => ({
data: null,
loading: oldState.loading + 1,
error: null,
controller
}));
setState(
(oldState: FetchState<T>): FetchState<T> => ({
data: null,
loading: oldState.loading + 1,
error: null,
controller
})
);

rsp = await fetch(url, actualInit);
const contentTypeHeader = rsp.headers.get('content-type');
Expand All @@ -31,16 +33,20 @@ const fetchData = async <T>(
} else {
data = await rsp.text();
}
setState((oldState: FetchState<T>) => ({
...oldState,
data,
loading: oldState.loading - 1
}));
setState(
(oldState: FetchState<T>): FetchState<T> => ({
...oldState,
data,
loading: oldState.loading - 1
})
);
} else {
setState((oldState: FetchState<T>) => ({
...oldState,
loading: oldState.loading - 1
}));
setState(
(oldState: FetchState<T>): FetchState<T> => ({
...oldState,
loading: oldState.loading - 1
})
);
}

if (!rsp.ok) {
Expand All @@ -53,12 +59,14 @@ const fetchData = async <T>(

const error = err.name !== 'AbortError' ? err : null;

setState((oldState: FetchState<T>) => ({
...oldState,
error,
// Only decrease the loading counter if there is no repsonse
loading: rsp ? oldState.loading : oldState.loading - 1
}));
setState(
(oldState: FetchState<T>): FetchState<T> => ({
...oldState,
error,
// Only decrease the loading counter if there is no repsonse
loading: rsp ? oldState.loading : oldState.loading - 1
})
);
}
};

Expand Down
8 changes: 4 additions & 4 deletions src/index.test.tsx
Expand Up @@ -24,7 +24,7 @@ const LoadData = (): JSX.Element => {
);
};

test('Renders the data after success', async () => {
test('Renders the data after success', async (): Promise<void> => {
(fetch as any).mockResponseOnce(JSON.stringify({ x: 1 }), {
headers: {
'content-type': 'application/json'
Expand All @@ -34,13 +34,13 @@ test('Renders the data after success', async () => {
const { getByText } = render(<LoadData />);

getByText('Loading: true');
await waitForElement(() => getByText('Loading: false'));
await waitForElement((): any => getByText('Loading: false'));

getByText('Data: {"x":1}');
getByText('Error: null');
});

test('Renders the error after failure', async () => {
test('Renders the error after failure', async (): Promise<void> => {
(fetch as any).mockResponseOnce(
JSON.stringify({
x: 2
Expand All @@ -56,7 +56,7 @@ test('Renders the error after failure', async () => {
const { getByText } = render(<LoadData />);

getByText('Loading: true');
await waitForElement(() => getByText('Loading: false'));
await waitForElement((): any => getByText('Loading: false'));

getByText('Data: {"x":2}');
getByText('Error: {"status":404}');
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Expand Up @@ -15,31 +15,31 @@ const useAbortableFetch = <T>(
});

const isMounted = useRef(false);
useLayoutEffect(() => {
useLayoutEffect((): (() => void) => {
isMounted.current = true;
return () => {
return (): void => {
isMounted.current = false;
};
}, []);

useEffect(() => {
useEffect((): (() => void) => {
const controller = new AbortController();
if (url) {
fetchData<T>(url, init, controller, state => {
fetchData<T>(url, init, controller, (state): void => {
if (isMounted.current) {
setState(state);
}
});
}

return () => controller.abort();
return (): void => controller.abort();
}, [init, url]);

return {
data: state.data,
loading: !!state.loading,
error: state.error,
abort: () => state.controller && state.controller.abort()
abort: (): null | void => state.controller && state.controller.abort()
};
};

Expand Down
4 changes: 2 additions & 2 deletions src/isJSON.test.ts
Expand Up @@ -18,7 +18,7 @@ test.each([
'application/vnd.restful+json',
'text/json',
'model/gltf+json'
])('Content type "%s" should be JSON', (contentType: string) => {
])('Content type "%s" should be JSON', (contentType: string): void => {
expect(isJSON(contentType)).toBeTruthy();
});

Expand All @@ -32,7 +32,7 @@ test.each([
'text/html'
])(
'Content type "%s" should not be JSON',
(contentType: string | null | undefined) => {
(contentType: string | null | undefined): void => {
expect(isJSON(contentType)).toBeFalsy();
}
);

0 comments on commit 7889ab8

Please sign in to comment.