Clean up API callers
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
@ -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.')
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@ -24,8 +24,11 @@
|
||||
<path fill-rule="evenodd" clip-rule="evenodd" d="M9.91626 18.6047C14.8868 18.6047 18.9163 14.5752 18.9163 9.60468C18.9163 4.63411 14.8868 0.604675 9.91626 0.604675C4.9457 0.604675 0.91626 4.63411 0.91626 9.60468C0.91626 14.5752 4.9457 18.6047 9.91626 18.6047ZM9.91626 17.1046C14.0584 17.1046 17.4163 13.7468 17.4163 9.60463C17.4163 5.4625 14.0584 2.10463 9.91626 2.10463C5.77412 2.10463 2.41626 5.4625 2.41626 9.60463C2.41626 13.7468 5.77412 17.1046 9.91626 17.1046Z" fill="#32D74B"/>
|
||||
<path d="M13.3538 7.28851L8.7704 11.9209L6.47876 9.60469" stroke="#32D74B" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</svg>`,
|
||||
failed:
|
||||
'<path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0zm0 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1zm0 10.5a.5.5 0 1 1 0 1 .5.5 0 0 1 0-1zm0-8a.5.5 0 0 1 .5.5v6a.5.5 0 1 1-1 0V4a.5.5 0 0 1 .5-.5z"/>',
|
||||
failure: `
|
||||
<svg width="19" height="19" viewBox="0 0 19 19" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<path fill-rule="evenodd" clip-rule="evenodd" d="M9.74048 18.5508C14.711 18.5508 18.7405 14.5213 18.7405 9.55078C18.7405 4.58022 14.711 0.550781 9.74048 0.550781C4.76992 0.550781 0.740479 4.58022 0.740479 9.55078C0.740479 14.5213 4.76992 18.5508 9.74048 18.5508ZM9.74048 17.0507C13.8826 17.0507 17.2405 13.6929 17.2405 9.55074C17.2405 5.4086 13.8826 2.05074 9.74048 2.05074C5.59834 2.05074 2.24048 5.4086 2.24048 9.55074C2.24048 13.6929 5.59834 17.0507 9.74048 17.0507Z" fill="#C7372F"/>
|
||||
<path d="M12.794 11.897L12.794 11.897L10.4474 9.55078L12.794 7.2046L12.794 7.20459C12.8878 7.11079 12.9405 6.98358 12.9405 6.85093C12.9405 6.71828 12.8878 6.59107 12.794 6.49727C12.7002 6.40348 12.573 6.35078 12.4403 6.35078C12.3077 6.35078 12.1805 6.40348 12.0867 6.49727L12.0867 6.49728L9.74048 8.84382L7.3943 6.49728L7.39429 6.49727C7.30049 6.40348 7.17328 6.35078 7.04063 6.35078C6.90798 6.35078 6.78077 6.40348 6.68697 6.49727C6.59317 6.59107 6.54048 6.71828 6.54048 6.85093C6.54048 6.98358 6.59317 7.11079 6.68697 7.20459L6.68698 7.2046L9.03351 9.55078L6.68698 11.897L6.68697 11.897C6.59317 11.9908 6.54048 12.118 6.54048 12.2506C6.54048 12.3833 6.59317 12.5105 6.68697 12.6043C6.78077 12.6981 6.90798 12.7508 7.04063 12.7508C7.17328 12.7508 7.30049 12.6981 7.39429 12.6043L7.3943 12.6043L9.74048 10.2577L12.0867 12.6043L12.0867 12.6043C12.1331 12.6507 12.1882 12.6876 12.2489 12.7127C12.3096 12.7378 12.3746 12.7508 12.4403 12.7508C12.506 12.7508 12.571 12.7378 12.6317 12.7127C12.6924 12.6876 12.7475 12.6507 12.794 12.6043C12.8404 12.5578 12.8773 12.5027 12.9024 12.442C12.9275 12.3814 12.9405 12.3163 12.9405 12.2506C12.9405 12.1849 12.9275 12.1199 12.9024 12.0592C12.8773 11.9986 12.8404 11.9434 12.794 11.897Z" fill="#C7372F" stroke="#C7372F" stroke-width="0.4"/>
|
||||
</svg>`,
|
||||
close:
|
||||
'<path d="M3.646 3.646a.5.5 0 0 1 .708 0L8 7.293l3.646-3.647a.5.5 0 0 1 .708.708L8.707 8l3.647 3.646a.5.5 0 0 1-.708.708L8 8.707l-3.646 3.647a.5.5 0 0 1-.708-.708L7.293 8 3.646 4.354a.5.5 0 0 1 0-.708z"/>',
|
||||
animatedLoader: `
|
||||
@ -123,6 +126,8 @@
|
||||
case 'success':
|
||||
statusBox.innerHTML = systemIcons.success
|
||||
break
|
||||
case 'failure':
|
||||
statusBox.innerHTML = systemIcons.failure
|
||||
}
|
||||
}
|
||||
break
|
||||
@ -224,6 +229,8 @@
|
||||
return systemIcons.animatedLoader
|
||||
case 'success':
|
||||
return systemIcons.success
|
||||
case 'failure':
|
||||
return systemIcons.failure
|
||||
default:
|
||||
return undefined
|
||||
}
|
||||
|
||||
@ -6,8 +6,8 @@
|
||||
|
||||
<title>Omnivore background</title>
|
||||
|
||||
<script src="/scripts/common.js"></script>
|
||||
<script src="/scripts/api.js"></script>
|
||||
<script src="/scripts/common.js"></script>
|
||||
<script src="/scripts/background.js"></script>
|
||||
</head>
|
||||
|
||||
|
||||
@ -71,6 +71,7 @@
|
||||
margin-left: auto;
|
||||
}
|
||||
#omnivore-toast-container button:hover {
|
||||
color: #3B3A38;
|
||||
background-color: #EBEBEB;
|
||||
}
|
||||
#omnivore-toast-container .omnivore-save-button button {
|
||||
|
||||
Reference in New Issue
Block a user