language store created
This commit is contained in:
parent
76cca13882
commit
7f5c6ee9a6
|
|
@ -1,7 +1,7 @@
|
||||||
// Types
|
// Types
|
||||||
import { ActiveLinkActionType } from "../types/store.types";
|
import { ActiveLinkActionType } from "../types/store.types";
|
||||||
|
|
||||||
export const setActiveLink = (active: number) => ({
|
export const setActiveLink = (active: number): ActiveLinkActionType => ({
|
||||||
type: "SET_ACTIVE_LINK",
|
type: "SET_ACTIVE_LINK",
|
||||||
payload: active,
|
payload: active,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
// Types
|
||||||
|
import { ILanguage, ILanguageAction } from "../types/store.types";
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
import setSavedLanguage from "../helpers/setSavedLanguage";
|
||||||
|
|
||||||
|
export const setLanguage = (title: ILanguage["title"]): ILanguageAction => ({
|
||||||
|
type: "SET_LANGUAGE",
|
||||||
|
payload: setSavedLanguage(title),
|
||||||
|
});
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
import { motion } from "framer-motion";
|
import { motion } from "framer-motion";
|
||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
|
|
||||||
// Icons
|
// Icons
|
||||||
import { ReactComponent as ArrowDownBlack } from "../../assets/icons/arrow-down-black.svg";
|
import { ReactComponent as ArrowDownBlack } from "../../assets/icons/arrow-down-black.svg";
|
||||||
|
|
@ -9,9 +10,11 @@ import { ReactComponent as ArrowDownBlack } from "../../assets/icons/arrow-down-
|
||||||
// Animations
|
// Animations
|
||||||
import { languageMotion } from "../../animations/language.animation";
|
import { languageMotion } from "../../animations/language.animation";
|
||||||
|
|
||||||
interface ILanguage {
|
// Types
|
||||||
title: "RU" | "EN" | "TM";
|
import { ILanguage, RootState } from "../../types/store.types";
|
||||||
}
|
|
||||||
|
// Actions
|
||||||
|
import { setLanguage } from "../../actions/setLanguage";
|
||||||
|
|
||||||
const languages: ILanguage[] = [
|
const languages: ILanguage[] = [
|
||||||
{
|
{
|
||||||
|
|
@ -27,9 +30,18 @@ const languages: ILanguage[] = [
|
||||||
|
|
||||||
const LanguageSelect = () => {
|
const LanguageSelect = () => {
|
||||||
const [dropdown, setDropdown] = useState<boolean>(false);
|
const [dropdown, setDropdown] = useState<boolean>(false);
|
||||||
|
|
||||||
|
const activeLanguage = useSelector<RootState, RootState["language"]["title"]>(
|
||||||
|
(state) => state.language.title
|
||||||
|
);
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
|
const onLanguageClick = (title: ILanguage["title"]) => {
|
||||||
|
dispatch(setLanguage(title));
|
||||||
|
};
|
||||||
return (
|
return (
|
||||||
<div className="language" onClick={() => setDropdown(!dropdown)}>
|
<div className="language" onClick={() => setDropdown(!dropdown)}>
|
||||||
<span>EN</span>
|
<span>{activeLanguage}</span>
|
||||||
<ArrowDownBlack />
|
<ArrowDownBlack />
|
||||||
<motion.ul
|
<motion.ul
|
||||||
className="language-dropdown"
|
className="language-dropdown"
|
||||||
|
|
@ -50,6 +62,7 @@ const LanguageSelect = () => {
|
||||||
background: "#f1f1f1",
|
background: "#f1f1f1",
|
||||||
type: "spring",
|
type: "spring",
|
||||||
}}
|
}}
|
||||||
|
onClick={() => onLanguageClick(language.title)}
|
||||||
>
|
>
|
||||||
{language.title}
|
{language.title}
|
||||||
</motion.button>
|
</motion.button>
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import { motion } from "framer-motion";
|
||||||
import { useDispatch, useSelector } from "react-redux";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
import { ActiveLinkType } from "../../types/store.types";
|
import { ActiveLinkType, RootState } from "../../types/store.types";
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
import { setActiveLink } from "../../actions/setActiveLink.action";
|
import { setActiveLink } from "../../actions/setActiveLink.action";
|
||||||
|
|
@ -61,8 +61,8 @@ const subNavData: subNavDataType[] = [
|
||||||
];
|
];
|
||||||
|
|
||||||
const SubNav = () => {
|
const SubNav = () => {
|
||||||
const activeLink = useSelector<ActiveLinkType, ActiveLinkType["active"]>(
|
const activeLink = useSelector<RootState, RootState["activeLink"]["active"]>(
|
||||||
(state) => state.active
|
(state) => state.activeLink.active
|
||||||
);
|
);
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { ILanguage } from "../types/store.types";
|
||||||
|
|
||||||
|
export default (): ILanguage["title"] => {
|
||||||
|
// @ts-ignore
|
||||||
|
return localStorage.getItem("savedLanguage") || ("TM" as ILanguage["title"]);
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { ILanguage } from "../types/store.types";
|
||||||
|
|
||||||
|
export default (title: ILanguage["title"]): ILanguage["title"] => {
|
||||||
|
localStorage.setItem("savedLanguage", title);
|
||||||
|
return title;
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
// Types
|
||||||
|
import { ILanguage, ILanguageAction } from "../types/store.types";
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
import getSavedLanguage from "../helpers/getSavedLanguage";
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
title: getSavedLanguage(),
|
||||||
|
};
|
||||||
|
|
||||||
|
export const languageReducer = (
|
||||||
|
state: ILanguage = initialState,
|
||||||
|
action: ILanguageAction
|
||||||
|
) => {
|
||||||
|
switch (action.type) {
|
||||||
|
case "SET_LANGUAGE": {
|
||||||
|
return { ...state, title: action.payload };
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -1,9 +1,15 @@
|
||||||
// Modules
|
// Modules
|
||||||
import { configureStore } from "@reduxjs/toolkit";
|
import { combineReducers, configureStore } from "@reduxjs/toolkit";
|
||||||
|
|
||||||
// Reducers
|
// Reducers
|
||||||
import { activeLinkReducer } from "../reducers/activeLink.reducer";
|
import { activeLinkReducer } from "../reducers/activeLink.reducer";
|
||||||
|
import { languageReducer } from "../reducers/language.reducer";
|
||||||
|
|
||||||
|
export const allReducers = combineReducers({
|
||||||
|
activeLink: activeLinkReducer,
|
||||||
|
language: languageReducer,
|
||||||
|
});
|
||||||
|
|
||||||
export const functionalityStore = configureStore({
|
export const functionalityStore = configureStore({
|
||||||
reducer: activeLinkReducer,
|
reducer: allReducers,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,9 @@
|
||||||
|
// Store
|
||||||
|
// import { functionalityStore } from "../store/functionality";
|
||||||
|
|
||||||
|
// Reducers
|
||||||
|
import { allReducers } from "../store/functionality";
|
||||||
|
|
||||||
export interface ActiveLinkType {
|
export interface ActiveLinkType {
|
||||||
active: number;
|
active: number;
|
||||||
}
|
}
|
||||||
|
|
@ -5,3 +11,14 @@ export interface ActiveLinkActionType {
|
||||||
type: "SET_ACTIVE_LINK";
|
type: "SET_ACTIVE_LINK";
|
||||||
payload: number;
|
payload: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ILanguage {
|
||||||
|
title: "RU" | "EN" | "TM";
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ILanguageAction {
|
||||||
|
type: "SET_LANGUAGE";
|
||||||
|
payload: "RU" | "EN" | "TM";
|
||||||
|
}
|
||||||
|
|
||||||
|
export type RootState = ReturnType<typeof allReducers>;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue