news-pubish-management(1)--layout
Here I am going to perform the layout first for news-publish-management
1. Setup setupProxy.js
1  | yarn add http-proxy-middleware  | 
copy below code-snippets into setupProxy.
1  | const { createProxyMiddleware } = require('http-proxy-middleware');  | 
Every each JS file we can add below code-snippets for creating react function component quickly.
1  | import React from 'react'  | 
We can install a plugin ES7 React/Redux/GraphQL/React-Native snippets for VS Code. Once install successfully, use key
rfcfor lightning generating above code-snippets.
We assume src/router/IndexRouter.js is the first level router, when user already logined the system, they will be redirected to src/views/sandbox/NewsSandBox.js, otherwise they will be redireted to src/views/login/Login.js. So the IndexRouter.js looks like below
1  | import React from 'react'  | 
Go to src/App.js, and import our src/router/IndexRouter.js component.
1  | import React from 'react'  | 
Go to browser, url https//localhost:3000 will show our Login component. If you manually setup the token with command of 
1  | localStorage.setItem("token","token")  | 
then the browser will show NewsSandBox component.
3. Assume NewsSandBox.js as a second level router, and change its content to
1  | import React from 'react'  | 
4. Import Ant Design and setup layout, our layout was designed in src/views/sandbox/NewsSandBox.js. NewsSandBox contains src/components/sandbox/TopHeader.js and src/componets/sandbox/SideMenu.js`
1  | yarn add antd  | 
choose the custom trigger as our layout. First thing is show code and copy code into NewsSandBox and then sperate them as below 
Layout.Siderwas pasted toSideMenuLayout.Headerwas pasted toTopHeader
The import thing is don’t forget to copy the CSS into our src/views/sandbox/NewsSandBox.css file.
5. Add click event on the collapsed/expanded icon in src/components/sandbox/TopHeader.js.
1  | const [collapsed, setCollapsed] = useState(false)  | 
React hooks is important. Here we use the
useState.
Then give the icon an onClick event property. for example
1  | <MenuFoldOutlined onClick={changeCollapsed} />  | 
6. Add Wellcome user in the TopHeader.
We copy the Dropdown Basic and Avator Basic components and modify them as we expected.
7. Modify the SideMenu
use axios for getting data.
1  | yarn add axios  | 
http.js with content
1  | import axios from "axios";  | 
In this page, will use hook useEffect for getting data back and use  hook useState to control returned data
1  | import React, { useEffect, useState } from 'react'  | 
8. Fix some issue for SideMenu
Finished above 7 steps, the page should be correctly rendering, but it is weird, some menu should be in second level route, but it is in SideMenu, another thing is each of the menu without icon. Let’s fix those issues we identified so far:
- For issue 1, we can identifiy the field pagepermission, check its 1 or not.
1
2
3const checkPagePermission = (item) => {
return item.pagepermission === 1
}pagepermission is very important!!
 - For issue 2, we can define an iconList to store icon, and use array[index] to get the icon.
1
2
3
4
5
6
7
8const iconList = {
'/home':<HomeOutlined />,
'/user-manage':<UserOutlined />,
'/user-manage/list':<UserOutlined />,
'/right-manage':<CrownOutlined />,
'/right-manage/role/list':<CrownOutlined />,
'/right-manage/right/list':<CrownOutlined />,
} - Issue 3, The Home is no children, we do not need the expansed icon in the right, so we have to add children.length to check
1
item.children?.length > 0 && checkPagePermission(item)
 - Issue 4, the style is egly, we import 
antd.cssintoApp.css1
2
3
4
5@import '~antd/dist/antd.css';
::-webkit-scrollbar {width: 5px;height: 5px;position:absolute}
::-webkit-scrollbar-thumb {background-color: #1890ff;}
::-webkit-scrollbar-track {background-color: #ddd;} 
-Issue 5, focus the previous select menu if user refresh page.
Menu component have SelectedKeys and defaultOpenKeys, we can set those two field to fix our issue.
In Ant library, most of time, if field called defaultXXXX, that meant it is uncontrolled field, otherwise it use useState for controlling.
1  | const location = useLocation();  |