make a new call api after the x-locale change
Hello i'm currently making an app with refine and i have implement i18n. I also have an API which allow me to fetch data in french and in english accord to "x-locale" in REST request header. I would like to know how can i make a new api call without refreshing the page ?
52 Replies
correct-apricotβ’3y ago
Hi @harisris, All the data related hooks returns
refetch
functionality.
for example;
u can call this tableQueryResult.refetch()
itchy-amethystOPβ’3y ago
Hi Alican, thank you for your response !!
I am using a DataGrid component and i use a useDataGrid hook. I do not find any refetch function for this hook. How can I make it work with useDataGrid or does any other solution exist ?
correct-apricotβ’3y ago
it's inside
tableQueryResult
object.
itchy-amethystOPβ’3y ago
Finnaly this does the refetch but the request has the same header. I put the
tableQueryResult.refetch();
inside a useEffect triggered when the locale change and nothing change. Does this functionnality just replay the last request ?correct-apricotβ’3y ago
Yes, It's refetching with current values.
I tested this code snippet. it's work as expected. what is your implementation look like ?
itchy-amethystOPβ’3y ago
i will try your code
correct-apricotβ’3y ago
where do you set headers ?
itchy-amethystOPβ’3y ago
the headers are set in an other component
but the set work fine
when i refresh the page the request is made with the right header
here is the component which change the locale
correct-apricotβ’3y ago
I undestand.
refine uses react-query under the hood. and actually headers out of scope for react-query.
we need to use fetch library with react-query like axios or window.fetch
our examples uses react query with axios. and we have implementation for passing headers with data hooks.
for example:
useDataGrid takes metaData, and passing to react-query and react-query passes to axios.
in this situtation. you need to write this useEffect and metaData to everywhere. actually it's a bad implementation
let me think what we can do. i have couple ideas
https://refine.dev/docs/api-reference/core/hooks/invalidate/useInvalidate/
with this, you can invalidate all queries. you don't need to useEffect and refetch.
I thought you would only use it once.
you can do that after language changed
itchy-amethystOPβ’3y ago
Ok i'm going to try
correct-apricotβ’3y ago
but before do that, we need to set x-locale header
are you using axios with custom data provider?
itchy-amethystOPβ’3y ago
ok ok i try and i get back to you
yes i'm using axios
correct-apricotβ’3y ago
we have couple of options to do that. you can search on google like "how to set dynamically axios headers"
but this is the easiest way for me.
you can get xLocale before each request.
but dont forget. you need to call
invalidate
after modify the localeStorageitchy-amethystOPβ’3y ago
actually the language is set correctly in the localStorage and it change when i change it with the select component
correct-apricotβ’3y ago
nice, but we need to update default axios headers after you change locale
or you can set x-locale header before every request with axios interceptors
itchy-amethystOPβ’3y ago
this is also already done and it works
i'm trying to implement the invalidate functionnality
I tried to put your code snippet in the component where is the select onChange, just after i modify the local but nothing change. The invalidate is not trigger and there is no new API call
correct-apricotβ’3y ago
Hi again, sorry for your trouble.
i quickly check
invalidate()
function in our examples. it's work as espected. can you share latest state of your implementation with me ? i will try to reproduceitchy-amethystOPβ’3y ago
Hi excuse me to bother your since this morning π
correct-apricotβ’3y ago
no no, i'm glad to help if i canπ
itchy-amethystOPβ’3y ago
this in the component which change the language
correct-apricotβ’3y ago
dataProviderName: "dataProvider", can you remove this and try again ?
you have only one data provider isn't it ?
itchy-amethystOPβ’3y ago
yes only one
which i use in App.tsx
correct-apricotβ’3y ago
sorry for misunderstanding. I should have explained better. if you have one. it will be probably work when you delete this
dataProviderName: "dataProvider"
itchy-amethystOPβ’3y ago
this time it trigger and a new api call was emit
but the data grid do not change language
correct-apricotβ’3y ago
on chrome network tab, did you see header with new value on x-locale ?
itchy-amethystOPβ’3y ago
yes
it's set to fr-FR for exampleand do not change
even if the select is in english
but if i reload the page the content is now in english
correct-apricotβ’3y ago
hmm. please let me go over the steps again. i want to understand better
select en, then you see on network tab header is setted to english
select fr-FR, then you see network tab header is setted to fr-FR
but nothing is changed on UI. im i correct?
itchy-amethystOPβ’3y ago
select en, then you see on network tab header is setted to english
select fr-FR, then you see network tab header is setted to english
correct-apricotβ’3y ago
can you try this ?
itchy-amethystOPβ’3y ago
i've try this and this does not work
correct-apricotβ’3y ago
Sorry Haris, i couldn't find to issue.
can you reproduce the issue on stackblitz or can you provide me github repo ? i can try to debug.
itchy-amethystOPβ’3y ago
it is a private repo and a private api with a authentication token i don't if it will work on stackblitz
i'll try
correct-apricotβ’3y ago
please can you share your axios interceptor code with me?
hmm. please let me go over the steps again π
select en, then you see on localStorage i18nextLng="en"
select fr-FR, then you see localStorage i18nextLng="fr-FR"
ofc
i18nextLng
key name can be change by implementationitchy-amethystOPβ’3y ago
select en, then you see on localStorage i18nextLng="en"
select fr-FR, then you see localStorage i18nextLng="en"
correct-apricotβ’3y ago
what is your i18n provider ?
itchy-amethystOPβ’3y ago
correct-apricotβ’3y ago
sorry i was thinkink your i18n provider is using local storage for saving language. but probably using something else.
because this is stays same
according to this : https://github.com/i18next/i18next-browser-languageDetector#detector-options
it uses query string
btw. this probably will work. but it's not a best practive.
with someway you need to pass current language value to axios.
axiosInstance.interceptors.request.use(config => {
config.headers.put['x-locale'] = <current-value-from-i18n-provider>
return config;
});
itchy-amethystOPβ’3y ago
currently this is how i set the
x-locale
in axios header
correct-apricotβ’3y ago
this is the default header. this will run once when axios initalized
you need this too
but if localStorage stays same. this will not work
itchy-amethystOPβ’3y ago
ok that is why the localStorage not change
correct-apricotβ’3y ago
why
itchy-amethystOPβ’3y ago
sorry it was a question
and localStorage do not change event if i put your code snippet in the selet' onChange()
correct-apricotβ’3y ago
sometimes chrome not refresh automatically
you need to press this

itchy-amethystOPβ’3y ago
sorry not the localStorage but the x-locale in the request header
the localStorage work fine
correct-apricotβ’3y ago
Harris im so sorry.
okay, i cloned this https://refine.dev/docs/api-reference/core/providers/i18n-provider/#example-1 and try to implement.
i only add this and it works !!!
can you change your previous interceptors with above please π
itchy-amethystOPβ’3y ago
It finnaly works !!!
correct-apricotβ’3y ago
YESSS
im so sorry. i share wrong code with you
probably axios api changed
thank you for time π¦
itchy-amethystOPβ’3y ago
Your a boss πͺ
correct-apricotβ’3y ago
you are the boss π
itchy-amethystOPβ’3y ago
thanks YOU for your time
sorry that i have bother your since this morning
have a nice day
correct-apricotβ’3y ago
no no. im glad we solve the problem ! please don't be hesitate to ask questions π
have a nice day too
itchy-amethystOPβ’3y ago
thank you π