Refresh gallery less aggressively.

pull/217/head
Atul Varma 2021-08-22 12:01:30 -04:00
rodzic 3668d078ee
commit f7d3475cd2
3 zmienionych plików z 10 dodań i 8 usunięć

Wyświetl plik

@ -117,6 +117,7 @@ export const FirebaseGalleryProvider: React.FC<{}> = ({ children }) => {
const [compositions, setCompositions] = useState<GalleryComposition[]>([]); const [compositions, setCompositions] = useState<GalleryComposition[]>([]);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | undefined>(undefined); const [error, setError] = useState<string | undefined>(undefined);
const [lastRefresh, setLastRefresh] = useState(0);
const handleError = (e: Error) => { const handleError = (e: Error) => {
setIsLoading(false); setIsLoading(false);
@ -127,6 +128,7 @@ export const FirebaseGalleryProvider: React.FC<{}> = ({ children }) => {
compositions, compositions,
isLoading, isLoading,
error, error,
lastRefresh,
refresh: useCallback(() => { refresh: useCallback(() => {
if (!(appCtx && !isLoading)) return false; if (!(appCtx && !isLoading)) return false;
@ -134,6 +136,7 @@ export const FirebaseGalleryProvider: React.FC<{}> = ({ children }) => {
setIsLoading(true); setIsLoading(true);
getDocs(getGalleryCollection(appCtx)) getDocs(getGalleryCollection(appCtx))
.then((snapshot) => { .then((snapshot) => {
setLastRefresh(Date.now());
setIsLoading(false); setIsLoading(false);
setCompositions( setCompositions(
snapshot.docs.map((doc) => ({ snapshot.docs.map((doc) => ({

Wyświetl plik

@ -14,10 +14,12 @@ export interface GalleryContext {
isLoading: boolean; isLoading: boolean;
error?: string; error?: string;
refresh(): boolean; refresh(): boolean;
lastRefresh: number;
} }
export const GalleryContext = React.createContext<GalleryContext>({ export const GalleryContext = React.createContext<GalleryContext>({
compositions: [], compositions: [],
isLoading: false, isLoading: false,
refresh: () => true, refresh: () => true,
lastRefresh: 0,
}); });

Wyświetl plik

@ -1,4 +1,4 @@
import React, { useContext, useEffect, useState } from "react"; import React, { useContext, useEffect } from "react";
import { GalleryComposition, GalleryContext } from "../gallery-context"; import { GalleryComposition, GalleryContext } from "../gallery-context";
import { Page } from "../page"; import { Page } from "../page";
import { createPageWithStateSearchParams } from "../page-with-shareable-state"; import { createPageWithStateSearchParams } from "../page-with-shareable-state";
@ -23,20 +23,17 @@ const GalleryCompositionView: React.FC<GalleryComposition> = (props) => {
export const GalleryPage: React.FC<{}> = () => { export const GalleryPage: React.FC<{}> = () => {
const ctx = useContext(GalleryContext); const ctx = useContext(GalleryContext);
const [refreshed, setRefreshed] = useState(false);
useEffect(() => { useEffect(() => {
if (!refreshed) { if (ctx.lastRefresh === 0) {
if (ctx.refresh()) { ctx.refresh();
setRefreshed(true);
}
} }
}, [ctx, refreshed]); }, [ctx]);
return ( return (
<Page title="Gallery!"> <Page title="Gallery!">
<div className="sidebar"> <div className="sidebar">
<button onClick={() => setRefreshed(false)} disabled={ctx.isLoading}> <button onClick={ctx.refresh} disabled={ctx.isLoading}>
{ctx.isLoading ? "Loading\u2026" : "Refresh"} {ctx.isLoading ? "Loading\u2026" : "Refresh"}
</button> </button>
{ctx.error && <p className="error">{ctx.error}</p>} {ctx.error && <p className="error">{ctx.error}</p>}