Sync method

This commit is contained in:
Анатолий Копыл
2022-05-09 03:02:19 +03:00
parent b741964c3a
commit 7458750053
10 changed files with 126 additions and 21 deletions

View File

@@ -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
View 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();

View File

@@ -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],

View File

@@ -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>