Skip to content

Commit

Permalink
feat: ✨ users can now create landmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBroughton committed Nov 19, 2023
1 parent c5af2ec commit 2f5dabb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-yaks-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"forager": minor
---

Users can now create landmarks
8 changes: 4 additions & 4 deletions src/components/ItemDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { usePocketBase } from '@/pocketbase'
import { useMapbox } from '@/mapbox'
const mapboxStore = useMapbox()
const { selectedItem } = storeToRefs(mapboxStore)
const { selectedItem, selectedCollection } = storeToRefs(mapboxStore)
const pocketbaseStore = usePocketBase()
const { selectedItemPocketbase } = storeToRefs(pocketbaseStore)
watch(() => selectedItem.value, () => {
if (selectedItem.value !== undefined)
pocketbaseStore.getSelectedItem(selectedItem.value)
pocketbaseStore.getSelectedItem(selectedItem.value, selectedCollection.value)
})
function clearSelected() {
Expand Down Expand Up @@ -51,7 +51,7 @@ const previewImg = computed(() => {
</div>
</Teleport>
</div>
<div class="item-forage-details">
<div v-if="selectedCollection === 'items'" class="item-forage-details">
<p>Last Foraged: {{ selectedItemPocketbase.lastForaged ? new Date(selectedItemPocketbase.lastForaged!).toDateString() : 'Never' }}</p>
<p> Start Month: {{ selectedItemPocketbase.startMonth || 'Not Set' }}</p>
<p>End Month: {{ selectedItemPocketbase.endMonth || 'Not Set' }}</p>
Expand All @@ -60,7 +60,7 @@ const previewImg = computed(() => {
<BaseButton @click="deleteItem">
Delete
</BaseButton>
<BaseButton @click="forageItem">
<BaseButton v-if="selectedCollection === 'items'" @click="forageItem">
Forage Now
</BaseButton>
<BaseButton @click="clearSelected">
Expand Down
62 changes: 50 additions & 12 deletions src/mapbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const useMapbox = defineStore('mapbox-store', () => {
const lat = ref(0)
const markerUIHidden = ref(true)
const selectedItem = ref<string | undefined>(undefined)
const selectedCollection = ref('')
const detailsHidden = ref(true)
const items = ref<ItemsRecordWithID[] | null>(null)
const landmarks = ref<LandmarksRecordWithID[]>([])
Expand Down Expand Up @@ -203,6 +204,7 @@ export const useMapbox = defineStore('mapbox-store', () => {
markerUIHidden.value = true
detailsHidden.value = false
selectedItem.value = e.features![0].properties!.id
selectedCollection.value = 'items'
})

// Change the cursor to a pointer when the mouse is over the places layer.
Expand All @@ -216,6 +218,22 @@ export const useMapbox = defineStore('mapbox-store', () => {
if (map)
map.getCanvas().style.cursor = ''
})

map.on('click', 'landmark', async (e) => {
markerUIHidden.value = true
detailsHidden.value = false
selectedItem.value = e.features![0].properties!.id
selectedCollection.value = 'landmarks'
})
map.on('mouseenter', 'landmark', () => {
if (map)
map.getCanvas().style.cursor = 'pointer'
})

map.on('mouseleave', 'landmark', () => {
if (map)
map.getCanvas().style.cursor = ''
})
}

const addLandmark = async (landmark: Omit<LandmarksRecord, 'owner'>) => {
Expand Down Expand Up @@ -281,24 +299,43 @@ export const useMapbox = defineStore('mapbox-store', () => {
}

const deleteMarker = async (id: string) => {
const { getItems, deleteItem } = usePocketBase()
const { getItems, getLandmarks, deleteItem } = usePocketBase()

await deleteItem(id)
await deleteItem(id, selectedCollection.value)

items.value = await getItems()
if (selectedCollection.value === 'items') {

const itemLayer = ref<Feature[]>([])
items.value = await getItems()

// Get the GeoJSON source by ID
const source = map?.getSource('items') as mapboxgl.GeoJSONSource
const itemLayer = ref<Feature[]>([])

if (items.value)
itemLayer.value = translateItemToLayerItem(items.value)
// Get the GeoJSON source by ID
const source = map?.getSource(selectedCollection.value) as mapboxgl.GeoJSONSource

source.setData({
type: 'FeatureCollection',
features: itemLayer.value,
})
if (items.value)
itemLayer.value = translateItemToLayerItem(items.value)

source.setData({
type: 'FeatureCollection',
features: itemLayer.value,
})
}
if (selectedCollection.value === 'landmarks') {
landmarks.value = await getLandmarks()

const itemLayer = ref<Feature[]>([])

// Get the GeoJSON source by ID
const source = map?.getSource(selectedCollection.value) as mapboxgl.GeoJSONSource

if (landmarks.value)
itemLayer.value = translateItemToLayerItem(landmarks.value)

source.setData({
type: 'FeatureCollection',
features: itemLayer.value,
})
}
}

const updateMarkerLayer = async () => {
Expand Down Expand Up @@ -338,5 +375,6 @@ export const useMapbox = defineStore('mapbox-store', () => {
items,
updateMarkerLayer,
addLandmark,
selectedCollection,
}
})
2 changes: 1 addition & 1 deletion src/mapbox/layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function createLayers(map: mapboxgl.Map) {

export function createLandmarks(map: mapboxgl.Map) {
map.addLayer({
id: 'unclustered-landmark',
id: 'landmark',
type: 'symbol',
source: 'landmarks',
minzoom: 14,
Expand Down
8 changes: 4 additions & 4 deletions src/pocketbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ export const usePocketBase = defineStore('pocketbase-store', () => {
return response
}

const getSelectedItem = async (id: string) => {
const getSelectedItem = async (id: string, collection: string) => {
try {
// or fetch only the first record that matches the specified filter
selectedItemPocketbase.value = await pb.collection('items').getOne(id)
selectedItemPocketbase.value = await pb.collection(collection).getOne(id)
}
catch (error: unknown) {
if (isError(error))
Expand Down Expand Up @@ -203,9 +203,9 @@ export const usePocketBase = defineStore('pocketbase-store', () => {
}
}

const deleteItem = async (id: string) => {
const deleteItem = async (id: string, collection: string) => {
try {
await pb.collection('items').delete(id)
await pb.collection(collection).delete(id)
await getItems()
}
catch (error: unknown) {
Expand Down

0 comments on commit 2f5dabb

Please sign in to comment.