Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collectionSubの引数にQueryConstraintを取るようにした #113

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app/firebase/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import {
onSnapshot,
query,
type FirestoreDataConverter,
type QueryConstraint,
} from "firebase/firestore";
import { type SWRSubscription } from "swr/subscription";

import { prodDB } from "./firestore";

export const collectionSub = <T>(converter: FirestoreDataConverter<T>) => {
export const collectionSub = <T>(
{ converter }: { converter: FirestoreDataConverter<T> },
...queryConstraints: QueryConstraint[]
) => {
const sub: SWRSubscription<string, T[], Error> = (key, { next }) => {
const unsub = onSnapshot(
query(collection(prodDB, key)).withConverter(converter),
query(collection(prodDB, key), ...queryConstraints).withConverter(
converter,
),
(snapshot) => {
next(
null,
Expand Down
2 changes: 1 addition & 1 deletion app/routes/_header.serve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const clientLoader = async () => {
export default function Serve() {
const { data: orders } = useSWRSubscription(
"orders",
collectionSub(orderConverter),
collectionSub({ converter: orderConverter }),
);

return (
Expand Down
4 changes: 2 additions & 2 deletions app/routes/cashier-v2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const keys = ["a", "s", "d", "f", "g", "h", "j", "k", "l", ";"];
export default function Cashier() {
const { data: items } = useSWRSubscription(
"items",
collectionSub(itemConverter),
collectionSub({ converter: itemConverter }),
);
const { data: orders } = useSWRSubscription(
"orders",
collectionSub(orderConverter),
collectionSub({ converter: orderConverter }),
);
const [orderItems, setOrderItems] = useState<WithId<ItemEntity>[]>([]);
const submit = useSubmit();
Expand Down
2 changes: 1 addition & 1 deletion app/routes/items/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const meta: MetaFunction = () => {
export default function Item() {
const { data: items } = useSWRSubscription(
"items",
collectionSub(itemConverter),
collectionSub({ converter: itemConverter }),
);
const navigation = useNavigation();
const lastResult = useActionData<typeof addItem>();
Expand Down
5 changes: 3 additions & 2 deletions app/routes/master.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type MetaFunction } from "@remix-run/react";
import { orderBy } from "firebase/firestore";
import useSWRSubscription from "swr/subscription";

import { Card, CardContent, CardHeader, CardTitle } from "~/components/ui/card";
Expand All @@ -10,10 +11,10 @@ export const meta: MetaFunction = () => {
return [{ title: "マスター画面" }];
};

export default function Serve() {
export default function FielsOfMaster() {
const { data: orders } = useSWRSubscription(
"orders",
collectionSub(orderConverter),
collectionSub({ converter: orderConverter }, orderBy("orderId", "desc")),
);

return (
Expand Down