Skip to content

Commit

Permalink
fix: add strict mode & update users hook
Browse files Browse the repository at this point in the history
  • Loading branch information
tednaaa committed Sep 24, 2023
1 parent 9a2b381 commit d0c9747
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/app/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { createRoot } from 'react-dom/client';
import { App } from './app';

import './scss/main.scss';
import { StrictMode } from 'react';

const container = document.querySelector('#app') as HTMLElement;
const root = createRoot(container);

root.render(<App />);
root.render(
<StrictMode>
<App />
</StrictMode>
);
5 changes: 3 additions & 2 deletions src/modules/users/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { jsonApiInstance } from '@/shared/api/instances';
import { User } from './types';
import { GenericAbortSignal } from 'axios';

export const routes = {
users: '/users',
};

export const fetchUsers = () => {
return jsonApiInstance.get<User[]>(routes.users);
export const fetchUsers = (signal: GenericAbortSignal) => {
return jsonApiInstance.get<User[]>(routes.users, { signal });
};
12 changes: 10 additions & 2 deletions src/modules/users/lib/use-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ export const useUsers = () => {
const [error, setError] = useState<string>('');

useEffect(() => {
const abortController = new AbortController();

setLoading(true);

setTimeout(() => {
fetchUsers()
fetchUsers(abortController.signal)
.then(({ data }) => setUsers(data))
.catch(() => setError('Error fetching users'))
.catch((error) => {
const isNotCancelled = error.name !== 'CanceledError';

if (isNotCancelled) setError('Error fetching users');
})
.finally(() => setLoading(false));
}, 800);

return () => abortController.abort();
}, []);

return { users, isLoading, error };
Expand Down

0 comments on commit d0c9747

Please sign in to comment.