Skip to content

Commit

Permalink
Create 'useUpdateSubscription' hook for 'react-redux-firebase-firesto…
Browse files Browse the repository at this point in the history
…re' functionality
  • Loading branch information
ikusteu committed Jan 16, 2024
1 parent ae2b3c3 commit 0a0619f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";

import { CollectionSubscription } from "../types";

import { getFirestoreListeners } from "../selectors";

import { updateSubscription } from "../thunks/subscribe";

import { getCollectionPath, getConstraintForColl } from "../utils/utils";

const useUpdateSubscription = (
subscription: CollectionSubscription,
deps: any[]
): void => {
const dispatch = useDispatch();
const collection = subscription.collection;

const listeners = useSelector(getFirestoreListeners);

useEffect(() => {
const listener = listeners[collection];

if (!subscription.meta || !listener) return;

const meta = { ...listener.meta, ...subscription.meta };

dispatch(
updateSubscription({
storeAs: collection,
collPath: getCollectionPath(subscription.collection, meta),
constraint: getConstraintForColl(subscription.collection, meta),
meta,
})
);
}, deps);
};

export default useUpdateSubscription;
8 changes: 4 additions & 4 deletions packages/react-redux-firebase-firestore/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createFirestoreReducer } from "./reducer";
import useFirestoreSubscribe from "./hooks/useFirestoreSubscribe";
import usePaginateFirestore from "./hooks/usePaginateFirestore";
export { createFirestoreReducer } from "./reducer";

export { createFirestoreReducer, useFirestoreSubscribe, usePaginateFirestore };
export { default as useFirestoreSubscribe } from "./hooks/useFirestoreSubscribe";
export { default as usePaginateFirestore } from "./hooks/usePaginateFirestore";
export { default as useUpdateSubscription } from "./hooks/useUpdateSubscription";

export * from "./types";
export * from "./actions";

0 comments on commit 0a0619f

Please sign in to comment.