|
4 | 4 | <md-card-content class="md-layout"> |
5 | 5 | <md-field class="md-layout-item md-size-75 md-small-size-100"> |
6 | 6 | <label>Title</label> |
7 | | - <md-input v-model="title"></md-input> |
| 7 | + <md-input v-model="pixel.title"></md-input> |
8 | 8 | </md-field> |
9 | 9 | <md-field class="md-layout-item md-size-100"> |
10 | 10 | <label>Body</label> |
11 | | - <md-textarea v-model="body"></md-textarea> |
| 11 | + <md-textarea v-model="pixel.body"></md-textarea> |
12 | 12 | </md-field> |
13 | 13 | </md-card-content> |
14 | 14 | <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> |
16 | 16 | <md-button class="md-primary" :disabled="!canEdit" @click="saveDiary">Save</md-button> |
17 | 17 | </md-card-actions> |
18 | 18 | </md-card> |
|
37 | 37 | <script> |
38 | 38 | import Vue from 'vue'; |
39 | 39 | import {MdCard, MdField, MdButton, MdSnackbar, MdDialogConfirm} from 'vue-material/dist/components'; |
40 | | - import axios from 'axios'; |
| 40 | + import {Pixela} from '@/pixela'; |
41 | 41 |
|
42 | 42 | Vue.use(MdCard); |
43 | 43 | Vue.use(MdField); |
|
59 | 59 | return !this.hasError; |
60 | 60 | }, |
61 | 61 | set(ignore) { |
62 | | - // unnecessary setter |
| 62 | + // setter is unnecessary, but add to calm down linter |
63 | 63 | }, |
64 | 64 | }, |
65 | 65 | }, |
66 | 66 | data: () => ({ |
67 | | - quantity: '', |
68 | | - title: '', |
69 | | - body: '', |
| 67 | + pixel: {quantity: '', title: '', body: ''}, |
70 | 68 | hasError: false, |
71 | 69 | showErrorMessage: false, |
72 | 70 | showCompleteMessage: false, |
73 | 71 | showConfirm: false, |
| 72 | + pixela: null, |
74 | 73 | }), |
| 74 | + created() { |
| 75 | + this.pixela = new Pixela(this.username, this.token); |
| 76 | + this.loadDiary(); |
| 77 | + }, |
75 | 78 | methods: { |
76 | 79 | isDateChanged(newDate, oldDate) { |
77 | 80 | if (!oldDate) { |
|
81 | 84 | && newDate.getMonth() === oldDate.getMonth() |
82 | 85 | && newDate.getDate() === oldDate.getDate()); |
83 | 86 | }, |
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; |
119 | 94 | } |
120 | 95 | }, |
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 | + } |
137 | 104 | }, |
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: ''}; |
148 | 109 | this.showCompleteMessage = true; |
149 | | - }) |
150 | | - .catch((ignore) => { |
151 | | - this.showErrorMessage = true; |
152 | | - }); |
| 110 | + } else { |
| 111 | + this.showErrorMessage = true; |
| 112 | + } |
153 | 113 | }, |
154 | 114 | }, |
155 | | - created() { |
156 | | - this.loadDiary(); |
157 | | - }, |
158 | 115 | watch: { |
159 | 116 | diaryDate(newDate, oldDate) { |
160 | 117 | if (!newDate) { |
|
0 commit comments