Parafia Skoczów
  • 👋Witaj w Parafii Skoczów
  • O APLIKACJI
    • ✨Funkcjonalności
    • 📷Galeria
    • 🛠️Zarządzanie treściami
  • DLA PROGRAMISTÓW
    • 🏗️Przegląd narzędzi
    • ⛪Parafia Skoczów
      • Assets
      • Components
        • Atoms
        • Molecules
        • Organisms
        • Templates
      • Helpers
        • getAnimationProps
        • getData
        • getTransmissionUrl
        • searchContent
        • useModal
      • Hooks
        • useDisablePinchZoom
        • usePinching
        • useSwipe
      • Providers
        • ContentProvider
        • FirebaseProvider
        • PlaylistProvider
      • Utils
      • Views
        • Home
        • Categories
        • Titles
        • Text
        • Playlist
        • Search
      • Cordova
      • Rozwiązywanie problemów
    • 🎶Playlist Maker
      • Assets
      • Components
        • Atoms
        • Molecules
        • Organisms
        • Templates
      • Helpers
      • Hooks
        • useDnd
        • useEdit
        • useName
        • useModal
      • Providers
        • ContentProvider
        • FirebaseProvider
        • NotificationProvider
        • PlaylistProvider
      • Utils
      • Cordova
      • Rozwiązywanie problemów
Powered by GitBook
On this page
  1. DLA PROGRAMISTÓW
  2. Parafia Skoczów
  3. Providers

FirebaseProvider

Pobiera dane do utworzenia playlist z Firebase Real Time Database.

FirebaseProvider.js
import React, { useState } from 'react';
import { initializeApp } from 'firebase/app';
import { getDatabase, ref, onValue } from 'firebase/database';

export const FirebaseContext = React.createContext({
  playlists: [],
});

const firebaseConfig = {
  apiKey: '',
  authDomain: '',
  databaseURL: '',
  projectId: '',
  storageBucket: '',
  messagingSenderId: '',
  appId: '',
  measurementId: '',
};

const FirebaseProvider = ({ children }) => {
  const app = initializeApp(firebaseConfig);
  const db = getDatabase(app);
  const distanceRef = ref(db, '/');
  const [playlists, setPlaylists] = useState([]);

  // Fetches data immediately after a change occurs in the database
  onValue(distanceRef, (snapshot) => {
    let data = [];
    let value = snapshot.val();
    for (const key in value) {
      // Convert ISO string to a Date object
      value[key].day = new Date(value[key].day);
      data.push(value[key]);
    }

    // Updates the data if a change has occurred
    if (JSON.stringify(data) !== JSON.stringify(playlists)) {
      setPlaylists(data);
    }
  });

  return (
    <FirebaseContext.Provider
      value={{
        playlists,
      }}
    >
      {children}
    </FirebaseContext.Provider>
  );
};

export default FirebaseProvider;
PreviousContentProviderNextPlaylistProvider

Last updated 1 year ago

⛪