/*
 * @Description: 升降级切换锁定用户缺省页
 * @Author: taosu
 * @Date: 2022-03-10 10:03:02
 */
import React, { useEffect, useState } from 'react';
import LargeLoading from 'components/LargeLoading/LargeLoading';
import { request } from 'utils/fetch';
import { api } from 'constants/config';
import { useHistory } from 'react-router-dom';

let timeChange;

export default function UserLock() {
  const [isLock, setIsLock] = useState(true);
  const history = useHistory();

  useEffect(() => {
    clearInterval(timeChange);
    if (isLock) {
      timeChange = setInterval(async () => {
        const result = await request(api.userGet, undefined, undefined, undefined);
        if (result) {
          setIsLock(result?.isLocked);
        }
      }, 10 * 1000);
    } else {
      clearInterval(timeChange);
      history.replace('/');
    }
    return () => {
      // 清除定时器
      clearInterval(timeChange);
    };
  }, [isLock]);

  return (
    <div
      style={{
        display: 'flex',
        flexDirection: 'column',
        justifyContent: 'center',
        alignItems: 'center',
        height: '100vh',
        background: '#181924'
      }}>
      <LargeLoading spinning={true} tip="版本切换中..." delay={500} />
      <p style={{ color: '#B4B7C1', fontSize: 14 }}>当前产品版本已变更，系统处理将会花费一些时间进行切换</p>
    </div>
  );
}
