Skip to content

Commit 0e61136

Browse files
authored
Merge pull request #10 from ryosms/refactor-scripts
Refactor scripts
2 parents 07dbb87 + 7f41220 commit 0e61136

File tree

5 files changed

+182
-115
lines changed

5 files changed

+182
-115
lines changed

src/components/DiaryDetail.vue

Lines changed: 33 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<md-card-content class="md-layout">
55
<md-field class="md-layout-item md-size-75 md-small-size-100">
66
<label>Title</label>
7-
<md-input v-model="title"></md-input>
7+
<md-input v-model="pixel.title"></md-input>
88
</md-field>
99
<md-field class="md-layout-item md-size-100">
1010
<label>Body</label>
11-
<md-textarea v-model="body"></md-textarea>
11+
<md-textarea v-model="pixel.body"></md-textarea>
1212
</md-field>
1313
</md-card-content>
1414
<md-card-actions>
15-
<md-button class="md-accent" v-if="quantity" @click="showConfirm=true">Delete</md-button>
15+
<md-button class="md-accent" v-if="pixel.quantity" @click="showConfirm=true">Delete</md-button>
1616
<md-button class="md-primary" :disabled="!canEdit" @click="saveDiary">Save</md-button>
1717
</md-card-actions>
1818
</md-card>
@@ -37,7 +37,7 @@
3737
<script>
3838
import Vue from 'vue';
3939
import {MdCard, MdField, MdButton, MdSnackbar, MdDialogConfirm} from 'vue-material/dist/components';
40-
import axios from 'axios';
40+
import {Pixela} from '@/pixela';
4141
4242
Vue.use(MdCard);
4343
Vue.use(MdField);
@@ -59,19 +59,22 @@
5959
return !this.hasError;
6060
},
6161
set(ignore) {
62-
// unnecessary setter
62+
// setter is unnecessary, but add to calm down linter
6363
},
6464
},
6565
},
6666
data: () => ({
67-
quantity: '',
68-
title: '',
69-
body: '',
67+
pixel: {quantity: '', title: '', body: ''},
7068
hasError: false,
7169
showErrorMessage: false,
7270
showCompleteMessage: false,
7371
showConfirm: false,
72+
pixela: null,
7473
}),
74+
created() {
75+
this.pixela = new Pixela(this.username, this.token);
76+
this.loadDiary();
77+
},
7578
methods: {
7679
isDateChanged(newDate, oldDate) {
7780
if (!oldDate) {
@@ -81,80 +84,34 @@
8184
&& newDate.getMonth() === oldDate.getMonth()
8285
&& newDate.getDate() === oldDate.getDate());
8386
},
84-
formatDiaryDate(diaryDate) {
85-
return diaryDate.getFullYear()
86-
+ ('0' + (diaryDate.getMonth() + 1)).slice(-2)
87-
+ ('0' + diaryDate.getDate()).slice(-2);
88-
},
89-
loadDiary() {
90-
const self = this;
91-
const url = 'https://pixe.la/v1/users/' + this.username
92-
+ '/graphs/' + this.graphId + '/' + this.formatDiaryDate(this.diaryDate);
93-
const headers = {
94-
'X-USER-TOKEN': this.token,
95-
'accept': 'application/json',
96-
};
97-
axios.get(url, {headers})
98-
.then((response) => {
99-
self.hasError = false;
100-
self.quantity = response.data.quantity;
101-
self.parseOptionalData(response.data.optionalData);
102-
})
103-
.catch((error) => {
104-
// FIXME: in case of 404, pixela don't return CORS headers.
105-
// self.hasError = (!error.response || error.response.status !== 404);
106-
self.quantity = '';
107-
self.title = '';
108-
self.body = '';
109-
});
110-
},
111-
parseOptionalData(optionalData) {
112-
try {
113-
const json = JSON.parse(optionalData);
114-
this.title = json.title;
115-
this.body = json.body;
116-
} catch (ignore) {
117-
this.title = '';
118-
this.body = '';
87+
async loadDiary() {
88+
this.pixel = {quantity: '', title: '', body: ''};
89+
const res = await this.pixela.loadPixel(this.graphId, this.diaryDate);
90+
// FIXME: in case of 404, pixela don't return CORS headers.
91+
// this.hasError = !res;
92+
if (!!res) {
93+
this.pixel = res;
11994
}
12095
},
121-
saveDiary() {
122-
const diary = this.quantity ? this.quantity : '1';
123-
const optionalData = {title: this.title, body: this.body};
124-
const url = 'https://pixe.la/v1/users/' + this.username
125-
+ '/graphs/' + this.graphId + '/' + this.formatDiaryDate(this.diaryDate);
126-
const headers = {
127-
'X-USER-TOKEN': this.token,
128-
};
129-
axios.put(url, {quantity: diary, optionalData: JSON.stringify(optionalData)}, {headers})
130-
.then((ignore) => {
131-
this.quantity = diary;
132-
this.showCompleteMessage = true;
133-
})
134-
.catch((ignore) => {
135-
this.showErrorMessage = true;
136-
});
96+
async saveDiary() {
97+
const res = await this.pixela.postPixel(this.graphId, this.diaryDate, this.pixel);
98+
if (!res) {
99+
this.showErrorMessage = true;
100+
} else {
101+
this.pixel = res;
102+
this.showCompleteMessage = true;
103+
}
137104
},
138-
deleteDiary() {
139-
const url = 'https://pixe.la/v1/users/' + this.username
140-
+ '/graphs/' + this.graphId + '/' + this.formatDiaryDate(this.diaryDate);
141-
const headers = {
142-
'X-USER-TOKEN': this.token,
143-
};
144-
axios.delete(url, {headers}).then((ignore) => {
145-
this.quantity = '';
146-
this.title = '';
147-
this.body = '';
105+
async deleteDiary() {
106+
const res = await this.pixela.deletePixel(this.graphId, this.diaryDate);
107+
if (res) {
108+
this.pixel = {quantity: '', title: '', body: ''};
148109
this.showCompleteMessage = true;
149-
})
150-
.catch((ignore) => {
151-
this.showErrorMessage = true;
152-
});
110+
} else {
111+
this.showErrorMessage = true;
112+
}
153113
},
154114
},
155-
created() {
156-
this.loadDiary();
157-
},
158115
watch: {
159116
diaryDate(newDate, oldDate) {
160117
if (!newDate) {

src/components/PixelaGraph.vue

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<md-dialog :md-active.sync="showDialog">
66
<md-dialog-title>Select date</md-dialog-title>
7-
<md-dialog-content class="md-layout">
7+
<md-dialog-content v-if="!!svg" class="md-layout">
88
<div v-html="svg" class="md-layout-item md-size-100" style="width: 300px; max-width: 300px;"></div>
99
<MdButton class="md-layout-item md-primary" @click="changeGraphDate(-30)">
1010
<md-icon class="md-size-2x">replay_30</md-icon>
@@ -24,15 +24,15 @@
2424
<script>
2525
import Vue from 'vue';
2626
import {MdButton, MdDialog} from 'vue-material/dist/components';
27-
import axios from 'axios';
2827
import tippy from 'tippy.js';
2928
import 'tippy.js/dist/tippy.css';
29+
import {Pixela} from '@/pixela';
3030
3131
Vue.use(MdButton);
3232
Vue.use(MdDialog);
3333
3434
export default {
35-
name: "PixelaGraph",
35+
name: 'PixelaGraph',
3636
props: {
3737
diaryDate: Date,
3838
username: String,
@@ -41,14 +41,18 @@
4141
computed: {
4242
canSelect() {
4343
return !this.selectedDate;
44-
}
44+
},
4545
},
4646
data: () => ({
4747
selectedDate: null,
4848
graphDate: new Date(),
4949
svg: '',
5050
showDialog: false,
51+
pixela: null,
5152
}),
53+
created() {
54+
this.pixela = new Pixela(this.username, null);
55+
},
5256
methods: {
5357
show() {
5458
this.selectedDate = null;
@@ -57,29 +61,19 @@
5761
this.loadSvg();
5862
},
5963
loadSvg() {
60-
const targetDate = this.graphDate.getFullYear()
61-
+ ('0' + (this.graphDate.getMonth() + 1)).slice(-2)
62-
+ ('0' + this.graphDate.getDate()).slice(-2);
63-
const url = `https://pixe.la/v1/users/${this.username}/graphs/${this.graphId}`;
64-
const params = {
65-
'date': targetDate,
66-
'mode': 'short',
67-
};
68-
axios.get(url, {params}).then((response) => {
69-
this.svg = response.data;
70-
}).then((ignore) => {
71-
this.setPixelEvent();
64+
this.pixela.loadGraphSvg(this.graphId, this.graphDate).then((svg) => {
65+
this.svg = svg;
66+
}).then(() => {
67+
if (!!this.svg) {
68+
this.setPixelEvent();
69+
}
7270
});
7371
},
7472
setPixelEvent() {
75-
const self = this;
7673
const pixels = document.querySelectorAll('.each-day');
7774
tippy(pixels);
78-
for (let i = 0; i < pixels.length; i++) {
79-
const pixel = pixels[i];
80-
pixel.addEventListener('click', function() {
81-
self.pixelClicked(this);
82-
});
75+
for (const pixel of pixels) {
76+
pixel.addEventListener('click', (event) => this.pixelClicked(event.target));
8377
}
8478
},
8579
pixelClicked(pixel) {
@@ -89,8 +83,8 @@
8983
},
9084
clearActiveClass() {
9185
const pixels = document.querySelectorAll('.each-day.active');
92-
for (let i = 0; i < pixels.length; i++) {
93-
pixels[i].classList.remove('active');
86+
for (const pixel of pixels) {
87+
pixel.classList.remove('active');
9488
}
9589
},
9690
changeGraphDate(days) {

src/components/UserToken.vue

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<script>
2828
import Vue from 'vue';
2929
import {MdField, MdButton, MdSnackbar, MdProgress, MdCard} from 'vue-material/dist/components';
30-
import axios from 'axios';
30+
import {Pixela} from '@/pixela';
3131
3232
Vue.use(MdField);
3333
Vue.use(MdButton);
@@ -40,6 +40,7 @@
4040
data: () => ({
4141
hasError: false,
4242
isLoading: false,
43+
pixela: new Pixela(),
4344
}),
4445
computed: {
4546
username: {
@@ -62,21 +63,15 @@
6263
methods: {
6364
async authentication() {
6465
this.isLoading = true;
65-
try {
66-
const res = await axios.get('https://pixe.la/v1/users/' + this.$store.state.username + '/graphs',
67-
{headers: {'X-USER-TOKEN': this.$store.state.token}});
68-
this.$store.commit('setGraphs', res.data.graphs);
69-
this.hasError = false;
66+
const pixela = new Pixela(this.username, this.token);
67+
const graphs = await pixela.loadGraphs();
68+
this.hasError = !graphs;
69+
this.isLoading = false;
70+
if (!this.hasError) {
71+
this.$store.commit('setGraphs', graphs);
7072
this.$router.push('/graphs');
71-
} catch (e) {
72-
this.hasError = true;
73-
} finally {
74-
this.isLoading = false;
7573
}
7674
},
7775
},
7876
};
7977
</script>
80-
81-
<style scoped>
82-
</style>

0 commit comments

Comments
 (0)