Skip to content

Commit

Permalink
use fetchwrapper helper
Browse files Browse the repository at this point in the history
  • Loading branch information
devincowan committed Nov 27, 2023
1 parent 786fc8e commit ff1b897
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function request(method) {
requestOptions.headers['Content-Type'] = 'application/json';
requestOptions.body = JSON.stringify(body);
}
console.log("url", url, "optoins", requestOptions)
console.log("url", url, "options", requestOptions)
return fetch(url, requestOptions).then(handleResponse);
}
}
Expand All @@ -26,13 +26,14 @@ function request(method) {

function authHeader(url) {
// return auth header with jwt if user is logged in and request is to the api url
const { token } = useAuthStore();
const isLoggedIn = !!token?.token;
const authStore = useAuthStore();
const jwt = authStore.getToken()
const isLoggedIn = !!jwt;
const isApiUrl = url.startsWith(import.meta.env.VITE_APP_API_URL);
alert(`Isapi:${isApiUrl} isloggedin:${isLoggedIn}`)
if (isLoggedIn && isApiUrl) {
return { Authorization: `Bearer ${token.token}` };
return { Authorization: `Bearer ${jwt}` };
} else {
console.log(`Isapi:${isApiUrl} isloggedin:${isLoggedIn}`)
return {};
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/frontend/src/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ENDPOINTS, APP_URL } from '@/constants'
import { useAuthStore } from '@/stores/auth'
// import { fetchWrapper } from '@/_helpers/fetch-wrapper';
// import { fetchWrapper } from '@/_helpers/fetchWrapper';
// function openLogInDialog(redirectTo) {
// this.logInDialog$.next(redirectTo);
// }
Expand Down
14 changes: 3 additions & 11 deletions app/frontend/src/components/SubmitButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

<script setup>
import { useModelsStore } from '@/stores/models'
import { useAuthStore } from '@/stores/auth'
import { ENDPOINTS } from '@/constants'
import { useMapStore } from '@/stores/map'
import { fetchWrapper } from '@/_helpers/fetchWrapper';
const mapStore = useMapStore()
const Map = mapStore.mapObject
const modelsStore = useModelsStore();
const authStore = useAuthStore();
function submit() {
const model = modelsStore.selectedModel
Expand All @@ -23,16 +22,9 @@ async function submitHucs(selected_hucs, model) {
selected_hucs = selected_hucs.map(a => a.hucid);
const hucs = selected_hucs.join(",")
const jwt = authStore.getToken()
alert(`Submitting hucs: ${hucs} for ${model} subsetting`)
const parResp = await fetch(`${ENDPOINTS.submit}/${model}?hucs=${hucs}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${jwt}`,
}
})
const parJson = await parResp.json()
const parJson = await fetchWrapper.post(`${ENDPOINTS.submit}/${model}?hucs=${hucs}`)
alert(`Submitted ${parJson.workflow_name} workflow. Workflow_id: ${parJson.workflow_id}`)
}
</script>
12 changes: 2 additions & 10 deletions app/frontend/src/stores/submissions.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { defineStore } from 'pinia'
import { ENDPOINTS } from '../constants'
import { ref } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { fetchWrapper } from '@/_helpers/fetchWrapper';

const authStore = useAuthStore();
const jwt = authStore.getToken()

export const useSubmissionsStore = defineStore('submissions', () => {
const submissions = ref([])

async function getSubmissions() {
const submissionResp = await fetch(`${ENDPOINTS.submissions}`, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${jwt}`,
}
})
const submissionsObj = await submissionResp.json()
const submissionsObj = await fetchWrapper.get(`${ENDPOINTS.submissions}`)
let submissions = submissionsObj.submissions
this.submissions = submissions
}
Expand Down

0 comments on commit ff1b897

Please sign in to comment.