Skip to content

Commit

Permalink
Merge pull request #56 from visiky/fix-pr-54
Browse files Browse the repository at this point in the history
fix: 修复首次进入,没有请求简历信息
  • Loading branch information
visiky authored Mar 14, 2022
2 parents c3d8f51 + f8bfb3f commit fa6d280
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
52 changes: 30 additions & 22 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { copyToClipboard } from '@/helpers/copy-to-board';
import { getDevice } from '@/helpers/detect-device';
import { exportDataToLocal } from '@/helpers/export-to-local';
import { getConfig, saveToLocalStorage } from '@/helpers/store-to-local';
import { fetchResume } from '@/helpers/fetch-resume';
import { Drawer } from './Drawer';
import { Resume } from './Resume';
import { ResumeConfig, ThemeConfig } from './types';
Expand Down Expand Up @@ -49,31 +50,38 @@ export const Page: React.FC = () => {
const user = (query.user || '') as string;
const branch = (query.branch || 'master') as string;
const mode = query.mode;

function store(data) {
originalConfig.current = data;
changeConfig(
_.omit(customAssign({}, data, _.get(data, ['locales', lang])), [
'locales',
])
);
updateLoading(false);
}

if (!mode) {
const link = `https://github.com/${user}/${user}/tree/${branch}`;
Modal.info({
title: i18n.get('获取简历信息失败'),
content: (
<div>
请检查用户名 {user} 是否正确或者简历信息是否在
<a href={link} target="_blank">{`${link}/resume.json`}</a>
</div>
),
okText: i18n.get('进入在线编辑'),
onOk: () => {
changeMode('edit');
},
});
fetchResume(lang, branch, user)
.then(data => store(data))
.catch(() => {
Modal.info({
title: i18n.get('获取简历信息失败'),
content: (
<div>
请检查用户名 {user} 是否正确或者简历信息是否在
<a href={link} target="_blank">{`${link}/resume.json`}</a>
</div>
),
okText: i18n.get('进入在线编辑'),
onOk: () => {
changeMode('edit');
},
});
});
} else {
getConfig(lang, branch, user).then(data => {
originalConfig.current = data;
changeConfig(
_.omit(customAssign({}, data, _.get(data, ['locales', lang])), [
'locales',
])
);
updateLoading(false);
});
getConfig(lang, branch, user).then(data => store(data));
}
}, [lang, query.user, query.branch]);

Expand Down
22 changes: 22 additions & 0 deletions src/helpers/fetch-resume.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fetch from 'cross-fetch';
import _ from "lodash";
import { ResumeConfig } from "@/components/types";
import { customAssign } from "./customAssign";

export function fetchResume(lang: string, branch: string, user: string): Promise<ResumeConfig> {

return fetch(
`https://raw.githubusercontent.com/${user}/${user}/${branch}/resume.json`
)
.then(data => {
if (data.status !== 200) {
return Promise.reject(new Error());
}
return data.json();
})
.then(data => {
return _.omit(customAssign({}, data, _.get(data, ['locales', lang])), [
'locales',
])
});
}
27 changes: 8 additions & 19 deletions src/helpers/store-to-local.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fetch from 'cross-fetch';
import { message } from 'antd';
import { ResumeConfig } from "@/components/types";
import { customAssign } from "@/helpers/customAssign";
import _ from "lodash";
import { getLocale } from '@/locale';
import { RESUME_INFO } from '@/datas/resume';
import { fetchResume } from './fetch-resume';

export const LOCAL_KEY = (user) => `${user ?? ''}resume-config`;

Expand All @@ -22,24 +22,13 @@ export async function getConfig(lang: string, branch: string, user: string): Pro
}
}

return fetch(
`https://raw.githubusercontent.com/${user}/${user}/${branch}/resume.json`
)
.then(data => {
if (data.status !== 200) {
message.warn(i18n.get('从模板中获取'), 1);
return _.omit(
customAssign({}, RESUME_INFO, _.get(RESUME_INFO, ['locales', lang])),
['locales']
);
}
return data.json();
})
.then(data => {
return _.omit(customAssign({}, data, _.get(data, ['locales', lang])), [
'locales',
])
});
return fetchResume(lang, branch, user).catch(() => {
message.warn(i18n.get('从模板中获取'), 1);
return _.omit(
customAssign({}, RESUME_INFO, _.get(RESUME_INFO, ['locales', lang])),
['locales']
);
})
}

export const saveToLocalStorage = _.throttle((user: string, config: ResumeConfig) => {
Expand Down

0 comments on commit fa6d280

Please sign in to comment.