diff --git a/scripts/insertCode.js b/scripts/insertCode.js index 1dc3d9ad24b3e694dc00aadab3a3cd3292b51a2e..d631dfa1434a16782b85fe88cf6631f98a0d3ce3 100644 --- a/scripts/insertCode.js +++ b/scripts/insertCode.js @@ -33,7 +33,7 @@ const SettingCodeString = ` - dispatch!({ + dispatch({ type: 'settings/changeSetting', payload: config, }) diff --git a/src/components/GlobalHeader/AvatarDropdown.tsx b/src/components/GlobalHeader/AvatarDropdown.tsx index 4454ccaceca1bb95a1300688242bda4358f7a546..5b135c6c3433c6bfa1f97a618ba940c6ccddc6c0 100644 --- a/src/components/GlobalHeader/AvatarDropdown.tsx +++ b/src/components/GlobalHeader/AvatarDropdown.tsx @@ -20,9 +20,12 @@ class AvatarDropdown extends React.Component { if (key === 'logout') { const { dispatch } = this.props; - dispatch!({ - type: 'login/logout', - }); + if (dispatch) { + dispatch({ + type: 'login/logout', + }); + } + return; } router.push(`/account/${key}`); diff --git a/src/components/GlobalHeader/NoticeIconView.tsx b/src/components/GlobalHeader/NoticeIconView.tsx index f79429aaeb604198991c51c9889b020e2c11da46..d79550b028811385c7a29d4efa2088354ea7e1bd 100644 --- a/src/components/GlobalHeader/NoticeIconView.tsx +++ b/src/components/GlobalHeader/NoticeIconView.tsx @@ -66,16 +66,20 @@ class GlobalHeaderRight extends Component { changeReadState = (clickedItem: NoticeItem) => { const { id } = clickedItem; const { dispatch } = this.props; - dispatch!({ - type: 'global/changeNoticeReadState', - payload: id, - }); + if (dispatch) { + dispatch({ + type: 'global/changeNoticeReadState', + payload: id, + }); + } }; componentDidMount() { const { dispatch } = this.props; - dispatch!({ - type: 'global/fetchNotices', - }); + if (dispatch) { + dispatch({ + type: 'global/fetchNotices', + }); + } } handleNoticeClear = (title: string, key: string) => { const { dispatch } = this.props; diff --git a/src/components/GlobalHeader/RightContent.tsx b/src/components/GlobalHeader/RightContent.tsx index 65fabe444faffdb241d90cd367b1f50bcf491f6c..cf4f5bf6e538f440a228aba3678427c470a36467 100644 --- a/src/components/GlobalHeader/RightContent.tsx +++ b/src/components/GlobalHeader/RightContent.tsx @@ -7,6 +7,7 @@ import SelectLang from '../SelectLang'; import styles from './index.less'; import Avatar from './AvatarDropdown'; import { connect } from 'dva'; + export type SiderTheme = 'light' | 'dark'; export interface GlobalHeaderRightProps extends ConnectProps { theme?: SiderTheme; diff --git a/src/layouts/BasicLayout.tsx b/src/layouts/BasicLayout.tsx index a5ead0026c4c377becf769d6be3408fe8971e892..33d94ff2833d76ab64bc71c47ef3c449f21cba7a 100644 --- a/src/layouts/BasicLayout.tsx +++ b/src/layouts/BasicLayout.tsx @@ -41,25 +41,28 @@ const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] => { }; const BasicLayout: React.FC = props => { - const { dispatch, children, settings, location } = props; + const { dispatch, children, settings } = props; /** * constructor */ useState(() => { - dispatch!({ - type: 'user/fetchCurrent', - }); - dispatch!({ - type: 'settings/getSetting', - }); + if (dispatch) { + dispatch({ + type: 'user/fetchCurrent', + }); + dispatch({ + type: 'settings/getSetting', + }); + } }); + /** * init variables */ - const handleMenuCollapse = (payload: boolean) => - dispatch!({ + dispatch && + dispatch({ type: 'global/changeLayoutCollapsed', payload, }); diff --git a/src/models/global.ts b/src/models/global.ts index 5f1d2bd71a8625429befa0fce0192c083bfa38dc..24a32d106f9c3d98d305ec28c7423792543eaca0 100644 --- a/src/models/global.ts +++ b/src/models/global.ts @@ -99,7 +99,7 @@ const GlobalModel: GlobalModelType = { }, reducers: { - changeLayoutCollapsed(state, { payload }) { + changeLayoutCollapsed(state = { notices: [], collapsed: true }, { payload }) { return { ...state, collapsed: payload, @@ -112,7 +112,7 @@ const GlobalModel: GlobalModelType = { notices: payload, }; }, - saveClearedNotices(state, { payload }) { + saveClearedNotices(state = { notices: [], collapsed: true }, { payload }) { return { collapsed: false, ...state, diff --git a/src/models/login.ts b/src/models/login.ts index e7cf877be00d33b6e9f6438fe7f2e9517151de6b..18dfb971529859bc2be8d7eeb9125b23402c59ce 100644 --- a/src/models/login.ts +++ b/src/models/login.ts @@ -1,28 +1,25 @@ import { routerRedux } from 'dva/router'; -import { Reducer } from 'redux'; +import { Reducer, AnyAction } from 'redux'; import { EffectsCommandMap } from 'dva'; -import { AnyAction } from 'redux'; import { stringify, parse } from 'qs'; export function getPageQuery() { return parse(window.location.href.split('?')[1]); } -export interface IStateType {} - export type Effect = ( action: AnyAction, - effects: EffectsCommandMap & { select: (func: (state: IStateType) => T) => T }, + effects: EffectsCommandMap & { select: (func: (state: {}) => T) => T }, ) => void; export interface ModelType { namespace: string; - state: IStateType; + state: {}; effects: { logout: Effect; }; reducers: { - changeLoginStatus: Reducer; + changeLoginStatus: Reducer<{}>; }; } diff --git a/src/models/setting.ts b/src/models/setting.ts index b7f9068e9dba8738e6be1de8a0a74542be7e9ad7..a5a278ae897862c362c09fb50dad54b06c59c5c7 100644 --- a/src/models/setting.ts +++ b/src/models/setting.ts @@ -6,8 +6,8 @@ export interface SettingModelType { namespace: 'settings'; state: DefaultSettings; reducers: { - getSetting: Reducer; - changeSetting: Reducer; + getSetting: Reducer; + changeSetting: Reducer; }; } let lessNodesAppended: boolean; @@ -72,7 +72,7 @@ const updateTheme: (primaryColor?: string) => void = primaryColor => { } }; -const updateColorWeak: (colorWeak: string) => void = colorWeak => { +const updateColorWeak: (colorWeak: boolean) => void = colorWeak => { const root = document.getElementById('root'); if (root) { root.className = colorWeak ? 'colorWeak' : ''; @@ -83,8 +83,8 @@ const SettingModel: SettingModelType = { namespace: 'settings', state: defaultSettings, reducers: { - getSetting(state) { - const setting: any = {}; + getSetting(state = defaultSettings) { + const setting: Partial = {}; const urlParams = new URL(window.location.href); Object.keys(state).forEach(key => { if (urlParams.searchParams.has(key)) { @@ -97,13 +97,13 @@ const SettingModel: SettingModelType = { if (state.primaryColor !== primaryColor) { updateTheme(primaryColor); } - updateColorWeak(colorWeak); + updateColorWeak(!!colorWeak); return { ...state, ...setting, }; }, - changeSetting(state, { payload }) { + changeSetting(state = defaultSettings, { payload }) { const urlParams = new URL(window.location.href); Object.keys(defaultSettings).forEach(key => { if (urlParams.searchParams.has(key)) { diff --git a/src/models/user.ts b/src/models/user.ts index 4bcdfba8e10d9267e12af8729364c96c1a4c942b..8f3b208a872d33535cab474a1b8e9b1175a2ae0d 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -9,13 +9,15 @@ export interface CurrentUser { group?: string; signature?: string; geographic?: any; - tags?: any[]; + tags?: { + key: string; + label: string; + }[]; unreadCount?: number; } export interface UserModelState { - list: any[]; - currentUser: CurrentUser; + currentUser?: CurrentUser; } export interface UserModelType { @@ -26,9 +28,8 @@ export interface UserModelType { fetchCurrent: Effect; }; reducers: { - save: Reducer; - saveCurrentUser: Reducer; - changeNotifyCount: Reducer; + saveCurrentUser: Reducer; + changeNotifyCount: Reducer; }; } @@ -36,7 +37,6 @@ const UserModel: UserModelType = { namespace: 'user', state: { - list: [], currentUser: {}, }, @@ -58,19 +58,18 @@ const UserModel: UserModelType = { }, reducers: { - save(state, action) { - return { - ...state, - list: action.payload, - }; - }, saveCurrentUser(state, action) { return { ...state, currentUser: action.payload || {}, }; }, - changeNotifyCount(state, action) { + changeNotifyCount( + state = { + currentUser: {}, + }, + action, + ) { return { ...state, currentUser: { diff --git a/src/pages/Authorized.tsx b/src/pages/Authorized.tsx index 561f0fde0c99b8e84bc5475d5ae35b2b94cdae94..6f502d36848ed9097ed63ec29f52e33e2e25628b 100644 --- a/src/pages/Authorized.tsx +++ b/src/pages/Authorized.tsx @@ -1,6 +1,5 @@ import Authorized from '@/utils/Authorized'; -import { Route } from '@/models/connect'; -import { ConnectProps, ConnectState, UserModelState } from '@/models/connect'; +import { ConnectProps, ConnectState, UserModelState, Route } from '@/models/connect'; import { connect } from 'dva'; import pathToRegexp from 'path-to-regexp'; import React from 'react'; @@ -11,7 +10,7 @@ interface AuthComponentProps extends ConnectProps { } const getRouteAuthority = (path: string, routeData: Route[]) => { - let authorities: string[] | string | undefined = void 0; + let authorities: string[] | string | undefined = undefined; routeData.forEach(route => { // match prefix if (pathToRegexp(`${route.path}(.*)`).test(path)) { diff --git a/src/pages/Welcome.tsx b/src/pages/Welcome.tsx index c9787fa25b2284e18d04085fb3d96f672a9326b3..1f78bed0fda63b325a19ff1d092e034e24970532 100644 --- a/src/pages/Welcome.tsx +++ b/src/pages/Welcome.tsx @@ -1,6 +1,6 @@ import React from 'react'; -const Welcome = () => ( +export default () => (

想要添加更多页面?请参考{' '} @@ -9,5 +9,3 @@ const Welcome = () => ( 。

); -Welcome.title = '欢迎使用'; -export default Welcome;