upd: realtime db firebase
Deskripsi: - percobaan realtime database firebase pada diskusi umum No Issues
This commit is contained in:
@@ -11,6 +11,7 @@ import Styles from "@/constants/Styles";
|
|||||||
import { apiGetDiscussionGeneralOne, apiSendDiscussionGeneralCommentar } from "@/lib/api";
|
import { apiGetDiscussionGeneralOne, apiSendDiscussionGeneralCommentar } from "@/lib/api";
|
||||||
import { useAuthSession } from "@/providers/AuthProvider";
|
import { useAuthSession } from "@/providers/AuthProvider";
|
||||||
import { Ionicons, MaterialIcons } from "@expo/vector-icons";
|
import { Ionicons, MaterialIcons } from "@expo/vector-icons";
|
||||||
|
import { firebase } from '@react-native-firebase/database';
|
||||||
import { router, Stack, useLocalSearchParams } from "expo-router";
|
import { router, Stack, useLocalSearchParams } from "expo-router";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { Pressable, ScrollView, Text, View } from "react-native";
|
import { Pressable, ScrollView, Text, View } from "react-native";
|
||||||
@@ -46,6 +47,26 @@ export default function DetailDiscussionGeneral() {
|
|||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const [loadingKomentar, setLoadingKomentar] = useState(true)
|
const [loadingKomentar, setLoadingKomentar] = useState(true)
|
||||||
const arrSkeleton = Array.from({ length: 3 }, (_, index) => index)
|
const arrSkeleton = Array.from({ length: 3 }, (_, index) => index)
|
||||||
|
const reference = firebase.app().database('https://mobile-darmasaba-default-rtdb.asia-southeast1.firebasedatabase.app').ref(`/discussion-general/${id}`);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onValueChange = reference.on('value', snapshot => {
|
||||||
|
if (snapshot.val() == null) {
|
||||||
|
reference.set({ trigger: true })
|
||||||
|
}
|
||||||
|
handleLoad('komentar', false)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Stop listening for updates when no longer required
|
||||||
|
return () => reference.off('value', onValueChange);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
function updateTrigger() {
|
||||||
|
reference.once('value', snapshot => {
|
||||||
|
const data = snapshot.val();
|
||||||
|
reference.update({ trigger: !data.trigger });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async function handleLoad(cat: 'detail' | 'komentar' | 'cek-anggota', loading: boolean) {
|
async function handleLoad(cat: 'detail' | 'komentar' | 'cek-anggota', loading: boolean) {
|
||||||
@@ -94,7 +115,7 @@ export default function DetailDiscussionGeneral() {
|
|||||||
const response = await apiSendDiscussionGeneralCommentar({ id: id, data: { desc: komentar, user: hasil } })
|
const response = await apiSendDiscussionGeneralCommentar({ id: id, data: { desc: komentar, user: hasil } })
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
setKomentar('')
|
setKomentar('')
|
||||||
handleLoad('komentar', false)
|
updateTrigger()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
"@react-native-async-storage/async-storage": "^2.1.2",
|
"@react-native-async-storage/async-storage": "^2.1.2",
|
||||||
"@react-native-community/datetimepicker": "8.2.0",
|
"@react-native-community/datetimepicker": "8.2.0",
|
||||||
"@react-native-firebase/app": "^22.2.1",
|
"@react-native-firebase/app": "^22.2.1",
|
||||||
|
"@react-native-firebase/database": "^22.4.0",
|
||||||
"@react-native-firebase/messaging": "^22.2.1",
|
"@react-native-firebase/messaging": "^22.2.1",
|
||||||
"@react-navigation/bottom-tabs": "^7.2.0",
|
"@react-navigation/bottom-tabs": "^7.2.0",
|
||||||
"@react-navigation/native": "^7.0.14",
|
"@react-navigation/native": "^7.0.14",
|
||||||
|
|||||||
Reference in New Issue
Block a user