Add a createdAt field to compositions.

pull/218/head
Atul Varma 2021-08-22 15:03:09 -04:00
rodzic be3f1abab0
commit aaf76fe86f
2 zmienionych plików z 21 dodań i 6 usunięć

Wyświetl plik

@ -13,7 +13,10 @@ import {
getFirestore, getFirestore,
collection, collection,
getDocs, getDocs,
query,
orderBy,
CollectionReference, CollectionReference,
Timestamp,
} from "firebase/firestore"; } from "firebase/firestore";
import React, { useCallback, useContext, useEffect, useState } from "react"; import React, { useCallback, useContext, useEffect, useState } from "react";
import { AuthContext } from "./auth-context"; import { AuthContext } from "./auth-context";
@ -103,7 +106,12 @@ export const FirebaseGithubAuthProvider: React.FC<{}> = ({ children }) => {
return <AuthContext.Provider value={context} children={children} />; return <AuthContext.Provider value={context} children={children} />;
}; };
type FirebaseCompositionDocument = Omit<GalleryComposition, "id">; type FirebaseCompositionDocument = Omit<
GalleryComposition,
"id" | "createdAt"
> & {
createdAt: Timestamp;
};
function getGalleryCollection(appCtx: FirebaseAppContext) { function getGalleryCollection(appCtx: FirebaseAppContext) {
return collection( return collection(
@ -134,15 +142,19 @@ export const FirebaseGalleryProvider: React.FC<{}> = ({ children }) => {
setError(undefined); setError(undefined);
setIsLoading(true); setIsLoading(true);
getDocs(getGalleryCollection(appCtx)) getDocs(query(getGalleryCollection(appCtx), orderBy("createdAt", "desc")))
.then((snapshot) => { .then((snapshot) => {
setLastRefresh(Date.now()); setLastRefresh(Date.now());
setIsLoading(false); setIsLoading(false);
setCompositions( setCompositions(
snapshot.docs.map((doc) => ({ snapshot.docs.map((doc) => {
const { createdAt, ...data } = doc.data();
return {
...data,
id: doc.id, id: doc.id,
...doc.data(), createdAt: createdAt.toDate(),
})) };
})
); );
}) })
.catch(handleError); .catch(handleError);

Wyświetl plik

@ -24,6 +24,9 @@ export type GalleryComposition = {
/** The title of the composition. */ /** The title of the composition. */
title: string; title: string;
/** When the composition was submitted to the gallery. */
createdAt: Date;
}; };
/** /**