Sync method
This commit is contained in:
@@ -4,6 +4,16 @@
|
||||
<div id="modalSpot" />
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import api from '@/api';
|
||||
|
||||
export default {
|
||||
async mounted() {
|
||||
await api.sync();
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
@import "@/scss/style.scss";
|
||||
|
||||
|
||||
36
frontend/src/api/index.js
Normal file
36
frontend/src/api/index.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import store from '@/store';
|
||||
|
||||
class Api {
|
||||
baseUrl = 'http://localhost:3000/api';
|
||||
|
||||
async login(token, authProvider) {
|
||||
const response = await fetch(`${this.baseUrl}/login`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({
|
||||
token,
|
||||
authProvider,
|
||||
}),
|
||||
});
|
||||
console.log(await response.json());
|
||||
}
|
||||
|
||||
async sync() {
|
||||
const { tasks, categories, updatedAt } = store.state;
|
||||
|
||||
const response = await fetch(`${this.baseUrl}/sync`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({
|
||||
user: {
|
||||
tasks,
|
||||
categories,
|
||||
updatedAt,
|
||||
},
|
||||
}),
|
||||
});
|
||||
console.log(await response.json());
|
||||
}
|
||||
}
|
||||
|
||||
export default new Api();
|
||||
@@ -11,7 +11,7 @@ export default createStore({
|
||||
tasks: [],
|
||||
midnightReset: false,
|
||||
lastReset: new Date(),
|
||||
token: null,
|
||||
updatedAt: new Date(),
|
||||
darkTheme: true,
|
||||
},
|
||||
mutations: {
|
||||
@@ -96,8 +96,8 @@ export default createStore({
|
||||
state.midnightReset = !!value;
|
||||
},
|
||||
|
||||
setToken(state, token) {
|
||||
state.token = token;
|
||||
updated(state) {
|
||||
state.updatedAt = new Date();
|
||||
},
|
||||
},
|
||||
plugins: [vuexLocal.plugin],
|
||||
|
||||
@@ -5,21 +5,14 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import api from '@/api';
|
||||
|
||||
export default {
|
||||
async mounted() {
|
||||
const baseUrl = 'http://localhost:3000/api/login';
|
||||
const token = /access_token=([^&]+)/.exec(this.$route.hash)[1];
|
||||
const authProvider = /state=([^&]+)/.exec(this.$route.hash)[1];
|
||||
|
||||
const response = await fetch(baseUrl, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({
|
||||
token,
|
||||
authProvider,
|
||||
}),
|
||||
});
|
||||
console.log(await response.json());
|
||||
api.login(token, authProvider);
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user