From d3f63de6e795dba350d8ad69a030f06c3cf42edc Mon Sep 17 00:00:00 2001 From: JUNO <32605822+junhoyeo@users.noreply.github.com> Date: Wed, 10 Jul 2019 22:40:01 +0900 Subject: [PATCH] feat: cache dimibob in localStorage (#31) --- src/main.js | 1 + src/pages/Index.vue | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main.js b/src/main.js index 526fdc9..3a2725a 100644 --- a/src/main.js +++ b/src/main.js @@ -8,6 +8,7 @@ import './assets/style.css'; Vue.prototype.$api = axios; Vue.prototype.moment = moment; +/* eslint-disable no-new */ new Vue({ el: '#app', render: h => h(App), diff --git a/src/pages/Index.vue b/src/pages/Index.vue index 0bc097a..5d7022f 100644 --- a/src/pages/Index.vue +++ b/src/pages/Index.vue @@ -53,7 +53,15 @@ export default { }, async created() { - await this.getDimibob(); + try { + const dimibob = await JSON.parse(localStorage.dimibob); + if (dimibob.timestamp !== this.moment().format('YYYYMMDD')) { + throw Error(); + } + this.dimibob = dimibob; + } catch (err) { + await this.getDimibob(); + } this.getTimetable(); }, @@ -61,10 +69,16 @@ export default { async getDimibob() { const today = this.moment().format('YYYYMMDD'); const { data: { breakfast, lunch, dinner } } = await this.$api.get(`https://dev-api.dimigo.in/dimibobs/${today}`); - console.log(breakfast); this.dimibob.breakfast = breakfast || '아침 급식 정보가 없습니다.'; this.dimibob.lunch = lunch || '점심 급식 정보가 없습니다.'; this.dimibob.dinner = dinner || '저녁 급식 정보가 없습니다.'; + + localStorage.dimibob = JSON.stringify({ + breakfast, + lunch, + dinner, + timestamp: this.moment().format('YYYYMMDD'), + }); }, getTimetable() {