import { ostan } from "../../constants/states"; import location from "../../utils/location"; const initialState = { isDark: false, isMobile: window.innerWidth < 1024, homeData: [], newProducts: [], levels: [], loading: false, error: null, searchResult: [], arList: [], blogList: [], blog: null, province: null, city: null, departmans: [ { id: 0, name: "محتوا", }, { id: 0, name: "دیزاین", }, { id: 0, name: "برنامه نویسی", }, ], header: [], selectedMenu: null, contactData: null, selectedVideo: null, bookInfo: null, activeCategory: null, categories: null, bookSection: null, grades: [ { id: 1, title: 'اول' }, { id: 2, title: 'دوم' }, { id: 3, title: 'سوم' }, { id: 4, title: 'چهارم' }, { id: 5, title: 'پنجم' }, { id: 6, title: 'ششم' }, { id: 7, title: 'هفتم' }, { id: 8, title: 'هشتم' }, { id: 9, title: 'نهم' }, { id: 10, title: 'دهم' }, { id: 11, title: 'یازدهم' }, { id: 12, title: 'دوازدهم' }, ], headerOptions: { shown: false, logo: false, hamburgerMenu: false, qr: false, menu: false, back: false, title: false, }, desktopContentTransform: false, }; const grades = [ "پیش دبستان", "اول", "دوم", "سوم", "چهارم", "پنجم", "ششم", "هفتم", "هشتم", "نهم", "دهم", "یازدهم", "دوازدهم", ]; export default function publicApi(state = initialState, action) { let { type, data } = action; switch (type) { case "public/setDesktopContentTransform": return { ...state, desktopContentTransform: data }; case "public/setHeaderOptions": return { ...state, headerOptions: data }; case "public/setIsDark": localStorage.setItem("isDark", data); return { ...state, location: false, error: null, isDark: data }; case "active/video": return { ...state, loading: false, error: null, selectedVideo: data, }; case "active/category": return { ...state, loading: false, activeCategory: state.activeCategory === data ? null : data, error: null, }; case "public/vod/bookSection": const videoId = location.getId(); return { ...state, loading: false, bookSection: data, error: null, selectedVideo: data.vods?.filter((item) => item.id !== videoId)[0], }; case "public/vod/bookCourse": let allCats; allCats = Array.from( new Set(data.vods.map((item) => item.categoryId && item.categoryId)) ); return { ...state, loading: false, bookInfo: { ...data, vods: data.vods.map((item) => { return { ...item, active: false }; }), }, error: null, categories: allCats.map((item) => data.vods.filter((item1) => item1.categoryId === item) ), selectedVideo: data.vods.filter( (item) => item.id !== location.getId() )[0], }; case "public/homePage": let itemsLevel; if (window.innerWidth > 1000) { const jadidTarin = data.filter( (item) => item.title === "جدیدترین کتاب‌های دانوین" ); itemsLevel = jadidTarin[0].data .map((item) => item.book.gradeId * 1) .sort((a, b) => a - b) .map((e) => grades[e]); itemsLevel.unshift("همه"); } return { ...state, loading: false, levels: window.innerWidth > 1000 ? Array.from(new Set(itemsLevel)) : [], error: null, homeData: data, }; case "public/ar": return { ...state, loading: false, error: null, arList: data }; case "public/contact": return { ...state, loading: false, error: null, contactData: data }; case "public/blogList": return { ...state, loading: false, error: null, blogList: data }; case "public/blogInfo": return { ...state, loading: false, error: null, blog: data }; case "public/setSubscribe": return { ...state, loading: false, error: null, selectedSubscribe: data }; case "public/province": return { ...state, loading: false, error: null, province: data }; case "public/city": return { ...state, loading: false, error: null, city: data }; case "public/selectMenu": return { ...state, loading: false, error: null, selectedMenu: data }; case "public/VOD/homePage": return { ...state, loading: false, error: null, header: data }; case "loading": return { ...state, loading: true }; case "error": return { ...state, loading: false, error: data.message }; default: return state; } }