-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor!: store config query #862
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,29 @@ | ||
import { computed, ComputedRef, useContext } from '@nuxtjs/composition-api'; | ||
import { StoreConfig } from '~/modules/GraphQL/types'; | ||
import { storeConfigGetters } from '~/getters'; | ||
|
||
import { useConfig } from '~/composables'; | ||
|
||
type UseMagentoConfiguration = () => { | ||
storeConfig: ComputedRef<StoreConfig>; | ||
selectedCurrency: ComputedRef<string | undefined>; | ||
selectedLocale: ComputedRef<string | undefined>; | ||
selectedStore: ComputedRef<string | undefined>; | ||
loadConfiguration: (params: { updateCookies: boolean; updateLocale: boolean; }) => Promise<void>; | ||
}; | ||
|
||
export const useMagentoConfiguration: UseMagentoConfiguration = () => { | ||
const { app: { i18n, $vsf: { $magento: { config } } } } = useContext(); | ||
const { app: { $vsf: { $magento: { config } } } } = useContext(); | ||
|
||
const { | ||
config: storeConfig, | ||
load: loadConfig, | ||
} = useConfig(); | ||
|
||
const selectedCurrency = computed<string | undefined>(() => config.state.getCurrency()); | ||
const selectedLocale = computed<string | undefined>(() => config.state.getLocale()); | ||
const selectedStore = computed<string | undefined>(() => config.state.getStore()); | ||
|
||
const loadConfiguration: (params: { updateCookies: boolean; updateLocale: boolean; }) => Promise<void> = async (params = { | ||
updateCookies: false, | ||
updateLocale: false, | ||
}) => { | ||
const { | ||
updateCookies, | ||
updateLocale, | ||
} = params; | ||
|
||
await loadConfig(); | ||
if (config.state.getStore() || updateCookies) { | ||
config.state.setStore(storeConfigGetters.getCode(storeConfig.value)); | ||
} | ||
|
||
if (!config.state.getLocale() || updateCookies) { | ||
config.state.setLocale(storeConfigGetters.getLocale(storeConfig.value)); | ||
} | ||
|
||
if (!config.state.getCurrency() || updateCookies) { | ||
config.state.setCurrency(storeConfigGetters.getCurrency(storeConfig.value)); | ||
} | ||
|
||
// eslint-disable-next-line promise/always-return | ||
if (updateLocale) { | ||
i18n.setLocale(storeConfigGetters.getLocale(storeConfig.value)); | ||
} | ||
}; | ||
|
||
return { | ||
storeConfig, | ||
selectedCurrency, | ||
selectedLocale, | ||
selectedStore, | ||
loadConfiguration, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { gql } from 'graphql-request'; | ||
|
||
/** GraphQL Query that fetches store configuration from the API */ | ||
export const StoreConfigQuery = gql` | ||
query storeConfig { | ||
storeConfig { | ||
store_code, | ||
default_title, | ||
store_name, | ||
default_display_currency_code, | ||
locale, | ||
header_logo_src, | ||
logo_width, | ||
logo_height, | ||
logo_alt | ||
} | ||
} | ||
`; | ||
|
||
export default StoreConfigQuery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { PiniaPluginContext } from 'pinia'; | ||
import { Plugin } from '@nuxt/types'; | ||
import { ref, set } from '@nuxtjs/composition-api'; | ||
import StoreConfigGql from '~/plugins/query/StoreConfig.gql'; | ||
|
||
const storeConfigPlugin: Plugin = async ({ $pinia, $graphql }) => { | ||
const configData = await $graphql.query.request(StoreConfigGql); | ||
$pinia.use(({ store }: PiniaPluginContext) => { | ||
if (store.$id !== 'magentoConfig') return; | ||
const storeConfig = ref(configData.storeConfig); | ||
|
||
// eslint-disable-next-line no-prototype-builtins | ||
if (!store.$state.hasOwnProperty('storeConfig')) { | ||
set(store.$state, 'storeConfig', storeConfig); | ||
} else { | ||
// eslint-disable-next-line no-param-reassign | ||
store.$state.storeConfig = storeConfig; | ||
} | ||
}); | ||
}; | ||
|
||
export default storeConfigPlugin; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eh we have the annoying eslint rule where it forces us to provide a default value
would it be possible to use like as
PropType<StoreCofnig | null>
? The{}
doesn't really match StoreConfigThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should remove that rule then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, I updated the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you push it out? I can't see the updates