|
| 1 | +import log from '@swissgeo/log' |
| 2 | + |
| 3 | +import { createShortLink } from '@/api/shortlink.api.js' |
| 4 | +import { defineStore } from 'pinia' |
| 5 | +import type { ActionDispatcher } from '@/store/types' |
| 6 | + |
| 7 | +interface ShareStoreState { |
| 8 | + shortLink: string | null |
| 9 | + isMenuSectionShown: boolean |
| 10 | +} |
| 11 | + |
| 12 | +const useShareStore = defineStore('share', { |
| 13 | + state: (): ShareStoreState => ({ |
| 14 | + /** |
| 15 | + * Short link version of the current map position (and layers, and all...). This will not be |
| 16 | + * defined each time, but only when the share menu is opened first (it will then be updated |
| 17 | + * whenever the URL changes to match it) |
| 18 | + */ |
| 19 | + shortLink: null, |
| 20 | + /** |
| 21 | + * The state of the shortlink share menu section. As we need to be able to change this |
| 22 | + * whenever the user moves the map, and it should only be done within mutations. |
| 23 | + */ |
| 24 | + isMenuSectionShown: false, |
| 25 | + }), |
| 26 | + actions: { |
| 27 | + setShortLink(shortLink: string | null, dispatcher: ActionDispatcher) { |
| 28 | + this.shortLink = shortLink |
| 29 | + }, |
| 30 | + setIsMenuSectionShown(show: boolean, dispatcher: ActionDispatcher) { |
| 31 | + this.isMenuSectionShown = show |
| 32 | + }, |
| 33 | + |
| 34 | + async generateShortLinks(withCrosshair: boolean = false, dispatcher: ActionDispatcher) { |
| 35 | + try { |
| 36 | + const shortLink = await createShortLink(window.location.href, withCrosshair) |
| 37 | + |
| 38 | + if (shortLink) { |
| 39 | + this.setShortLink(shortLink, dispatcher) |
| 40 | + } |
| 41 | + } catch (err) { |
| 42 | + log.error({messages: ['Error while creating short link for', window.location.href, err]}) |
| 43 | + this.setShortLink(window.location.href, dispatcher) |
| 44 | + } |
| 45 | + }, |
| 46 | + closeShareMenuAndRemoveShortLinks(dispatcher: ActionDispatcher) { |
| 47 | + this.setIsMenuSectionShown(false, dispatcher ) |
| 48 | + this.clearShortLinks(dispatcher) |
| 49 | + }, |
| 50 | + toggleShareMenuSection(dispatcher: ActionDispatcher) { |
| 51 | + this.setIsMenuSectionShown(!this.isMenuSectionShown, dispatcher ) |
| 52 | + }, |
| 53 | + clearShortLinks(dispatcher: ActionDispatcher ) { |
| 54 | + this.shortLink = null |
| 55 | + }, |
| 56 | + }, |
| 57 | +}) |
| 58 | + |
| 59 | +export default useShareStore |
0 commit comments