import { Dimensions } from "react-native"; import AsyncStorage from "@react-native-async-storage/async-storage"; const { width, height } = Dimensions.get("window"); const initialState = { ltr: false, theme: null, mobile: width < 720, orientation: height > width ? "portrait" : "landscape", homeData: [], offline: false, newProducts: [], levels: [], loading: false, error: null, searchResult: [], arList: [], blogList: [], blog: null, provinces: [], cities: [], province: ["استان"], city: ["شهرستان"], 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: { 0: "پایه تحصیلی", // _0: "پیش دبستان", 1: "اول", 2: "دوم", 3: "سوم", 4: "چهارم", 5: "پنجم", 6: "ششم", 7: "هفتم", 8: "هشتم", 9: "نهم", 10: "دهم", 11: "یازدهم", 12: "دوازدهم", }, }; const grades = [ "پیش دبستان", "اول", "دوم", "سوم", "چهارم", "پنجم", "ششم", "هفتم", "هشتم", "نهم", "دهم", "یازدهم", "دوازدهم", ]; const publicApi = (state = initialState, action) => { let { type, data } = action; switch (type) { case "public/setOffline": return { ...state, offline: data, error: null, loading: false }; case "public/setOrientation": return { ...state, orientation: data, error: null, loading: false }; case "public/setLtr": return { ...state, ltr: true }; case "public/setIsDark": AsyncStorage.setItem("theme", data); return { ...state, location: false, error: null, theme: 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)) ); allCats.sort((a, b) => a - b); 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 !== 1)[0], }; case "public/homePage": let itemsLevel; if (width > 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: width > 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: [...state.province, ...data.map((item) => item.name)], provinces: data, }; case "public/city": return { ...state, loading: false, error: null, city: [...data.map((item) => item.name)], cities: 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; } }; export default publicApi;