Add fetchUrl/externalUrl settings for configured sites.
Showing
1 changed file
with
26 additions
and
5 deletions
... | @@ -7,6 +7,8 @@ import { parseHtml } from './html.ts' | ... | @@ -7,6 +7,8 @@ import { parseHtml } from './html.ts' |
7 | interface SiteConfig { | 7 | interface SiteConfig { |
8 | name: string, | 8 | name: string, |
9 | baseUrl: string, | 9 | baseUrl: string, |
10 | fetchUrl: string, | ||
11 | externalUrl: string, | ||
10 | } | 12 | } |
11 | 13 | ||
12 | export const configSlice = createSlice({ | 14 | export const configSlice = createSlice({ |
... | @@ -15,14 +17,25 @@ export const configSlice = createSlice({ | ... | @@ -15,14 +17,25 @@ export const configSlice = createSlice({ |
15 | sites: {}, | 17 | sites: {}, |
16 | }, | 18 | }, |
17 | reducers: { | 19 | reducers: { |
18 | setSiteConfig(state, { payload: { name, baseUrl } }) { | 20 | setSiteConfig(state, { payload: { name, baseUrl, fetchUrl, externalUrl } }) { |
19 | if (!state.sites[ name ]) state.sites[ name ] = {} | 21 | if (!state.sites[ name ]) state.sites[ name ] = {} |
20 | Object.assign(state.sites[ name ], { baseUrl }) | 22 | Object.assign(state.sites[ name ], { baseUrl, fetchUrl, externalUrl }) |
21 | } | 23 | } |
22 | }, | 24 | }, |
23 | selectors: { | 25 | selectors: { |
24 | getSites: (state) => Object.keys(state.sites).filter(site => site !== 'default'), | 26 | getSites: (state) => Object.keys(state.sites).filter(site => site !== 'default'), |
25 | getSiteBaseUrl: (state, name) => state.sites?.[ name ]?.baseUrl, | 27 | getSiteBaseUrl: (state, name) => { |
28 | const { sites: { [ name ]: siteConfig = {} } = {} } = state | ||
29 | return siteConfig.baseUrl | ||
30 | }, | ||
31 | getSiteFetchUrl: (state, name) => { | ||
32 | const { sites: { [ name ]: siteConfig = {} } = {} } = state | ||
33 | return siteConfig.fetchUrl || siteConfig.baseUrl | ||
34 | }, | ||
35 | getSiteExternalUrl: (state, name) => { | ||
36 | const { sites: { [ name ]: siteConfig = {} } = {} } = state | ||
37 | return siteConfig.externalUrl || siteConfig.baseUrl | ||
38 | }, | ||
26 | }, | 39 | }, |
27 | }) | 40 | }) |
28 | 41 | ||
... | @@ -31,8 +44,8 @@ const baseQuery = fetchBaseQuery() | ... | @@ -31,8 +44,8 @@ const baseQuery = fetchBaseQuery() |
31 | 44 | ||
32 | const siteBaseQuery = async (args, api, options) => { | 45 | const siteBaseQuery = async (args, api, options) => { |
33 | const { site, url } = args | 46 | const { site, url } = args |
34 | const baseUrl = getSiteBaseUrl(site) || '' | 47 | const fetchUrl = getSiteFetchUrl(site) || '' |
35 | return baseQuery({ ...args, url: `${baseUrl}${url}` }, api, options) | 48 | return baseQuery({ ...args, url: `${fetchUrl}${url}` }, api, options) |
36 | } | 49 | } |
37 | 50 | ||
38 | export const sitePageSlice = createApi({ | 51 | export const sitePageSlice = createApi({ |
... | @@ -99,6 +112,14 @@ export const getSiteBaseUrl = (name: string): string => { | ... | @@ -99,6 +112,14 @@ export const getSiteBaseUrl = (name: string): string => { |
99 | return configSlice.selectors.getSiteBaseUrl(store.getState(), name) | 112 | return configSlice.selectors.getSiteBaseUrl(store.getState(), name) |
100 | } | 113 | } |
101 | 114 | ||
115 | export const getSiteFetchUrl = (name: string): string => { | ||
116 | return configSlice.selectors.getSiteFetchUrl(store.getState(), name) | ||
117 | } | ||
118 | |||
119 | export const getSiteExternalUrl = (name: string): string => { | ||
120 | return configSlice.selectors.getSiteExternalUrl(store.getState(), name) | ||
121 | } | ||
122 | |||
102 | export const clearAll = () => { | 123 | export const clearAll = () => { |
103 | return store.dispatch(sitePageSlice.util.invalidateTags([ 'Page' ])) | 124 | return store.dispatch(sitePageSlice.util.invalidateTags([ 'Page' ])) |
104 | } | 125 | } | ... | ... |
-
Please register or sign in to post a comment