Skip to content

Commit

Permalink
feat(price create form): store last currency used in localstorage (#47)
Browse files Browse the repository at this point in the history
* Price create form: store last currency used

* Add comment
  • Loading branch information
raphodn authored Dec 22, 2023
1 parent ffe54ef commit 2b05eb1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
27 changes: 20 additions & 7 deletions src/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ import { useCookies } from '@vueuse/integrations/useCookies'
const USERNAME_COOKIE_KEY = 'username'
const TOKEN_COOKIE_KEY = 'access_token'
const RECENT_LOCATIONS_LOCAL_STORAGE_KEY = 'recent_locations'
const LAST_CURRENCY_USED_LOCAL_STORAGE_KEY = 'last_currency_used'


function getOrCreateLocalStorageItem(itemKey, defaultValue=[]) {
function getOrCreateLocalStorageItem(itemKey, defaultValue='') {
if (!localStorage.getItem(itemKey)) {
localStorage.setItem(itemKey, JSON.stringify(defaultValue));
}
return localStorage.getItem(itemKey);
}

function clearLocalStorageItem(itemKey, defaultValue=[]) {
function clearLocalStorageItem(itemKey, defaultValue='') {
return localStorage.setItem(itemKey, JSON.stringify(defaultValue));
}

function getParsedLocalStorageItem(itemKey) {
let item = getOrCreateLocalStorageItem(itemKey);
function getParsedLocalStorageItem(itemKey, defaultValue='') {
let item = getOrCreateLocalStorageItem(itemKey, defaultValue);
return JSON.parse(item);
}

function setValueToLocalStorageItem(itemKey, value) {
return localStorage.setItem(itemKey, JSON.stringify(value));
}

function addObjectToLocalStorageItemList(itemKey, obj, avoidDuplicates=true) {
let itemJSON = getParsedLocalStorageItem(itemKey);
let itemJSON = getParsedLocalStorageItem(itemKey, []);
var existingItem = itemJSON.find(item => JSON.stringify(item) === JSON.stringify(obj));
if (avoidDuplicates && existingItem) {
return;
Expand Down Expand Up @@ -112,7 +117,7 @@ export default {
},

getRecentLocations() {
return getParsedLocalStorageItem(RECENT_LOCATIONS_LOCAL_STORAGE_KEY)
return getParsedLocalStorageItem(RECENT_LOCATIONS_LOCAL_STORAGE_KEY, [])
},

addRecentLocation(location) {
Expand All @@ -121,5 +126,13 @@ export default {

clearRecentLocations() {
clearLocalStorageItem(RECENT_LOCATIONS_LOCAL_STORAGE_KEY)
}
},

getLastCurrencyUsed() {
return getParsedLocalStorageItem(LAST_CURRENCY_USED_LOCAL_STORAGE_KEY, 'EUR') // TODO: init with user locale?
},

setLastCurrencyUsed(currency) {
return setValueToLocalStorageItem(LAST_CURRENCY_USED_LOCAL_STORAGE_KEY, currency)
},
}
3 changes: 2 additions & 1 deletion src/views/AddPriceSingle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export default {
proof_id: null,
product_code: '',
price: null,
currency: 'EUR',
currency: api.getLastCurrencyUsed(),
location_osm_id: null,
location_osm_type: '',
date: new Date().toISOString().substr(0, 10)
Expand Down Expand Up @@ -247,6 +247,7 @@ export default {
if (data['detail']) {
alert(`Error: with input ${data['detail'][0]['input']}`)
} else {
api.setLastCurrencyUsed(this.addPriceSingleForm.currency)
this.$router.push({ path: '/add', query: { singleSuccess: 'true' } })
}
this.createPriceLoading = false
Expand Down

0 comments on commit 2b05eb1

Please sign in to comment.