Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Migrate to vue-query #2567

Merged
merged 227 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
227 commits
Select commit Hold shift + click to select a range
dc849cd
Clearer name for offer queries
nicksellen Jul 18, 2022
5766ab6
Initial version of vue-query for activities
nicksellen Jul 20, 2022
eb6b899
Add instrumentation for some enrich getters
nicksellen Jul 20, 2022
b2099e8
WIP Experimentation/exploration about getters
nicksellen Jul 20, 2022
7d088da
SHOW_PERFORMANCE_INFO also enable instrumentation
nicksellen Jul 20, 2022
db88a54
Refine activity users
nicksellen Jul 20, 2022
94b3c40
Work in progress queries/mutations/services
nicksellen Jul 22, 2022
2acfbd8
Ongoing vue-query work
nicksellen Jul 23, 2022
62a00cb
Don't require services to have an id
nicksellen Jul 28, 2022
9ba05c6
Switch from useRouteParam to useQueryParams
nicksellen Jul 28, 2022
f8cd059
Less activity query caching
nicksellen Jul 28, 2022
6cceb7d
Improved group activities page
nicksellen Jul 29, 2022
fcc09dd
Use vue-query for place activities
nicksellen Jul 29, 2022
3417890
Continuing with vue-query for places
nicksellen Jul 29, 2022
62d4243
Ongoing vue-query work
nicksellen Jul 29, 2022
9ed3e85
Implement status in vue-query
nicksellen Jul 29, 2022
7d7c274
More vue-query ongoing work
nicksellen Jul 30, 2022
4de9771
Remove status datastore
nicksellen Aug 1, 2022
f677c1d
More places/feedback into vue-query
nicksellen Aug 1, 2022
0979cba
A lot more vue-query...
nicksellen Aug 2, 2022
564d401
Remove unneeded code
nicksellen Aug 2, 2022
8be7962
Vue-query mutations for activity editing
nicksellen Aug 2, 2022
02f2d3e
Remove unused code
nicksellen Aug 2, 2022
54082a5
Ongoing
nicksellen Aug 2, 2022
c8134b3
Vue-query for history
nicksellen Aug 3, 2022
f6448e5
Remove unused history datastore
nicksellen Aug 3, 2022
626a7fa
Use vue-query for activity type editing
nicksellen Aug 3, 2022
ae8b51c
Some cleanup, and more implementation
nicksellen Aug 3, 2022
f22c94e
Clear data on login, minor tweaks
nicksellen Aug 3, 2022
ead8f9d
Initial vue-query for messages
nicksellen Aug 3, 2022
2e4c8ec
indexById for feedback
nicksellen Aug 3, 2022
bed6b64
Cleanup
nicksellen Aug 3, 2022
61dc017
Adding detail threads to vue-query
nicksellen Aug 4, 2022
2a69ce8
Working thread detail view :)
nicksellen Aug 8, 2022
283ca24
Only current group users mentionable
nicksellen Aug 8, 2022
a0551ae
Faked threadMeta for new threads
nicksellen Aug 8, 2022
4057f2c
20 messages per page
nicksellen Aug 8, 2022
84d59f1
Make place wall work again
nicksellen Aug 8, 2022
32da234
Add state docs
nicksellen Aug 8, 2022
b0c1f16
Don't refetch messages on window focus
nicksellen Aug 8, 2022
6dedf15
Add more state/query/mutation/enrichment docs
nicksellen Aug 8, 2022
c4d0f10
vue-query for application list
tiltec Aug 8, 2022
b897060
Convert activity enrichers to helpers
nicksellen Aug 9, 2022
f9b0074
Fix is-unread for walls
nicksellen Aug 9, 2022
9dc1587
Fixup user list and add trust/revoke mutations
nicksellen Aug 9, 2022
46b9e71
Fixup unread/continuation logic
nicksellen Aug 9, 2022
9b28f46
Implement detail view for activities
nicksellen Aug 9, 2022
ace78fb
Slightly improvement to detail
nicksellen Aug 9, 2022
9bb88f5
Implement user chat detail view
nicksellen Aug 9, 2022
dfc5f28
remove enriching for applications list
tiltec Aug 9, 2022
bb236e9
show application in detail view
tiltec Aug 9, 2022
8a2a84f
Add explicit ordering and button to load older
nicksellen Aug 9, 2022
261d0b8
Specify ordering for application messages
nicksellen Aug 9, 2022
62bd744
Add more sensible page size
nicksellen Aug 9, 2022
f36a469
Fix place display
nicksellen Aug 9, 2022
6ba731f
Convert remaining enrichers to helpers
nicksellen Aug 10, 2022
cff681d
Fix sidenav group map
nicksellen Aug 10, 2022
b33d1c0
Refactor sidenavboxes
nicksellen Aug 10, 2022
655dd2b
Use more bootstrap data
nicksellen Aug 10, 2022
06a9059
A few bits and bobs
nicksellen Aug 10, 2022
a759681
Don't fetch users/group-info in vuex...
nicksellen Aug 10, 2022
01c59d7
Move more breadcrumb logic to component
nicksellen Aug 10, 2022
cfe3b38
Remove some route store actions
nicksellen Aug 10, 2022
4bfe494
Fix activity history statistics
nicksellen Aug 10, 2022
8940839
Convert breadcrumb datastore
nicksellen Aug 10, 2022
460c323
Remove loadingprogress datastore
nicksellen Aug 10, 2022
2113fe2
Implement search without vuex
nicksellen Aug 10, 2022
7f04d01
Add back esc key to close search
nicksellen Aug 10, 2022
36ddca4
Fixup offers chat
nicksellen Aug 10, 2022
c7140fe
applications: create and withdraw
tiltec Aug 10, 2022
6e4b688
Remove debug <pre>
nicksellen Aug 11, 2022
ad8b095
Implement user-not-found logic
nicksellen Aug 11, 2022
d4020b9
Close detail service when using detail route
nicksellen Aug 11, 2022
085812d
Save detail notification options via vue-query
nicksellen Aug 11, 2022
ef63df2
Remove unused actions
nicksellen Aug 11, 2022
73841eb
Move home of current group to service from vuex
nicksellen Aug 11, 2022
b4dd860
Add TODO
nicksellen Aug 11, 2022
252ac71
fix applications in group gallery
tiltec Aug 11, 2022
59ed8ea
remove deprecated group join feature
tiltec Aug 11, 2022
398866f
add application accept/decline, cleanup
tiltec Aug 11, 2022
4188204
add applications updater, remove applications datastore
tiltec Aug 12, 2022
07ce7ee
convert notifications, first try
tiltec Aug 12, 2022
96b916c
application api cleanup
tiltec Aug 12, 2022
ef9f681
fix notification routes
tiltec Aug 13, 2022
d19e255
Move markUserActive to service
nicksellen Aug 15, 2022
a9fa7b8
Use composition API for performance tool
nicksellen Aug 15, 2022
71b7cac
Remove unused code
nicksellen Aug 15, 2022
5e5676a
Group socket updaters
nicksellen Aug 15, 2022
c02557c
Move banners out of store and implement toasts
nicksellen Aug 15, 2022
95620ce
Move geo out of datastore
nicksellen Aug 15, 2022
6b1a01d
Remove agreements
nicksellen Aug 15, 2022
bb76531
Less currentStore stuff
nicksellen Aug 15, 2022
323120f
Merge branch 'add/more-vue-query' of github.com:karrot-dev/karrot-fro…
nicksellen Aug 15, 2022
7085152
Migrate out more currentGroup.js
nicksellen Aug 15, 2022
7c75153
And more away from currentGroup store
nicksellen Aug 15, 2022
4fe2df5
Fix groups in a couple of places
nicksellen Aug 15, 2022
f104335
A whole bunch of vue-query progress
nicksellen Aug 15, 2022
e8608da
latestMessages, first steps
tiltec Aug 15, 2022
c4da15c
add todo
tiltec Aug 15, 2022
5aab389
disable calls to latestMessages datastore
tiltec Aug 15, 2022
b580512
Moved filtered places into current group service
nicksellen Aug 15, 2022
15e4f2a
Add artifical response delay for local dev
nicksellen Aug 15, 2022
07a2a7c
Better wall loading status
nicksellen Aug 15, 2022
e62d1b6
Add in-memory infinite scroll for member list
nicksellen Aug 15, 2022
70d19a6
Fixup signup
nicksellen Aug 15, 2022
99d01e4
Fixup history detail page
nicksellen Aug 15, 2022
24650ea
Migrate i18n
nicksellen Aug 15, 2022
be185eb
Remove currentGroup module :)
nicksellen Aug 16, 2022
fefb70e
Remove timezones module
nicksellen Aug 16, 2022
e6c79c3
Migrate community feed vuex store
nicksellen Aug 16, 2022
9ef2428
Remove invitations (they are unused now)
nicksellen Aug 16, 2022
7f14b2f
Remove unused file
nicksellen Aug 16, 2022
37b615f
Remove refresh (and pull to refresh)
nicksellen Aug 16, 2022
2c3a9b3
Close detail view on logout
nicksellen Aug 16, 2022
a5c007e
Don't hijack '/' from input/textareas
nicksellen Aug 16, 2022
9c6c4d0
Fixup groupId param for link
nicksellen Aug 16, 2022
19abc19
Implement mobile views for some detail pages
nicksellen Aug 16, 2022
13638dd
Nicer activity filter wrapping
nicksellen Aug 16, 2022
0c92734
Filter out less useful stuff for search
nicksellen Aug 16, 2022
5fb01c5
latestMessages: make it mostly work!
tiltec Aug 16, 2022
e70189f
Fancier dev sleep feature, not automatically on
nicksellen Aug 16, 2022
78cf9cd
Fix user chat mobile view
nicksellen Aug 16, 2022
9c80aae
Fixup latest messages opening things
nicksellen Aug 16, 2022
47c0839
Show detail sidebar over detail router view
nicksellen Aug 16, 2022
0de3ea1
Prevent overflowing slots
nicksellen Aug 16, 2022
ac167c9
Allow to dismiss community banner
nicksellen Aug 17, 2022
84ed927
Add application mobile route
nicksellen Aug 17, 2022
a7bf234
Migrate issues
nicksellen Aug 17, 2022
fe5af23
Fix query update helper
nicksellen Aug 17, 2022
47f9979
Remove issues store
nicksellen Aug 17, 2022
7302b80
Fix bread for active issue
nicksellen Aug 17, 2022
a6a4ad6
Almost removed groups datastore
nicksellen Aug 17, 2022
d75f477
Don't use stuff from >/helpers
nicksellen Aug 17, 2022
9407622
Only worry about query order meta when needed
nicksellen Aug 17, 2022
d25a282
Better activities view with no activities
nicksellen Aug 17, 2022
098dd23
Minor improvements to application detail UI
nicksellen Aug 17, 2022
dc7cf1f
Adding missing .value
nicksellen Aug 17, 2022
1c9099a
Remove calls to removed functions
nicksellen Aug 18, 2022
6f7aa6a
Fix missing order meta param, coding error
nicksellen Aug 18, 2022
d4f7c55
Fixup group create
nicksellen Aug 18, 2022
8cfebab
Reimplement and remove groups store module
nicksellen Aug 18, 2022
220eff6
Disable vuex stores!
nicksellen Aug 18, 2022
2594ce1
Fix axios handling of nulls
nicksellen Aug 18, 2022
d55b351
Remove .catch() calls from router push add debug
nicksellen Aug 18, 2022
a55bc18
Attempt to resolve router issue
nicksellen Aug 18, 2022
97f7200
Fix reactivity bug, must pass refs to query keys
nicksellen Aug 18, 2022
e063f43
Reorder imports and enforce with eslint
nicksellen Aug 19, 2022
1bbc6e9
Reimplement password reset
nicksellen Aug 19, 2022
abd2c60
Reimplement unsubscribe all for group
nicksellen Aug 19, 2022
d31b62d
Remove unused $store usage
nicksellen Aug 19, 2022
75cd76f
A little auth isLogged in refinement
nicksellen Aug 19, 2022
c5b6476
Convert push and pwa stuff
nicksellen Aug 19, 2022
f6771dd
Push tweaks and blind cordova implementations
nicksellen Aug 19, 2022
b36f28d
A bit more push token fiddling
nicksellen Aug 20, 2022
44800e6
Fixup multicroppa
nicksellen Aug 22, 2022
f8b6379
Send x-forwarded-for headers in dev proxy
nicksellen Aug 22, 2022
c2c2ed0
Only load cordova stuff if needed
nicksellen Aug 22, 2022
0121ba9
Fix up profile image edit
nicksellen Aug 22, 2022
642d95b
Remove away logging
nicksellen Aug 22, 2022
f95c420
Activity type editing remove vuex calls
nicksellen Aug 22, 2022
eaf6aac
Remove vuex from feedback list
nicksellen Aug 22, 2022
c1f0ae4
Remove vuex!
nicksellen Aug 22, 2022
a406cc8
Update vuex -> vue-query in readme
nicksellen Aug 22, 2022
ece5358
Remove unused datastore helpers
nicksellen Aug 22, 2022
c4b1d41
Initialize router plugin inside detail service
nicksellen Aug 22, 2022
a4eceb8
Linting fix
nicksellen Aug 22, 2022
89f58ef
Remove old datastore helpers tests
nicksellen Aug 22, 2022
de29d75
Refine eslint import order config
nicksellen Aug 22, 2022
60c52e3
Fixup offer query tests
nicksellen Aug 22, 2022
01fd94b
Fixup changephoto test
nicksellen Aug 22, 2022
d14aaad
Make tests pass, upgrade to jest 28
nicksellen Aug 22, 2022
0c34f0a
Enhance mock backend a little
nicksellen Aug 22, 2022
0c9aa78
Fixup linting
nicksellen Aug 22, 2022
0896578
Nicer "your application is pending" alignment
nicksellen Aug 22, 2022
4199da9
Slightly more refined application pending UI
nicksellen Aug 22, 2022
31fe337
Merge branch 'master' into add/more-vue-query
tiltec Aug 24, 2022
6124ae1
remove notice
tiltec Aug 24, 2022
62575e5
TODO is done
tiltec Aug 24, 2022
9f97820
Fix message reaction user names
nicksellen Aug 24, 2022
d447738
Fix chat conversation messages spilling out
nicksellen Aug 24, 2022
3e27778
Fix mobile reaction list dialog
nicksellen Aug 25, 2022
55c833a
Don't log ECONNABORTED query errors
nicksellen Aug 25, 2022
96195b3
Use native isArray method
nicksellen Aug 25, 2022
9038b17
Use object format for input-style
nicksellen Aug 25, 2022
c45e874
Use overflow: hidden for autogrow inputs
nicksellen Aug 25, 2022
68ad4fa
Remove unused class, use object shorthand notation
nicksellen Aug 25, 2022
821c6cc
Set groupId in link explicitly
nicksellen Aug 25, 2022
1a486fb
Refine note about archived activity type filtering
nicksellen Aug 25, 2022
d3c7099
Remove getPreviousPage from state docs
nicksellen Aug 25, 2022
9e08b46
Make sure activityTypeIconProps are present
nicksellen Aug 25, 2022
dbcb168
Remove unused mutationOptions configuration
nicksellen Aug 25, 2022
02f66bb
Remove unused listMore methods
nicksellen Aug 25, 2022
08a1503
Remove console log
nicksellen Aug 25, 2022
7d9db66
Remove console logging
nicksellen Aug 25, 2022
c59c4f4
Remove outdated comment
nicksellen Aug 25, 2022
cdafbb3
Remove unused createInstrument
nicksellen Aug 25, 2022
667d4b9
A little rejig of boot files
nicksellen Aug 25, 2022
5385b30
Add more groupIds to router links
nicksellen Aug 25, 2022
da664fd
Safer ref access
nicksellen Aug 25, 2022
7ba589e
Better function naming
nicksellen Aug 25, 2022
53d9d05
Add back leave group toast
nicksellen Aug 25, 2022
5f8870f
Use maybeUpdateDataWith more and document
nicksellen Aug 25, 2022
6b48740
Add TODO
nicksellen Aug 25, 2022
e806f45
Remove unneeded comments/logs
nicksellen Aug 25, 2022
fde597c
Remove empty file
nicksellen Aug 25, 2022
92f9e0b
Remove logging, add TODO detail
nicksellen Aug 25, 2022
b2acabc
Add "Load earlier messages" to translations
nicksellen Aug 25, 2022
a0fbce2
Re-enable eslint
nicksellen Aug 25, 2022
152343d
Use maybeUpdateDataWith for message detail
nicksellen Aug 25, 2022
35f7c40
Remove unneeded getPreviousPageParam
nicksellen Aug 25, 2022
e919c4b
Alter comments
nicksellen Aug 25, 2022
cd98e05
Remove commented out code
nicksellen Aug 25, 2022
5e8039a
Add comment
nicksellen Aug 25, 2022
04b11a6
Remove dev/debug code
nicksellen Aug 25, 2022
98a0d6d
Remove enrichment related code
nicksellen Aug 25, 2022
3b0ac23
Remove commented out code
nicksellen Aug 25, 2022
b0e555b
Add comment
nicksellen Aug 25, 2022
1cce976
Remove unused test file
nicksellen Aug 25, 2022
345e59f
Remove commented out line
nicksellen Aug 25, 2022
e08d4a8
Only use lodash random if we need it in dev
nicksellen Aug 23, 2022
d548585
fix feedback pagination
tiltec Aug 25, 2022
cd49bec
fix activity statistics periodFilter
tiltec Aug 25, 2022
6d86039
remove clearIssueMeta comment
tiltec Aug 25, 2022
4954e7c
enable period user refresh
tiltec Aug 25, 2022
5ff9f5e
remove notification meta
tiltec Aug 25, 2022
64297ff
remove WallConversation TODO
tiltec Aug 25, 2022
614ae1e
remove old code
tiltec Aug 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 35 additions & 12 deletions src/activities/pages/GroupActivities.vue
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,16 @@ import KSpinner from '@/utils/components/KSpinner'
import ActivityList from '@/activities/components/ActivityList'
import KNotice from '@/utils/components/KNotice'
import PlaceList from '@/places/components/PlaceList'
import { useActivityListQuery } from '@/activities/queries'
import { queryKeyActivityList, useActivityListQuery } from '@/activities/queries'
import reactiveNow from '@/utils/reactiveNow'
import { useActivityEnricher, useActivityTypeEnricher } from '@/activities/enrichers'
import { useActivityService } from '@/activities/services'
import { useCurrentGroupService } from '@/group/services'
import { usePlaceEnricher } from '@/places/enrichers'
import { usePlaceService } from '@/places/services'
import { useRouteParam } from '@/utils/mixins/bindRoute'
import { useQueryParams } from '@/utils/mixins/bindRoute'
import ICSBtn from '@/activities/components/ICSBtn'
import { useQueryClient } from 'vue-query'

export default {
components: {
Expand Down Expand Up @@ -184,12 +185,19 @@ export default {
const enrichActivityType = useActivityTypeEnricher()
const enrichPlace = usePlaceEnricher()

const type = useRouteParam('type')
const slots = useRouteParam('slots')
const place = useRouteParam('place')
const defaultQueryParams = {
type: null,
slots: null,
place: null,
}

const {
type,
slots,
place,
} = useQueryParams(defaultQueryParams)

function clearFilters () {
// TODO: if they are all set, clearing them like this doesn't actually work... like non-atomic updates to route.query...
type.value = null
slots.value = null
place.value = null
Expand All @@ -200,30 +208,45 @@ export default {
return new Date(Math.floor(new Date().getTime() / roundTo) * roundTo)
}

const queryClient = useQueryClient()

/*
TODO: something funny happening where it sort of freezes when there is query cache...
tiltec marked this conversation as resolved.
Show resolved Hide resolved
steps:
- visit http://localhost:8080/#/group/6/activities?type=15 (i.e. with filter selected)
- then select "All types" in the filter
- notice no freeze (even though it pauses to load, browser stays responsive)
- select some other type, then select "All types" again
- now there is some weird kind of lag...

The difference is that the second time the queryClient already has data...

cacheTime: 0 makes it go away, maybe that's fine...

*/
const {
isLoading,
isFetching,
isFetchingNextPage,
activities,
hasNextPage,
fetchNextPage,
remove,
// remove,
} = useActivityListQuery({
groupId,
placeId: place,
// so we can use cached query results for a while, otherwise it'll always be a fresh query
dateMin: newDateRoundedTo5Minutes(),
activityTypeId: type,
slots,
// TODO: add activity type filter for query to backend...
// so we can use cached query results for a while, otherwise it'll always be a fresh query
dateMin: newDateRoundedTo5Minutes(),
}, {
// TODO: consider this a bit more... ideally can speed up the activity rendering so we can keep more results, or just clear extra pages?
// cacheTime: 0,
cacheTime: 0,
})

onUnmounted(() => {
// really just putting this here as don't want to keep more than first page in cache and haven't worked out how to do it better yet...
remove()
// remove()
})

function isStartedOrUpcoming (activity) {
Expand Down
2 changes: 1 addition & 1 deletion src/activities/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { computed, unref } from 'vue'
import { useActivityTypeListQuery } from '@/activities/queries'
import { defineService, indexById } from '@/utils/datastore/helpers'

export const useActivityService = defineService('activity', () => {
export const useActivityService = defineService(() => {
// queries
const { activityTypes } = useActivityTypeListQuery()

Expand Down
2 changes: 1 addition & 1 deletion src/alerts/services.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineService } from '@/utils/datastore/helpers'
import { useStore } from 'vuex'

export const useToastService = defineService('toast', () => {
export const useToastService = defineService(() => {
const store = useStore()

// methods
Expand Down
2 changes: 1 addition & 1 deletion src/authuser/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineService } from '@/utils/datastore/helpers'
import { useAuthUserQuery } from '@/authuser/queries'
import { computed } from 'vue'

export const useAuthService = defineService('authuser', () => {
export const useAuthService = defineService(() => {
// queries
const { user, refetch } = useAuthUserQuery()

Expand Down
2 changes: 1 addition & 1 deletion src/base/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readonly, ref } from 'vue'
import { DEFAULT_LOCALE, detectLocale } from '@/base/datastore/i18n'
import { defineService } from '@/utils/datastore/helpers'

export const useI18nService = defineService('i18n', () => {
export const useI18nService = defineService(() => {
const locale = ref(detectLocale() || DEFAULT_LOCALE)

function setLocale (value) {
Expand Down
2 changes: 1 addition & 1 deletion src/group/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useStore } from 'vuex'
import { useUserService } from '@/users/services'
import { usePlaceService } from '@/places/services'

export const useCurrentGroupService = defineService('group', () => {
export const useCurrentGroupService = defineService(() => {
// services
const store = useStore()
const {
Expand Down
2 changes: 1 addition & 1 deletion src/groupInfo/services.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineService } from '@/utils/datastore/helpers'
import { useGroupInfoListQuery } from '@/groupInfo/queries'

export const useGroupInfoService = defineService('groupInfo', () => {
export const useGroupInfoService = defineService(() => {
// queries
// TODO: filtering for active status?
const { groups } = useGroupInfoListQuery()
Expand Down
6 changes: 4 additions & 2 deletions src/offers/pages/GroupOffers.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ import DateAsWords from '@/utils/components/DateAsWords'

import { DEFAULT_STATUS, useOfferListQuery } from '@/offers/queries'

import { useRouteParam } from '@/utils/mixins/bindRoute'
import { useQueryParams } from '@/utils/mixins/bindRoute'
import { useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useStoreUsers } from '@/users/queries'
Expand All @@ -146,7 +146,9 @@ const statusOptions = [

const { groupId } = useCurrentGroupService()

const status = useRouteParam('status', DEFAULT_STATUS)
const { status } = useQueryParams({
status: DEFAULT_STATUS,
})

const { getUserRef } = useStoreUsers()

Expand Down
2 changes: 1 addition & 1 deletion src/places/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { computed, unref } from 'vue'
import { defineService, indexById } from '@/utils/datastore/helpers'
import { usePlaceListQuery } from '@/places/queries'

export const usePlaceService = defineService('place', () => {
export const usePlaceService = defineService(() => {
// queries
const { places } = usePlaceListQuery()

Expand Down
2 changes: 1 addition & 1 deletion src/users/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { computed } from 'vue'
import { useUserListAllQuery } from '@/users/queries'
import { indexBy, indexById, defineService } from '@/utils/datastore/helpers'

export const useUserService = defineService('users', () => {
export const useUserService = defineService(() => {
// queries
const { isLoading, users } = useUserListAllQuery()

Expand Down
26 changes: 16 additions & 10 deletions src/utils/datastore/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,22 +358,28 @@ export function toggles (config) {
return result
}

const stores = []
export function defineService (id, storeSetup) {
export function defineService (serviceSetup) {
if (process.env.DEV) {
if (typeof id !== 'string' || typeof storeSetup !== 'function') {
throw new Error('must be string|function args to defineService')
if (typeof serviceSetup !== 'function') {
throw new Error('must pass a serviceSetup function to defineService')
}
}

// hold a reference to our service instance in this outer scope so we always return the same one
let service

return () => {
if (stores[id]) return stores[id]
let store
// We use a custom detached scope, otherwise it'll be cleared away, but we want this to stay around for reuse
// it's already setup, can just return our service instance
if (service) return service

// Create a detached scope so it will stay around beyond the lifecycle of the initial setup
const scope = effectScope(true)

scope.run(() => {
store = storeSetup()
// initialize our service in this scope, we get back a value, nothing fancy!
service = serviceSetup()
})
stores[id] = store
return store

return service
}
}
80 changes: 56 additions & 24 deletions src/utils/mixins/bindRoute.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed } from 'vue'
import { reactive, toRefs, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'

export default function bindRoute (params) {
Expand Down Expand Up @@ -33,31 +33,63 @@ export default function bindRoute (params) {
return mixin
}

export function useRouteParam (name, defaultValue = null) {
/**
* Create refs that map to query params.
*
* You must pass in defaults for all the parameters you are interested in.
* e.g.
* const { type, category } = useQueryParams({ type: 'animal', category: null })
*
* You can destructure the result, each param is a read/write ref that will keep
* up to date with the underlying query param.
*/
export function useQueryParams (defaultParams) {
const router = useRouter()
const route = useRoute()

return computed({
get () {
if (Object.prototype.hasOwnProperty.call(route.query, name)) {
return route.query[name]
}
else {
return defaultValue
}
},
set (val) {
if (val === defaultValue) {
if (!route.query[name]) return // not even in the query
// if value is same as the default, remove it from the query to keep the URL neat
const query = { ...route.query }
delete query[name]
router.replace({ query }).catch(() => {})
}
else {
if (route.query[name] === val) return // hasn't changed
router.replace({ query: { ...route.query, ...{ [name]: val } } }).catch(() => {})
}
},
// The defaults passed in form our base reactive state
const state = reactive({ ...defaultParams })

// Keep track of when we are updating so we can avoid circular updates
let updatingQuery = false
let updatingState = false

// Read the initial query params so our state is up to date
updateState()

// Watch for updates to state and query
watch(state, () => {
if (!updatingState) updateQuery()
})
watch(() => route.query, () => {
if (!updatingQuery) updateState()
})

// Set the state from the query
// If the query is missing a param, uses the default
function updateState () {
updatingState = true
for (const name of Object.keys(defaultParams)) {
state[name] = route.query[name] || defaultParams[name]
}
updatingState = false
}

// Set the query from the state
// No query param is set for falsey or default values
function updateQuery () {
updatingQuery = true
const query = {}
for (const name of Object.keys(state)) {
const value = state[name]
if (value && value !== defaultParams[name]) {
query[name] = value
}
}
router.replace({ query }).catch(() => {}).then(() => {
updatingQuery = false
})
}

return toRefs(state)
}