代码优化

This commit is contained in:
黄心宇 2024-01-13 15:11:58 +08:00
parent 6465a77744
commit f2e666009b
7 changed files with 46 additions and 30 deletions

View File

@ -65,7 +65,7 @@ export function initAxiosInterceptors(props) {
});
axios.interceptors.response.use(function (response) {
if (response === undefined) {
if (response === undefined || !response.data) {
return
}
const config = response.config;

View File

@ -10,6 +10,9 @@ import cookie from 'react-cookies';
import { setSeoMeta } from 'educoder';
import { Base64 } from 'js-base64';
import { setProject, setProjectDetail, setProjectEntries, setProjectMenuList, setProjectReadMe } from '../../redux/actions/server';
import actions from '../../redux/actions';
import types from '../../redux/actions/actionTypes'
import { getProjectFunc, getProjectDetailFunc } from '../../services/project'
import '../css/index.scss'
import './list.scss';
@ -168,21 +171,6 @@ function checkPathname(projectsId, owner, pathname) {
return name;
}
const getProjectFunc = (owner, projectsId, isServer = false) => {
const url = isServer ? `https://www.gitlink.org.cn/api/${owner}/${projectsId}/simple.json` : `/${owner}/${projectsId}/simple.json`;
return axios.get(url)
}
const getDetailFunc = (owner, projectsId, isServer = false) => {
const url = isServer ? `https://www.gitlink.org.cn/api/${owner}/${projectsId}/detail.json` : `/${owner}/${projectsId}/detail.json`;
return axios.get(url)
}
const getBannerFunc = (owner, projectsId, isServer = false) => {
const url = isServer ? `https://www.gitlink.org.cn/api/${owner}/${projectsId}/menu_list.json` : `/${owner}/${projectsId}/menu_list.json`;
return axios.get(url)
}
class Detail extends Component {
constructor(props) {
super(props);
@ -220,6 +208,12 @@ class Detail extends Component {
// this.getProject();
let history = this.props.location;
this.clearIssueCookies(history);
if (this.props.clearProject) {
const typeList = [ types.GET_PROJECT_BASE, types.GET_PROJECT_DETAIL, types.GET_PROJECT_ENTRIES, types.GET_PROJECT_MENU, types.GET_PROJECT_README ]
typeList.forEach(e => {
this.props.clearProject(e)
})
}
}
componentDidUpdate = async (prevState) => {
@ -307,7 +301,7 @@ class Detail extends Component {
if (this.props.defaultDetail) {
projectdata = this.props.defaultDetail
} else {
projectdata = await getDetailFunc(owner, projectsId, !window.location.host).data;
projectdata = await getProjectDetailFunc(owner, projectsId, !window.location.host).data;
}
if (projectdata) {
this.getDetail(projectdata)
@ -939,6 +933,10 @@ const mapStateToProps = (state) => {
};
}
const mapDispatchToProps = (dispatch) => ({
clearProject: (type) => dispatch(actions.clearProject(type)),
});
Detail.preFetch = ({ store, match, query }) => {
return new Promise(async function(resolve, reject) {
let { dispatch, getState } = store;
@ -950,7 +948,6 @@ Detail.preFetch = ({ store, match, query }) => {
dispatch(setProjectEntries({...match.params, branch: 'master'})),
dispatch(setProjectMenuList(match.params)),
dispatch(setProjectReadMe({...match.params, branch: 'master'}))
];
const data = await Promise.all(promises);
resolve({
@ -960,5 +957,6 @@ Detail.preFetch = ({ store, match, query }) => {
};
export default withRouter(connect(
mapStateToProps
mapStateToProps,
mapDispatchToProps
)(Detail));

View File

@ -28,16 +28,16 @@ class IndexItem extends Component {
<img className="p-r-photo" alt="" src={item.author && item.author.image_url} ></img>
</a>
:
<a href="javascript:void(0)" onClick={() => { window.location.href = `/${i.author && i.author.login}/${i.identifier}` }} className="show-user-link">
<a href="javascript:void(0)" onClick={() => { window.location.href = `/${item.author && item.author.login}/${item.identifier}` }} className="show-user-link">
<img className="p-r-photo" alt="" src={getImageUrl(`/${item.author && item.author.image_url}`)} ></img>
</a>
}
<div className="p-r-Infos">
<div className="p-r-name">
<AlignCenter>
<Link to={`/${item.author.login}/${item.identifier}`} title={`${item.author.name}/${item.name}`} className="color-grey-3 font-18 task-hide " style={{maxWidth: 470 }}>
<a href="javascript:void(0)" onClick={() => { window.location.href = `/${item.author && item.author.login}/${item.identifier}` }} title={`${item.author.name}/${item.name}`} className="color-grey-3 font-18 task-hide " style={{maxWidth: 470 }}>
{item.author.name}/{item.name}
</Link>
</a>
{ !item.is_public && <span className="privateTag">私有</span> }
{
item.forked_from_project_id ?

View File

@ -87,7 +87,7 @@ function ConcentrateProject({
list.map((i,k)=>{
return(
<li key={i.id}>
<Link to={`/${i.author && i.author.login}/${i.identifier}`} className="name task-hide">{i.name}</Link>
<a href="javascript:void(0)" onClick={() => { window.location.href = `/${i.author && i.author.login}/${i.identifier}`}}className="name task-hide">{i.name}</a>
<p className="task-hide desc">{i.description}</p>
{/* 项目标签 */}
{i.topics && <div className='viewProListTopics mt10'>

View File

@ -294,7 +294,6 @@ export function TPMIndexHOC(WrappedComponent) {
};
let path =this.props.location.pathname;
let pathCheck = path.indexOf("/zone")>-1 && path.indexOf("/newdetail")>-1 && IsPC();
console.log('+++++++++++++++++++++++++++++++fdsafgsda++++++++++++++')
return (
<div className="indexHOC">
<SystemNotice

View File

@ -129,6 +129,11 @@ import {
import {
setProject,
clearProject,
setProjectDetail,
setProjectEntries,
setProjectMenuList,
setProjectReadMe
} from './server.js';
export default {
@ -230,5 +235,10 @@ export default {
changeWXCodeEvaluateLoading,
changeWXCodeEvaluateDialog,
// 服务端渲染
setProject
setProject,
clearProject,
setProjectDetail,
setProjectEntries,
setProjectMenuList,
setProjectReadMe
}

View File

@ -15,6 +15,13 @@ export const setProject = ({ owner,projectsId }) => {
}
}
export const clearProject = (type) => {
return {
type: type,
payload: ''
}
}
// 获取项目详情信息
export const setProjectDetail = ({ owner,projectsId }) => {
return (dispatch) => {
@ -58,11 +65,13 @@ export const setProjectMenuList = ({ owner,projectsId }) => {
export const setProjectReadMe = ({ owner,projectsId, branch }) => {
return (dispatch) => {
return getProjectReadMe(owner, projectsId, branch).then(res => {
dispatch({
type: types.GET_PROJECT_README,
payload: res.data
});
return res.data
if (res && res.data) {
dispatch({
type: types.GET_PROJECT_README,
payload: res.data
});
return res.data
}
})
}
}