refactor: centralize Accept-Language header via axios interceptor
Add a request interceptor to the axios instance that automatically sets the Accept-Language header from the Zustand language store, ensuring all requests (including POST) send the locale. Remove redundant lang params from service functions and hooks, and refactor B2B form to use the shared service layer instead of raw axios with a hardcoded URL.
This commit is contained in:
parent
2fb73579bc
commit
ecfaff6bbd
|
|
@ -2,12 +2,12 @@ import { AnimatePresence } from "motion/react";
|
|||
import { FC, useMemo } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import axios from "axios";
|
||||
import {
|
||||
defaultValuesOfB2b,
|
||||
createFormSchema,
|
||||
FormType,
|
||||
} from "@/lib/get-b2b-form-details";
|
||||
import { postB2b } from "@/services/service";
|
||||
import { Form } from "@/components/ui/form";
|
||||
import { FormSuccesStatus } from "../form-succes-status";
|
||||
import { Stage1, Stage2, Stage3 } from "../b2b";
|
||||
|
|
@ -96,17 +96,9 @@ export const B2bForm: FC<Props> = ({
|
|||
}
|
||||
});
|
||||
|
||||
const res = await axios.post(
|
||||
"https://turkmentextile.turkmenexpo.com/app/api/v1/form",
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
},
|
||||
}
|
||||
);
|
||||
const success = await postB2b(formData);
|
||||
|
||||
if (res.status === 201) {
|
||||
if (success) {
|
||||
setSuccess(true);
|
||||
}
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import { Container } from "@/components/layout";
|
|||
export const HomeNews: FC = () => {
|
||||
const lang = useLangStore((state) => state.lang);
|
||||
|
||||
const { data, isPending } = useNews(lang);
|
||||
const { data, isPending } = useNews();
|
||||
|
||||
if (isPending) return <Loader />;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export const useContacts = () => {
|
|||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["contacts", lang],
|
||||
queryFn: () => getContacts(lang),
|
||||
queryFn: () => getContacts(),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export const useDesigners = (id: number) => {
|
|||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["designers", lang],
|
||||
queryFn: () => getDesigners(lang, id),
|
||||
queryFn: () => getDesigners(id),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ export const useExhibitionTime = () => {
|
|||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["exhibiton-time", lang],
|
||||
queryFn: () => getExhibitionTime(lang),
|
||||
queryFn: () => getExhibitionTime(),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export const useHomeContacts = () => {
|
|||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["home-contacts", lang],
|
||||
queryFn: () => getHomeContacts(lang),
|
||||
queryFn: () => getHomeContacts(),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ export const useIndustries = () => {
|
|||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["industries", lang],
|
||||
queryFn: () => getIndustries(lang),
|
||||
queryFn: () => getIndustries(),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
import { getNewsInner } from "@/services/service";
|
||||
import { useLangStore } from "@/store/lang";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
export const useNewsInner = (id: number, lang: string) => {
|
||||
export const useNewsInner = (id: number) => {
|
||||
const lang = useLangStore((state) => state.lang);
|
||||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["news-inner", id, lang],
|
||||
queryFn: () => getNewsInner(id, lang),
|
||||
queryFn: () => getNewsInner(id),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
import { getNews } from "@/services/service";
|
||||
import { useLangStore } from "@/store/lang";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
export const useNews = (lang: "ru" | "en" | "tm") => {
|
||||
export const useNews = () => {
|
||||
const lang = useLangStore((state) => state.lang);
|
||||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["news", lang],
|
||||
queryFn: () => getNews(lang),
|
||||
queryFn: () => getNews(),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export const useParticipants = () => {
|
|||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["participants", lang],
|
||||
queryFn: () => getParticipants(lang),
|
||||
queryFn: () => getParticipants(),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ export const usePartners = () => {
|
|||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["partners", lang],
|
||||
queryFn: () => getPartners(lang),
|
||||
queryFn: () => getPartners(),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export const useSponsors = () => {
|
|||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["sponsors", lang],
|
||||
queryFn: () => getSponsors(lang),
|
||||
queryFn: () => getSponsors(),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ export const useStaticWords = (id: string) => {
|
|||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["static-words", lang, id],
|
||||
queryFn: () => getStaticWords(lang, id),
|
||||
queryFn: () => getStaticWords(id),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ export const useStats = () => {
|
|||
|
||||
const { data, isPending } = useQuery({
|
||||
queryKey: ["stats", lang],
|
||||
queryFn: () => getStats(lang),
|
||||
queryFn: () => getStats(),
|
||||
select: ({ data }) => data.data,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ export default function NewsInner() {
|
|||
const pageId = Number(id);
|
||||
useScrollTop(pageId);
|
||||
|
||||
const { data, isPending } = useNewsInner(pageId, lang);
|
||||
const { data: news } = useNews(lang);
|
||||
const { data, isPending } = useNewsInner(pageId);
|
||||
const { data: news } = useNews();
|
||||
|
||||
if (isPending) return <Loader />;
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ export default function News() {
|
|||
|
||||
const lang = useLangStore((state) => state.lang);
|
||||
|
||||
const { data, isPending } = useNews(lang);
|
||||
const { data, isPending } = useNews();
|
||||
|
||||
if (isPending) return <Loader />;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import { SponsorFormType } from "@/lib/get-sponsor-form-details";
|
|||
import { StandFormType } from "@/lib/get-stend-form-details";
|
||||
import axios from "axios";
|
||||
import { ContactsPageType } from "./types/contacts.type";
|
||||
import { LangState } from "@/store/lang";
|
||||
import { StatsType } from "@/hooks/tanstack/use-stats";
|
||||
import { HomeContactsType } from "./types/home-contacts.type";
|
||||
import { StaticType } from "@/hooks/tanstack/use-static-words";
|
||||
|
|
@ -15,11 +14,18 @@ import { ParticipantsType } from "./types/participants.type";
|
|||
import { PhotoTypes } from "./types/photo.type";
|
||||
import { DesignersType } from "./types/designers.type";
|
||||
import { VideoTypes } from "./types/videos.type";
|
||||
import { useLangStore } from "@/store/lang";
|
||||
|
||||
const axios_url = axios.create({
|
||||
baseURL: "https://turkmentextile.turkmenexpo.com/app/api/v1/",
|
||||
});
|
||||
|
||||
axios_url.interceptors.request.use((config) => {
|
||||
const lang = useLangStore.getState().lang;
|
||||
config.headers["Accept-Language"] = lang;
|
||||
return config;
|
||||
});
|
||||
|
||||
export const postStend = async (data: StandFormType): Promise<boolean> => {
|
||||
const res = axios_url.post(`book_stand_form`, data);
|
||||
|
||||
|
|
@ -50,102 +56,62 @@ export const postSubscribe = async (data: { email: string }) => {
|
|||
return (await res).status === 201;
|
||||
};
|
||||
|
||||
export const getStaticWords = async (lang: LangState["lang"], id: string) => {
|
||||
const data = axios_url.get<StaticType>(`pages/${id}`, {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getStaticWords = async (id: string) => {
|
||||
const data = axios_url.get<StaticType>(`pages/${id}`);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getPartners = async (lang: LangState["lang"]) => {
|
||||
const data = axios_url.get<PartnersType>("partners", {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getPartners = async () => {
|
||||
const data = axios_url.get<PartnersType>("partners");
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getStats = async (lang: LangState["lang"]) => {
|
||||
const data = axios_url.get<StatsType>("stats", {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getStats = async () => {
|
||||
const data = axios_url.get<StatsType>("stats");
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getContacts = async (lang: LangState["lang"]) => {
|
||||
const data = axios_url.get<ContactsPageType>("contact_info", {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getContacts = async () => {
|
||||
const data = axios_url.get<ContactsPageType>("contact_info");
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getHomeContacts = async (lang: LangState["lang"]) => {
|
||||
const data = axios_url.get<HomeContactsType>("contact_data", {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getHomeContacts = async () => {
|
||||
const data = axios_url.get<HomeContactsType>("contact_data");
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getIndustries = async (lang: LangState["lang"]) => {
|
||||
const data = axios_url.get<IndustriesType>(`industries`, {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getIndustries = async () => {
|
||||
const data = axios_url.get<IndustriesType>(`industries`);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getExhibitionTime = async (lang: LangState["lang"]) => {
|
||||
const data = axios_url.get<TimeType>(`exhibition_time`, {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getExhibitionTime = async () => {
|
||||
const data = axios_url.get<TimeType>(`exhibition_time`);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getNews = async (lang: "ru" | "en" | "tm") => {
|
||||
const data = axios_url.get<NewsType>(`news?per_page=100`, {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getNews = async () => {
|
||||
const data = axios_url.get<NewsType>(`news?per_page=100`);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getNewsInner = async (id: number, lang: string) => {
|
||||
const data = axios_url.get<NewsInnerType>(`news/${id}`, {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getNewsInner = async (id: number) => {
|
||||
const data = axios_url.get<NewsInnerType>(`news/${id}`);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getParticipants = async (lang: LangState["lang"]) => {
|
||||
const data = axios_url<ParticipantsType>("participants", {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getParticipants = async () => {
|
||||
const data = axios_url<ParticipantsType>("participants");
|
||||
|
||||
return data;
|
||||
};
|
||||
|
|
@ -162,22 +128,14 @@ export const getVideos = async (id: number) => {
|
|||
return data;
|
||||
};
|
||||
|
||||
export const getSponsors = async (lang: LangState["lang"]) => {
|
||||
const data = axios_url<PartnersType>("sponsors_and_partners", {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getSponsors = async () => {
|
||||
const data = axios_url<PartnersType>("sponsors_and_partners");
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getDesigners = async (lang: LangState["lang"], id: number) => {
|
||||
const data = axios_url<DesignersType>("designers/category/" + id, {
|
||||
headers: {
|
||||
"Accept-Language": lang,
|
||||
},
|
||||
});
|
||||
export const getDesigners = async (id: number) => {
|
||||
const data = axios_url<DesignersType>("designers/category/" + id);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue