Skip to content

Commit

Permalink
feat: create contrib store + share enable state across tabs + persist…
Browse files Browse the repository at this point in the history
… state in localstorage #142
  • Loading branch information
wazolab committed Feb 19, 2024
1 parent 69078eb commit 571180c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
19 changes: 17 additions & 2 deletions middleware/contrib-mode.global.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isArray, mergeWith } from 'lodash'
import { siteStore } from '~/stores/site'
import type { ApiPoi, ApiPoiProperties } from '~/lib/apiPois'
import { EditorialGroupType } from '~/utils/types'
import { STORE_NAME, useContribStore } from '~/stores/contrib'

export interface Link {
icon: string
Expand All @@ -20,7 +20,22 @@ interface ContribProperties {
}

export default defineNuxtRouteMiddleware((to) => {
siteStore().contribMode = to.query.contrib === 'true'
if (process.server)
return

if (process.client) {
const contribLocalStorage = localStorage.getItem(STORE_NAME)

if (to.query.contrib !== 'undefined') {
useContribStore().setEnabled(to.query.contrib === 'true')
return
}

if (contribLocalStorage) {
const state = JSON.parse(contribLocalStorage).enabled
useContribStore().setEnabled(state)
}
}
})

function getEditorialGroup(mode: EditorialGroupType): ApiPoiProperties['editorial'] {
Expand Down
3 changes: 2 additions & 1 deletion pages/category/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { getAsyncDataOrThrows } from '~/lib/getAsyncData'
import { vidoConfig } from '~/plugins/vido-config'
import { menuStore } from '~/stores/menu'
import { siteStore } from '~/stores/site'
import { useContribStore } from '~/stores/contrib'
import type { VidoConfig } from '~/utils/types-config'
import { addContributorFields, isContribEligible } from '~/middleware/contrib-mode.global'
import { EditorialGroupType } from '~/utils/types'
Expand Down Expand Up @@ -134,7 +135,7 @@ export default defineNuxtComponent({
mounted() {
this.locale = this.$i18n.locale
if (siteStore().contribMode) {
if (useContribStore().enabled) {
this.pois.features.map(poi =>
isContribEligible(poi.properties)
? addContributorFields(poi, EditorialGroupType.List)
Expand Down
22 changes: 22 additions & 0 deletions stores/contrib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineStore } from 'pinia'

export const STORE_NAME = 'contrib'

interface State {
enabled: boolean
}

export const useContribStore = defineStore(STORE_NAME, {
state: (): State => ({
enabled: false,
}),
actions: {
setEnabled(state: boolean) {
this.enabled = state
localStorage.setItem(STORE_NAME, JSON.stringify(this.enabled))
},
},
share: {
enable: true,
},
})
4 changes: 2 additions & 2 deletions stores/map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineStore } from 'pinia'
import { siteStore } from './site'
import { useContribStore } from './contrib'
import { addContributorFields, isContribEligible } from '~/middleware/contrib-mode.global'
import type { ApiPoi, ApiPoiProperties } from '~/lib/apiPois'
import type { LatLng, Pitch } from '~/utils/types'
Expand Down Expand Up @@ -60,7 +60,7 @@ export const mapStore = defineStore('map', {
if (feature?.properties) {
const cleanProperties: ApiPoiProperties = {} as ApiPoiProperties

if (siteStore().contribMode && isContribEligible(feature.properties))
if (useContribStore().enabled && isContribEligible(feature.properties))
addContributorFields(feature, EditorialGroupType.Popup)

Object.keys(feature.properties).forEach((key) => {
Expand Down
2 changes: 0 additions & 2 deletions stores/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ interface State {
locale: string | null
config: VidoConfig | undefined
settings: Settings | undefined
contribMode: boolean
contents: ContentEntry[] | undefined
translations: PropertyTranslations | undefined
}
Expand All @@ -18,7 +17,6 @@ export const siteStore = defineStore('site', {
state: (): State => ({
locale: null,
config: undefined,
contribMode: false,
settings: undefined,
contents: undefined,
translations: undefined,
Expand Down

0 comments on commit 571180c

Please sign in to comment.