fix: webview services

This commit is contained in:
azizziy
2026-03-02 09:49:02 +05:00
parent a0e5582fc7
commit bdc205b538
2 changed files with 160 additions and 253 deletions

View File

@@ -1,42 +1,54 @@
import Express_diagnistika from '@/assets/images/Express_diagnistika.png'; import Express_diagnistika from "@/assets/images/Express_diagnistika.png";
import { useTheme } from '@/components/ThemeContext'; import { useTheme } from "@/components/ThemeContext";
import { useQuery, useQueryClient } from '@tanstack/react-query'; import { useQuery, useQueryClient } from "@tanstack/react-query";
import { Image } from 'expo-image'; import { Image } from "expo-image";
import { router } from 'expo-router'; import { router } from "expo-router";
import { ChevronLeft, XIcon } from 'lucide-react-native'; import { useState } from "react";
import React, { useRef, useState } from 'react'; import {
import { ActivityIndicator, FlatList, Modal, Text, TouchableOpacity, View } from 'react-native'; ActivityIndicator,
import { RefreshControl } from 'react-native-gesture-handler'; FlatList,
import { SafeAreaView } from 'react-native-safe-area-context'; Text,
import { WebView } from 'react-native-webview'; ToastAndroid,
import { eservices_api } from '../lib/api'; TouchableOpacity,
View,
} from "react-native";
import { RefreshControl } from "react-native-gesture-handler";
import * as WebBrowser from "expo-web-browser";
import { eservices_api } from "../lib/api";
import { useTranslation } from "react-i18next";
const dark = { const dark = {
bg: '#0f172a', bg: "#0f172a",
card: '#334155', card: "#334155",
border: '#1e293b', border: "#1e293b",
muted: '#334155', muted: "#334155",
text: '#E5B037', text: "#E5B037",
subText: '#0B0F2C', subText: "#0B0F2C",
}; };
export default function EServicesCategoryScreen() { export default function EServicesCategoryScreen() {
const { isDark } = useTheme(); const { isDark } = useTheme();
const [modalVisible, setModalVisible] = useState(false);
const webviewRef = useRef<WebView>(null);
const [webUrl, setWebUrl] = React.useState<string | null>(null);
const [refreshing, setRefreshing] = useState(false); const [refreshing, setRefreshing] = useState(false);
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { t } = useTranslation();
const { data, isLoading } = useQuery({ const { data, isLoading } = useQuery({
queryKey: ['goverment_category'], queryKey: ["goverment_category"],
queryFn: () => eservices_api.category(), queryFn: () => eservices_api.category(),
}); });
const handleOpenBrowser = async (fileUrl: string) => {
try {
await WebBrowser.openBrowserAsync(fileUrl);
} catch (error) {
ToastAndroid.show(t("Xatolik yuz berdi"), ToastAndroid.TOP);
}
};
const onRefresh = async () => { const onRefresh = async () => {
setRefreshing(true); setRefreshing(true);
try { try {
await queryClient.refetchQueries({ queryKey: ['goverment_category'] }); await queryClient.refetchQueries({ queryKey: ["goverment_category"] });
} finally { } finally {
setRefreshing(false); setRefreshing(false);
} }
@@ -52,22 +64,21 @@ export default function EServicesCategoryScreen() {
const staticCategory = { const staticCategory = {
id: 0, id: 0,
name: 'Express Diagnostika', name: "Express Diagnostika",
image: Express_diagnistika, image: Express_diagnistika,
url: 'https://myorg.uz/ru', url: "https://myorg.uz/ru",
}; };
const categories = [staticCategory, ...(data?.data?.data?.results ?? [])]; const categories = [staticCategory, ...(data?.data?.data?.results ?? [])];
const handlePress = (item: any) => { const handlePress = (item: any) => {
if (item.id === 0 && item.url) { if (item.id === 0 && item.url) {
setWebUrl(item.url); handleOpenBrowser(item.url);
setModalVisible(true);
return; return;
} }
router.push({ router.push({
pathname: '/(dashboard)/e-service/e-services-category', pathname: "/(dashboard)/e-service/e-services-category",
params: { params: {
categoryId: item.id, categoryId: item.id,
categoryName: item.name, categoryName: item.name,
@@ -79,7 +90,7 @@ export default function EServicesCategoryScreen() {
<View <View
style={{ style={{
flex: 1, flex: 1,
backgroundColor: isDark ? dark.bg : '#f8fafc', backgroundColor: isDark ? dark.bg : "#f8fafc",
padding: 16, padding: 16,
}} }}
> >
@@ -91,8 +102,8 @@ export default function EServicesCategoryScreen() {
<RefreshControl <RefreshControl
refreshing={refreshing} refreshing={refreshing}
onRefresh={onRefresh} onRefresh={onRefresh}
tintColor={isDark ? '#f8fafc' : '#020617'} tintColor={isDark ? "#f8fafc" : "#020617"}
colors={['#3b82f6']} colors={["#3b82f6"]}
/> />
} }
renderItem={({ item }) => ( renderItem={({ item }) => (
@@ -101,13 +112,13 @@ export default function EServicesCategoryScreen() {
onPress={() => handlePress(item)} onPress={() => handlePress(item)}
style={{ style={{
marginHorizontal: 1, marginHorizontal: 1,
backgroundColor: isDark ? '#FDFDFD' : '#ffffff', backgroundColor: isDark ? "#FDFDFD" : "#ffffff",
borderRadius: 16, borderRadius: 16,
flexDirection: 'row', flexDirection: "row",
alignItems: 'center', alignItems: "center",
borderWidth: isDark ? 1 : 0, borderWidth: isDark ? 1 : 0,
borderColor: isDark ? dark.border : 'transparent', borderColor: isDark ? dark.border : "transparent",
shadowColor: '#000', shadowColor: "#000",
shadowOffset: { width: 0, height: 2 }, shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.15, shadowOpacity: 0.15,
shadowRadius: 4, shadowRadius: 4,
@@ -116,98 +127,31 @@ export default function EServicesCategoryScreen() {
> >
<View <View
style={{ style={{
width: '100%', width: "100%",
height: 100, height: 100,
borderRadius: 12, borderRadius: 12,
alignItems: 'center', alignItems: "center",
justifyContent: 'center', justifyContent: "center",
marginRight: 14, marginRight: 14,
padding: 2, padding: 2,
overflow: 'hidden', overflow: "hidden",
}} }}
> >
{item.image ? ( {item.image ? (
<Image <Image
source={item.image} source={item.image}
style={{ width: '100%', height: '100%' }} style={{ width: "100%", height: "100%" }}
contentFit="contain" contentFit="contain"
/> />
) : ( ) : (
<Text style={{ fontWeight: '700', color: dark.text }}>{item.name[0]}</Text> <Text style={{ fontWeight: "700", color: dark.text }}>
{item.name[0]}
</Text>
)} )}
</View> </View>
</TouchableOpacity> </TouchableOpacity>
)} )}
/> />
<Modal visible={modalVisible} animationType="slide">
<SafeAreaView
style={{
flex: 1,
backgroundColor: isDark ? '#0f172a' : '#f8fafc',
}}
>
{/* WebView Header */}
<View
style={{
flexDirection: 'row',
paddingHorizontal: 16,
paddingVertical: 12,
backgroundColor: isDark ? '#1e293b' : '#f8fafc',
alignItems: 'center',
justifyContent: 'space-between',
borderBottomWidth: 1,
borderBottomColor: isDark ? '#334155' : '#e2e8f0',
}}
>
<TouchableOpacity
onPress={() => webviewRef.current?.goBack()}
hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
>
<ChevronLeft size={28} color={isDark ? '#f1f5f9' : '#0f172a'} />
</TouchableOpacity>
<Text
style={{
flex: 1,
textAlign: 'center',
fontSize: 16,
fontWeight: '600',
color: isDark ? '#f1f5f9' : '#0f172a',
}}
numberOfLines={1}
>
{webUrl}
</Text>
<TouchableOpacity
onPress={() => setModalVisible(false)}
hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
>
<XIcon size={28} color={isDark ? '#f1f5f9' : '#0f172a'} />
</TouchableOpacity>
</View>
{/* WebView */}
{webUrl && (
<WebView
ref={webviewRef}
originWhitelist={['*']}
javaScriptEnabled
domStorageEnabled
onShouldStartLoadWithRequest={(request) => {
// iOS va Android uchun barcha URLlarni WebView ichida ochish
return true;
}}
source={{ uri: webUrl }}
style={{ flex: 1, backgroundColor: isDark ? '#0f172a' : '#f8fafc' }}
startInLoadingState
renderLoading={() => (
<ActivityIndicator color="#3b82f6" size="large" style={{ flex: 1 }} />
)}
/>
)}
</SafeAreaView>
</Modal>
</View> </View>
); );
} }

View File

@@ -1,28 +1,26 @@
// EServicesScreen.tsx import { useTheme } from "@/components/ThemeContext";
import { useTheme } from '@/components/ThemeContext'; import { useInfiniteQuery, useQueryClient } from "@tanstack/react-query";
import { useInfiniteQuery, useQueryClient } from '@tanstack/react-query'; import { Image } from "expo-image";
import { Image } from 'expo-image'; import { router, useLocalSearchParams } from "expo-router";
import { router, useLocalSearchParams } from 'expo-router'; import { ChevronLeft } from "lucide-react-native";
import { ChevronLeft, XIcon } from 'lucide-react-native'; import { useCallback, useState } from "react";
import React, { useCallback, useRef, useState } from 'react';
import { import {
ActivityIndicator, ActivityIndicator,
Dimensions, Dimensions,
FlatList, FlatList,
Modal,
StyleSheet, StyleSheet,
Text, Text,
ToastAndroid,
TouchableOpacity, TouchableOpacity,
View, View,
} from 'react-native'; } from "react-native";
import { SafeAreaView } from 'react-native-safe-area-context'; import * as WebBrowser from "expo-web-browser";
import { WebView } from 'react-native-webview';
import { useTranslation } from 'react-i18next'; import { useTranslation } from "react-i18next";
import { RefreshControl } from 'react-native-gesture-handler'; import { RefreshControl } from "react-native-gesture-handler";
import { eservices_api } from '../lib/api'; import { eservices_api } from "../lib/api";
const { width: SCREEN_WIDTH } = Dimensions.get('window'); const { width: SCREEN_WIDTH } = Dimensions.get("window");
const PAGE_SIZE = 10; const PAGE_SIZE = 10;
export interface GovermentServiceDataRes { export interface GovermentServiceDataRes {
@@ -34,78 +32,100 @@ export interface GovermentServiceDataRes {
export default function EServicesScreen() { export default function EServicesScreen() {
const { isDark } = useTheme(); const { isDark } = useTheme();
const [webUrl, setWebUrl] = useState<string | null>(null);
const [modalVisible, setModalVisible] = useState(false);
const webviewRef = useRef<WebView>(null);
const params = useLocalSearchParams(); const params = useLocalSearchParams();
const { t } = useTranslation(); const { t } = useTranslation();
const [refreshing, setRefreshing] = useState(false); const [refreshing, setRefreshing] = useState(false);
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { data, isLoading, isError, fetchNextPage, hasNextPage, isFetchingNextPage } = const {
useInfiniteQuery({ data,
queryKey: ['goverment_service', params.categoryId], isLoading,
queryFn: async ({ pageParam = 1 }) => { isError,
const response = await eservices_api.list({ fetchNextPage,
page: pageParam, hasNextPage,
page_size: PAGE_SIZE, isFetchingNextPage,
category: Number(params.categoryId), } = useInfiniteQuery({
}); queryKey: ["goverment_service", params.categoryId],
return response.data.data; queryFn: async ({ pageParam = 1 }) => {
}, const response = await eservices_api.list({
getNextPageParam: (lastPage) => page: pageParam,
lastPage.current_page < lastPage.total_pages ? lastPage.current_page + 1 : undefined, page_size: PAGE_SIZE,
initialPageParam: 1, category: Number(params.categoryId),
}); });
return response.data.data;
},
getNextPageParam: (lastPage) =>
lastPage.current_page < lastPage.total_pages
? lastPage.current_page + 1
: undefined,
initialPageParam: 1,
});
const services: GovermentServiceDataRes[] = data?.pages.flatMap((p) => p.results) ?? []; const services: GovermentServiceDataRes[] =
data?.pages.flatMap((p) => p.results) ?? [];
const onRefresh = async () => { const onRefresh = async () => {
setRefreshing(true); setRefreshing(true);
try { try {
await queryClient.refetchQueries({ queryKey: ['goverment_service'] }); await queryClient.refetchQueries({ queryKey: ["goverment_service"] });
} finally { } finally {
setRefreshing(false); setRefreshing(false);
} }
}; };
const openWebView = (url: string) => { const handleOpenBrowser = async (fileUrl: string) => {
setWebUrl(url); ToastAndroid.show(t("Xatolik yuz berdi"), ToastAndroid.TOP);
setModalVisible(true); try {
await WebBrowser.openBrowserAsync(fileUrl);
} catch (error) {
ToastAndroid.show(t("Xatolik yuz berdi"), ToastAndroid.TOP);
}
}; };
const renderItem = useCallback( const renderItem = useCallback(
({ item }: { item: GovermentServiceDataRes }) => ( ({ item }: { item: GovermentServiceDataRes }) => (
<View style={{ alignItems: 'center', marginTop: 12, width: CARD_WIDTH }}> <View style={{ alignItems: "center", marginTop: 12, width: CARD_WIDTH }}>
{/* Logo (bosilganda WebView ochiladi) */}
<TouchableOpacity <TouchableOpacity
style={[ style={[
styles.card, styles.card,
{ backgroundColor: isDark ? '#1e293b' : '#f8fafc' }, { backgroundColor: isDark ? "#1e293b" : "#f8fafc" },
isDark ? styles.darkShadow : styles.lightShadow, isDark ? styles.darkShadow : styles.lightShadow,
]} ]}
onPress={() => openWebView(item.url)} onPress={() => handleOpenBrowser(item.url)}
> >
<Image source={{ uri: item.logo }} style={styles.logo} resizeMode="contain" /> <Image
source={{ uri: item.logo }}
style={styles.logo}
resizeMode="contain"
/>
</TouchableOpacity> </TouchableOpacity>
{/* Name (alogifa, faqat korsatish) */} {/* Name (alogifa, faqat korsatish) */}
<Text <Text
style={[ style={[
styles.name, styles.name,
{ color: isDark ? '#f1f5f9' : '#0f172a', marginTop: 4, paddingHorizontal: 5 }, {
color: isDark ? "#f1f5f9" : "#0f172a",
marginTop: 4,
paddingHorizontal: 5,
},
]} ]}
> >
{item.name} {item.name}
</Text> </Text>
</View> </View>
), ),
[isDark] [isDark],
); );
if (isLoading) { if (isLoading) {
return ( return (
<View style={[styles.center, { backgroundColor: isDark ? '#0f172a' : '#f8fafc' }]}> <View
style={[
styles.center,
{ backgroundColor: isDark ? "#0f172a" : "#f8fafc" },
]}
>
<ActivityIndicator size="large" color="#3b82f6" /> <ActivityIndicator size="large" color="#3b82f6" />
</View> </View>
); );
@@ -113,40 +133,45 @@ export default function EServicesScreen() {
if (isError) { if (isError) {
return ( return (
<View style={[styles.center, { backgroundColor: isDark ? '#0f172a' : '#f8fafc' }]}> <View
<Text style={{ color: 'red' }}>Xatolik yuz berdi</Text> style={[
styles.center,
{ backgroundColor: isDark ? "#0f172a" : "#f8fafc" },
]}
>
<Text style={{ color: "red" }}>Xatolik yuz berdi</Text>
</View> </View>
); );
} }
return ( return (
<View style={{ flex: 1, backgroundColor: isDark ? '#0f172a' : '#f8fafc' }}> <View style={{ flex: 1, backgroundColor: isDark ? "#0f172a" : "#f8fafc" }}>
{/* Header */} {/* Header */}
<View <View
style={{ style={{
flexDirection: 'row', flexDirection: "row",
alignItems: 'center', alignItems: "center",
paddingHorizontal: 16, paddingHorizontal: 16,
gap: 10, gap: 10,
paddingVertical: 12, paddingVertical: 12,
backgroundColor: isDark ? '#1e293b' : '#f8fafc', backgroundColor: isDark ? "#1e293b" : "#f8fafc",
borderBottomWidth: 1, borderBottomWidth: 1,
borderBottomColor: isDark ? '#334155' : '#e2e8f0', borderBottomColor: isDark ? "#334155" : "#e2e8f0",
}} }}
> >
<TouchableOpacity <TouchableOpacity
onPress={() => router.back()} onPress={() => router.back()}
hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }} hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
> >
<ChevronLeft size={28} color={isDark ? '#f1f5f9' : '#0f172a'} /> <ChevronLeft size={28} color={isDark ? "#f1f5f9" : "#0f172a"} />
</TouchableOpacity> </TouchableOpacity>
<Text <Text
style={{ style={{
flex: 1, flex: 1,
fontSize: 18, fontSize: 18,
fontWeight: '600', fontWeight: "600",
color: isDark ? '#f1f5f9' : '#0f172a', color: isDark ? "#f1f5f9" : "#0f172a",
}} }}
> >
{params.categoryName} {params.categoryName}
@@ -158,18 +183,18 @@ export default function EServicesScreen() {
<View style={[styles.center, { flex: 1, padding: 16, gap: 5 }]}> <View style={[styles.center, { flex: 1, padding: 16, gap: 5 }]}>
<Text <Text
style={{ style={{
color: isDark ? '#f1f5f9' : '#0f172a', color: isDark ? "#f1f5f9" : "#0f172a",
fontSize: 18, fontSize: 18,
textAlign: 'center', textAlign: "center",
}} }}
> >
{t("Bu kategoriya bo'yicha xizmat topilmadi")} {t("Bu kategoriya bo'yicha xizmat topilmadi")}
</Text> </Text>
<Text <Text
style={{ style={{
color: isDark ? '#f1f5f9' : '#0f172a', color: isDark ? "#f1f5f9" : "#0f172a",
fontSize: 16, fontSize: 16,
textAlign: 'center', textAlign: "center",
}} }}
> >
{t("Tez orada xizmat qo'shiladi")} {t("Tez orada xizmat qo'shiladi")}
@@ -182,96 +207,34 @@ export default function EServicesScreen() {
<RefreshControl <RefreshControl
refreshing={refreshing} refreshing={refreshing}
onRefresh={onRefresh} onRefresh={onRefresh}
tintColor={isDark ? '#f8fafc' : '#020617'} tintColor={isDark ? "#f8fafc" : "#020617"}
colors={['#3b82f6']} colors={["#3b82f6"]}
/> />
} }
keyExtractor={(item) => item.id.toString()} keyExtractor={(item) => item.id.toString()}
renderItem={renderItem} renderItem={renderItem}
numColumns={3} numColumns={3}
columnWrapperStyle={{ justifyContent: 'space-between', marginBottom: 12 }} columnWrapperStyle={{
justifyContent: "space-between",
marginBottom: 12,
}}
contentContainerStyle={{ padding: 16, paddingBottom: 80 }} contentContainerStyle={{ padding: 16, paddingBottom: 80 }}
onEndReached={() => hasNextPage && !isFetchingNextPage && fetchNextPage()} onEndReached={() =>
hasNextPage && !isFetchingNextPage && fetchNextPage()
}
onEndReachedThreshold={0.4} onEndReachedThreshold={0.4}
ListFooterComponent={ ListFooterComponent={
isFetchingNextPage ? ( isFetchingNextPage ? (
<ActivityIndicator color="#3b82f6" size="large" style={{ marginVertical: 20 }} /> <ActivityIndicator
color="#3b82f6"
size="large"
style={{ marginVertical: 20 }}
/>
) : null ) : null
} }
showsVerticalScrollIndicator={false} showsVerticalScrollIndicator={false}
/> />
)} )}
{/* WebView Modal */}
<Modal visible={modalVisible} animationType="slide">
<SafeAreaView
style={{
flex: 1,
backgroundColor: isDark ? '#0f172a' : '#f8fafc',
}}
>
{/* WebView Header */}
<View
style={{
flexDirection: 'row',
paddingHorizontal: 16,
paddingVertical: 12,
backgroundColor: isDark ? '#1e293b' : '#f8fafc',
alignItems: 'center',
justifyContent: 'space-between',
borderBottomWidth: 1,
borderBottomColor: isDark ? '#334155' : '#e2e8f0',
}}
>
<TouchableOpacity
onPress={() => webviewRef.current?.goBack()}
hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
>
<ChevronLeft size={28} color={isDark ? '#f1f5f9' : '#0f172a'} />
</TouchableOpacity>
<Text
style={{
flex: 1,
textAlign: 'center',
fontSize: 16,
fontWeight: '600',
color: isDark ? '#f1f5f9' : '#0f172a',
}}
numberOfLines={1}
>
{webUrl}
</Text>
<TouchableOpacity
onPress={() => setModalVisible(false)}
hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}
>
<XIcon size={28} color={isDark ? '#f1f5f9' : '#0f172a'} />
</TouchableOpacity>
</View>
{/* WebView */}
{webUrl && (
<WebView
ref={webviewRef}
originWhitelist={['*']}
javaScriptEnabled
domStorageEnabled
onShouldStartLoadWithRequest={(request) => {
// iOS va Android uchun barcha URLlarni WebView ichida ochish
return true;
}}
source={{ uri: webUrl }}
style={{ flex: 1, backgroundColor: isDark ? '#0f172a' : '#f8fafc' }}
startInLoadingState
renderLoading={() => (
<ActivityIndicator color="#3b82f6" size="large" style={{ flex: 1 }} />
)}
/>
)}
</SafeAreaView>
</Modal>
</View> </View>
); );
} }
@@ -279,12 +242,12 @@ export default function EServicesScreen() {
const CARD_WIDTH = (SCREEN_WIDTH - 50) / 3; const CARD_WIDTH = (SCREEN_WIDTH - 50) / 3;
const styles = StyleSheet.create({ const styles = StyleSheet.create({
center: { flex: 1, justifyContent: 'center', alignItems: 'center' }, center: { flex: 1, justifyContent: "center", alignItems: "center" },
card: { card: {
width: CARD_WIDTH, width: CARD_WIDTH,
borderRadius: 12, borderRadius: 12,
padding: 12, padding: 12,
alignItems: 'center', alignItems: "center",
}, },
logo: { logo: {
width: 80, width: 80,
@@ -293,18 +256,18 @@ const styles = StyleSheet.create({
}, },
name: { name: {
fontSize: 14, fontSize: 14,
fontWeight: '600', fontWeight: "600",
textAlign: 'center', textAlign: "center",
}, },
darkShadow: { darkShadow: {
shadowColor: '#000', shadowColor: "#000",
shadowOffset: { width: 0, height: 2 }, shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.4, shadowOpacity: 0.4,
shadowRadius: 6, shadowRadius: 6,
elevation: 3, elevation: 3,
}, },
lightShadow: { lightShadow: {
shadowColor: '#000', shadowColor: "#000",
shadowOffset: { width: 0, height: 2 }, shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1, shadowOpacity: 0.1,
shadowRadius: 4, shadowRadius: 4,