Add archive API call

This commit is contained in:
Logan Williams
2023-05-19 16:37:34 +02:00
parent 70e6db30e1
commit 14fa178d49
2 changed files with 42 additions and 3 deletions

View File

@@ -23,7 +23,7 @@
</v-btn>
</v-col>
<v-col>
<v-btn>Archive now</v-btn>
<v-btn @click="$store.dispatch('archive', doc)">Archive now</v-btn>
</v-col>
<v-dialog width="500" v-model="dialog" persistent :retain-focus="false">
<template v-slot:activator="{ on, attrs }">

View File

@@ -17,6 +17,7 @@ import {
getDocs,
doc,
deleteDoc,
updateDoc,
} from "firebase/firestore";
import { firebaseAuth, firebaseFirestore } from "@/firebase.js";
@@ -25,6 +26,7 @@ Vue.use(Vuex);
export default new Vuex.Store({
state: {
user: null,
access_token: null,
docs: [],
loading: false,
},
@@ -38,11 +40,15 @@ export default new Vuex.Store({
setLoading(state, loading) {
state.loading = loading;
},
setAccessToken(state, access_token) {
state.access_token = access_token;
},
},
actions: {
async signin({ commit, dispatch }) {
async function callback(tokenResponse) {
let access_token = tokenResponse.access_token;
commit("setAccessToken", access_token);
const credential = GoogleAuthProvider.credential(null, access_token);
const response = await signInWithCredential(firebaseAuth, credential);
@@ -57,7 +63,8 @@ export default new Vuex.Store({
const client = google.accounts.oauth2.initTokenClient({
client_id:
"406209235111-r1mpkvkfaqc2jg5iqbvffl2b0rf4clbo.apps.googleusercontent.com",
scope: "https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.profile",
scope:
"https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.profile",
callback,
});
@@ -110,6 +117,38 @@ export default new Vuex.Store({
}
},
async archive({ state, dispatch }, sheet) {
try {
// send a post request to the API with the sheet ID in the body
// and a bearer auth token in the header
await fetch(
"https://auto-archiver-api.bellingcat.com/sheet",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${state.access_token}`,
},
body: JSON.stringify({
sheet_id: sheet.sheetId,
}),
}
);
// update firestore with the archive status
const docRef = doc(firebaseFirestore, "sheets", sheet.id);
await updateDoc(docRef, {
lastArchived: Date.now(),
});
// update the store
dispatch("getDocs");
} catch (error) {
console.error("archive (firebase.js): ", error);
}
},
async add({ state, dispatch, commit }, { name }) {
commit("setLoading", true);
@@ -259,7 +298,7 @@ export default new Vuex.Store({
role: "writer",
type: "user",
emailAddress:
"test-auto-archiver@bc-auto-archiver.iam.gserviceaccount.com",
"m-auto-archiver@bc-auto-archiver.iam.gserviceaccount.com",
},
});