Skip to content

Commit

Permalink
code review updates, update set key data
Browse files Browse the repository at this point in the history
  • Loading branch information
ionutanin committed Feb 22, 2024
1 parent 9d10c2f commit e2af7cc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/background/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Background {
this.setStorageDefaultData()
}

// TODO: to be moved to a service
async setStorageDefaultData() {
try {
await storage.set({ data: defaultData })
Expand Down
19 changes: 12 additions & 7 deletions src/messageHandlers/getStorageData.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import browser from 'webextension-polyfill'

const storage = browser.storage.sync || browser.storage.local
import { storageApi } from '@/utils/storage'

const getStorageData = async () => {
const data = await storage.get('data')
return {
type: 'SUCCESS',
data,
try {
const data = await storageApi.get('data')
return {
type: 'SUCCESS',
data,
}
} catch (error) {
return {
type: 'ERROR',
error,
}
}
}

Expand Down
19 changes: 12 additions & 7 deletions src/messageHandlers/getStorageKey.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import browser from 'webextension-polyfill'

const storage = browser.storage.sync || browser.storage.local
import { storageApi } from '@/utils/storage'

const getStorageKey = async (key: any) => {
const data = await storage.get('data')
return {
type: 'SUCCESS',
[key]: data?.data[key],
try {
const data = await storageApi.get('data')
return {
type: 'SUCCESS',
[key]: data?.data[key],
}
} catch (error) {
return {
type: 'ERROR',
error,
}
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/messageHandlers/setStorageKey.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import browser from 'webextension-polyfill'
import { storageApi } from '@/utils/storage'

const storage = browser.storage.sync || browser.storage.local

const getStorageKey = async ({ key, value }: { key: string; value: any }) => {
await storage.set({ [key]: value })
return { type: 'SUCCESS' }
const setStorageKey = async ({ key, value }: { key: string; value: any }) => {
try {
const storageData = await storageApi.get('data')
await storageApi.set({ data: { ...storageData, [key]: value } })
return { type: 'SUCCESS' }
} catch (error) {
return { type: 'ERROR', error }
}
}

export default { callback: getStorageKey, type: 'SET_STORAGE_KEY' }
export default { callback: setStorageKey, type: 'SET_STORAGE_KEY' }
8 changes: 6 additions & 2 deletions src/popup/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const Home = () => {
const [receivingPaymentPointer, setReceivingPaymentPointer] = useState('')
const [formData, setFormData] = useState({
paymentPointer: sendingPaymentPointer || '',
amount: 20,
amount: 0,
})

useEffect(() => {
Expand All @@ -55,6 +55,7 @@ export const Home = () => {

const getRateOfPay = async () => {
const response = await getStorageKey('rateOfPay')
console.log('response', response)
response && setRateOfPay(response)
}

Expand Down Expand Up @@ -130,7 +131,10 @@ export const Home = () => {

const updateRateOfPay = async (event: any) => {
setRateOfPay(event.target.value)
// await sendMessage({ type: 'SET_STORAGE_KEY', data: { key: 'rateOfPay', value } })
await sendMessage({
type: 'SET_STORAGE_KEY',
data: { key: 'rateOfPay', value: event.target.value },
})
}

return (
Expand Down
4 changes: 4 additions & 0 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import browser from 'webextension-polyfill'

import { TPopupContext } from '@/providers/providers.interface'
import { sendMessage } from '@/utils/sendMessages'

Expand Down Expand Up @@ -30,3 +32,5 @@ export const getStorageKey = async (key: string) => {
const response: any = await sendMessage({ type: 'GET_STORAGE_KEY', data: key })
return response?.[key]
}

export const storageApi = browser.storage?.sync || browser.storage?.local

0 comments on commit e2af7cc

Please sign in to comment.