Checking for another store value (auth) in each getter, can this be extracted? #812
Answered
by
BenShelton
notflip
asked this question in
Help and Questions
-
I have my Photo Store, in which I init an Auth Store in 2 getters to see if the user is authenticated, otherwise I return an empty array, I'm wondering if this check could be simplified/extracted to a method to DRY up this piece of code?
Here inside my actions I'm also checking it the auth store has the user
|
Beta Was this translation helpful? Give feedback.
Answered by
BenShelton
Nov 21, 2021
Replies: 1 comment 1 reply
-
You could use a function, it can be outside the store definition, either in the same file or in another file if you want to use this check across multiple stores (or even in components): function userMissing (): boolean {
const authStore = useAuth()
const authUser = authStore.user
return authUser === undefined
}
export const useWhateverStore = defineStore(/* ... */) And then in the getters/actions: actions: {
async fetchUserPhotos(amount = 6): Promise<void> {
if (userMissing()) {
throw new Error('User is not signed in')
}
// ...
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
notflip
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could use a function, it can be outside the store definition, either in the same file or in another file if you want to use this check across multiple stores (or even in components):
And then in the getters/actions: