diff --git a/pkg/extension/src/scripts/api.js b/pkg/extension/src/scripts/api.js
index 06a80f926..c4a35a90a 100644
--- a/pkg/extension/src/scripts/api.js
+++ b/pkg/extension/src/scripts/api.js
@@ -1,4 +1,25 @@
-function updateLabelsCache(apiUrl, tab) {
+function gqlRequest(apiUrl, query) {
+ return fetch(apiUrl, {
+ method: 'POST',
+ redirect: 'follow',
+ credentials: 'include',
+ mode: 'cors',
+ headers: {
+ Accept: 'application/json',
+ 'Content-Type': 'application/json',
+ },
+ body: query,
+ })
+ .then((response) => response.json())
+ .then((json) => {
+ if (!json['data']) {
+ throw new Error('No response data')
+ }
+ return json['data']
+ })
+}
+
+async function updateLabelsCache(apiUrl, tab) {
const query = JSON.stringify({
query: `query GetLabels {
labels {
@@ -21,33 +42,22 @@ function updateLabelsCache(apiUrl, tab) {
}
`,
})
- return fetch(apiUrl, {
- method: 'POST',
- redirect: 'follow',
- credentials: 'include',
- mode: 'cors',
- headers: {
- Accept: 'application/json',
- 'Content-Type': 'application/json',
- },
- body: query,
+
+ const data = await gqlRequest(apiUrl, query)
+ if (!data.labels || data.labels['errorCodes'] || !data.labels['labels']) {
+ console.log('GQL Error updating label cache response:', data, data)
+ console.log(!data.labels, data.labels['errorCodes'], !data.labels['labels'])
+ return []
+ }
+ await setStorage({
+ labels: data.labels.labels,
+ labelsLastUpdated: new Date().toISOString(),
})
- .then((response) => response.json())
- .then((data) => {
- const result = data.data.labels.labels
- return result
- })
- .then((labels) => {
- setStorage({
- labels: labels,
- labelsLastUpdated: new Date().toISOString(),
- })
- return labels
- })
+
+ return data.labels.labels
}
-function updatePageTitle(apiUrl, pageId, title) {
- console.log('updated title: ', apiUrl, pageId, title)
+async function updatePageTitle(apiUrl, pageId, title) {
const mutation = JSON.stringify({
query: `mutation UpdatePage($input: UpdatePageInput!) {
updatePage(input: $input) {
@@ -70,25 +80,19 @@ function updatePageTitle(apiUrl, pageId, title) {
},
})
- return fetch(apiUrl, {
- method: 'POST',
- redirect: 'follow',
- credentials: 'include',
- mode: 'cors',
- headers: {
- Accept: 'application/json',
- 'Content-Type': 'application/json',
- },
- body: mutation,
- })
- .then((response) => response.json())
- .then((data) => {
- console.log('updated title: ', data)
- })
+ const data = await gqlRequest(apiUrl, mutation)
+ if (
+ !data.updatePage ||
+ data.updatePage['errorCodes'] ||
+ !data.updatePage['updatedPage']
+ ) {
+ console.log('GQL Error updating page:', data)
+ throw new Error('Error updating title.')
+ }
+ return data.updatePage.updatePage
}
-function setLabels(apiUrl, pageId, labelIds) {
- console.log('setting labels: ', apiUrl, pageId, labelIds)
+async function setLabels(apiUrl, pageId, labelIds) {
const mutation = JSON.stringify({
query: `mutation SetLabels($input: SetLabelsInput!) {
setLabels(input: $input) {
@@ -111,19 +115,14 @@ function setLabels(apiUrl, pageId, labelIds) {
},
})
- return fetch(apiUrl, {
- method: 'POST',
- redirect: 'follow',
- credentials: 'include',
- mode: 'cors',
- headers: {
- Accept: 'application/json',
- 'Content-Type': 'application/json',
- },
- body: mutation,
- })
- .then((response) => response.json())
- .then((data) => {
- console.log('updated labels: ', data)
- })
+ const data = await gqlRequest(apiUrl, mutation)
+ if (
+ !data.setLabels ||
+ data.setLabels['errorCodes'] ||
+ !data.setLabels['labels']
+ ) {
+ console.log('GQL Error setting labels:', data)
+ throw new Error('Error setting labels.')
+ }
+ return data.setLabels.labels
}
diff --git a/pkg/extension/src/scripts/background.js b/pkg/extension/src/scripts/background.js
index a0304a832..bec45fcef 100644
--- a/pkg/extension/src/scripts/background.js
+++ b/pkg/extension/src/scripts/background.js
@@ -670,64 +670,6 @@ function reflectIconState(tab) {
updateActionIcon(tabId, active)
}
-// function updateLabelsCache(tab) {
-// const query = JSON.stringify({
-// query: `query GetLabels {
-// labels {
-// ... on LabelsSuccess {
-// labels {
-// ...LabelFields
-// }
-// }
-// ... on LabelsError {
-// errorCodes
-// }
-// }
-// }
-// fragment LabelFields on Label {
-// id
-// name
-// color
-// description
-// createdAt
-// }
-// `,
-// })
-// return fetch(omnivoreGraphqlURL + 'graphql', {
-// method: 'POST',
-// redirect: 'follow',
-// credentials: 'include',
-// mode: 'cors',
-// headers: {
-// Accept: 'application/json',
-// 'Content-Type': 'application/json',
-// },
-// body: query,
-// })
-// .then((response) => response.json())
-// .then((data) => {
-// const result = data.data.labels.labels
-// return result
-// })
-// .then((labels) => {
-// setStorage({
-// labels: labels,
-// labelsLastUpdated: new Date().toISOString(),
-// })
-// return labels
-// })
-// .then((labels) => {
-// browserApi.tabs.sendMessage(tab.id, {
-// action: ACTIONS.LabelCacheUpdated,
-// payload: {},
-// })
-// })
-// .catch((err) => {
-// console.error('error fetching labels', err, omnivoreGraphqlURL)
-// return undefined
-// })
-// }
-
function init() {
/* Extension icon switcher on page/tab load status */
browserApi.tabs.onActivated.addListener(({ tabId }) => {
@@ -761,6 +703,16 @@ function init() {
// forward messages from grab-iframe-content.js script to tabs
browserApi.runtime.onMessage.addListener((request, sender, sendResponse) => {
+ function updateClientStatus(target, status, message) {
+ browserApi.tabs.sendMessage(sender.tab.id, {
+ action: ACTIONS.UpdateStatus,
+ payload: {
+ target,
+ status,
+ message,
+ },
+ })
+ }
if (request.forwardToTab) {
delete request.forwardToTab
browserApi.tabs.sendRequest(sender.tab.id, request)
@@ -776,15 +728,14 @@ function init() {
omnivoreGraphqlURL + 'graphql',
request.payload.pageId,
request.payload.title
- ).then(() => {
- browserApi.tabs.sendMessage(sender.tab.id, {
- action: ACTIONS.UpdateStatus,
- payload: {
- target: 'title',
- status: 'success',
- },
+ )
+ .then(() => {
+ updateClientStatus('title', 'success', 'Title updated.')
+ })
+ .catch((err) => {
+ console.log('caught error updating title: ', err)
+ updateClientStatus('title', 'failure', 'Error updating title.')
})
- })
}
if (request.action === ACTIONS.SetLabels) {
@@ -792,15 +743,13 @@ function init() {
omnivoreGraphqlURL + 'graphql',
request.payload.pageId,
request.payload.labelIds
- ).then(() => {
- browserApi.tabs.sendMessage(sender.tab.id, {
- action: ACTIONS.UpdateStatus,
- payload: {
- target: 'labels',
- status: 'success',
- },
+ )
+ .then(() => {
+ updateClientStatus('labels', 'success', 'Labels updated.')
+ })
+ .catch(() => {
+ updateClientStatus('labels', 'failure', 'Error updating labels.')
})
- })
}
})
diff --git a/pkg/extension/src/scripts/content/toast.js b/pkg/extension/src/scripts/content/toast.js
index 9920402dc..ebe518b9e 100644
--- a/pkg/extension/src/scripts/content/toast.js
+++ b/pkg/extension/src/scripts/content/toast.js
@@ -24,8 +24,11 @@