Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from 'react';
  2. import { useDispatch, useSelector } from 'react-redux';
  3. import { getToken } from '../../services/api/tokenService';
  4. import useActions from '../../hooks/useActions';
  5. import { AppState } from '../../reducers';
  6. import Spinner from '../../spinner/spinner';
  7. import { getCurrentUser } from '../../services/api/usersService';
  8. import * as userActions from '../../actions/user/user-action';
  9. import { bindActionCreators } from 'redux';
  10.  
  11. const UserContainer = () => {
  12.     const [loading, setLoading] = useState(false);
  13.  
  14.     const token = getToken();
  15.     if (!token) {
  16.         throw new Error('Ошибка. Нет токена!');
  17.     }
  18.  
  19.     const user = useSelector((s: AppState) => s.user.currentUser);
  20.     // todo: разобраться с типами, и обновить useActions, что бы он стал типизированным
  21.     const dispatch = useDispatch();
  22.     const { setUser } = bindActionCreators(userActions, dispatch);
  23.  
  24.     if (!user) {
  25.         setLoading(true);
  26.         getCurrentUser()
  27.             .then(user => {
  28.                 setUser(user);
  29.                 setLoading(false);
  30.             })
  31.             .catch(err => {
  32.                 throw new Error(err);
  33.             });
  34.     }
  35.  
  36.     return (
  37.         <React.Fragment>
  38.             {loading ? (
  39.                 <Spinner message={'Загрузка пользователя...'} />
  40.             ) : (
  41.                 <div>
  42.                     Мы тут подгрузили пидора {user ? user.displayName : ''}
  43.                 </div>
  44.             )}
  45.         </React.Fragment>
  46.     );
  47. };
  48.  
  49. export default UserContainer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement