| 1 |
import { KB_HOST } from '@constants'; |
| 2 |
import useSWRImmutable from 'swr/immutable'; |
| 3 |
|
| 4 |
export const fetcher = (slug) => { |
| 5 |
const lang = window.extSharedData.wpLanguage || null; |
| 6 |
const params = new URLSearchParams({ lang }); |
| 7 |
return fetch(`${KB_HOST}/api/posts/${slug}?${params.toString()}`).then( |
| 8 |
(res) => { |
| 9 |
if (res.status === 404) throw new Error('Not found'); |
| 10 |
if (!res.ok) throw new Error(res.statusText); |
| 11 |
return res.json(); |
| 12 |
}, |
| 13 |
); |
| 14 |
}; |
| 15 |
|
| 16 |
export const useSupportArticle = (slug) => { |
| 17 |
const { data, error } = useSWRImmutable(slug, fetcher); |
| 18 |
return { data, error, loading: !data && !error }; |
| 19 |
}; |
| 20 |
|