User Type Judgment

  • Ts requires type inference
  • The value of the enum type can be directly assigned at the initial time to facilitate update during subsequent maintenance
  • API requests need to be placed in a try/catch
  • In this case, loding should be set to ensure that the page loads correctly, otherwise enterprise Sion will appear and then normal content will be displayed
import React from 'react';
import api from '@/common/utils/api';

enum EModelUploadUserType {
    /** * 1- Individual user */
    PERSONAL_USER = 1./**
     * 2-付费商家
     */
    PAYING_MERCHANT = 2./** * 3- Other users (prompted to purchase enterprise edition) */
    OTHER_USER = 3,}export default function Layout() {
    const [userType, setUserType] = React.useState<EModelUploadUserType>(EModelUploadUserType.OTHER_USER);
    const [loading, setLoding] = React.useState<Boolean> (true);

    React.useEffect(() = > {
        try {
            api.get<EModelUploadUserType>('/modelshow/api/upload/model/user_type').then(res= > {
                setUserType(res);
                setLoding(false);
            });
        } catch (e) {
            setUserType(EModelUploadUserType.OTHER_USER);
            setLoding(false); }} []);if (loading) {
        return null;
    }
    
    if (userType === EModelUploadUserType.OTHER_USER) {
        return <EnterpriseVersion />;
    }

    return (
        <React.Fragment>
            <StyledListContainer id="bg-list-app" ref={bgListRef} />{/ *<StepsGuide />* /}</React.Fragment>
    );
}
Copy the code