-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2116 from gdg-x/realtime
Update data in realtime
- Loading branch information
Showing
26 changed files
with
419 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,27 @@ | ||
{ | ||
"indexes": [], | ||
"fieldOverrides": [] | ||
"fieldOverrides": [ | ||
{ | ||
"collectionGroup": "members", | ||
"fieldPath": "name", | ||
"indexes": [ | ||
{ | ||
"order": "ASCENDING", | ||
"queryScope": "COLLECTION" | ||
}, | ||
{ | ||
"order": "DESCENDING", | ||
"queryScope": "COLLECTION" | ||
}, | ||
{ | ||
"arrayConfig": "CONTAINS", | ||
"queryScope": "COLLECTION" | ||
}, | ||
{ | ||
"order": "ASCENDING", | ||
"queryScope": "COLLECTION_GROUP" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export interface Id { | ||
id: string; | ||
} | ||
|
||
export type ParentId = Id & { | ||
parentId: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,31 @@ | ||
import { collection, getDocs, orderBy, query } from 'firebase/firestore'; | ||
import { Initialized, Success } from '@abraham/remotedata'; | ||
import { orderBy } from 'firebase/firestore'; | ||
import { Dispatch } from 'redux'; | ||
import { db } from '../../firebase'; | ||
import { Post } from '../../models/post'; | ||
import { mergeDataAndId } from '../../utils/firestore'; | ||
import { subscribeToCollection, Subscription } from '../../utils/firestore'; | ||
import { | ||
BlogActions, | ||
FETCH_BLOG_LIST, | ||
FETCH_BLOG_LIST_FAILURE, | ||
FETCH_BLOG_LIST_SUCCESS, | ||
} from './types'; | ||
|
||
const getPosts = async (): Promise<Post[]> => { | ||
const { docs } = await getDocs(query(collection(db, 'blog'), orderBy('published', 'desc'))); | ||
let subscription: Subscription = new Initialized(); | ||
|
||
return docs.map<Post>(mergeDataAndId); | ||
export const unsubscribe = () => { | ||
if (subscription instanceof Success) { | ||
subscription.data(); | ||
} | ||
}; | ||
|
||
export const fetchBlogPosts = async (dispatch: Dispatch<BlogActions>) => { | ||
dispatch({ | ||
type: FETCH_BLOG_LIST, | ||
}); | ||
|
||
try { | ||
dispatch({ | ||
type: FETCH_BLOG_LIST_SUCCESS, | ||
payload: await getPosts(), | ||
}); | ||
} catch (error) { | ||
dispatch({ | ||
type: FETCH_BLOG_LIST_FAILURE, | ||
payload: error, | ||
}); | ||
if (subscription instanceof Initialized) { | ||
subscription = subscribeToCollection( | ||
'blog', | ||
() => dispatch({ type: FETCH_BLOG_LIST }), | ||
(payload: Post[]) => dispatch({ type: FETCH_BLOG_LIST_SUCCESS, payload }), | ||
(payload: Error) => dispatch({ type: FETCH_BLOG_LIST_FAILURE, payload }), | ||
orderBy('published', 'desc') | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,31 @@ | ||
import { collection, getDocs, query } from 'firebase/firestore'; | ||
import { Initialized, Success } from '@abraham/remotedata'; | ||
import { orderBy } from 'firebase/firestore'; | ||
import { Dispatch } from 'redux'; | ||
import { db } from '../../firebase'; | ||
import { Photo } from '../../models/photo'; | ||
import { mergeDataAndId } from '../../utils/firestore'; | ||
import { subscribeToCollection, Subscription } from '../../utils/firestore'; | ||
import { | ||
FETCH_GALLERY, | ||
FETCH_GALLERY_FAILURE, | ||
FETCH_GALLERY_SUCCESS, | ||
GalleryActions, | ||
} from './types'; | ||
|
||
const getGalleries = async (): Promise<Photo[]> => { | ||
const { docs } = await getDocs(query(collection(db, 'gallery'))); | ||
let subscription: Subscription = new Initialized(); | ||
|
||
return docs.map<Photo>(mergeDataAndId); | ||
export const unsubscribe = () => { | ||
if (subscription instanceof Success) { | ||
subscription.data(); | ||
} | ||
}; | ||
|
||
export const fetchGallery = async (dispatch: Dispatch<GalleryActions>) => { | ||
dispatch({ | ||
type: FETCH_GALLERY, | ||
}); | ||
|
||
try { | ||
dispatch({ | ||
type: FETCH_GALLERY_SUCCESS, | ||
payload: await getGalleries(), | ||
}); | ||
} catch (error) { | ||
dispatch({ | ||
type: FETCH_GALLERY_FAILURE, | ||
payload: error, | ||
}); | ||
if (subscription instanceof Initialized) { | ||
subscription = subscribeToCollection( | ||
'gallery', | ||
() => dispatch({ type: FETCH_GALLERY }), | ||
(payload: Photo[]) => dispatch({ type: FETCH_GALLERY_SUCCESS, payload }), | ||
(payload: Error) => dispatch({ type: FETCH_GALLERY_FAILURE, payload }), | ||
orderBy('order') | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Initialized, Success } from '@abraham/remotedata'; | ||
import { Dispatch } from 'redux'; | ||
import { Member } from '../../models/member'; | ||
import { subscribeToCollectionGroup, Subscription } from '../../utils/firestore'; | ||
import { | ||
FETCH_MEMBERS, | ||
FETCH_MEMBERS_FAILURE, | ||
FETCH_MEMBERS_SUCCESS, | ||
MembersActions, | ||
} from './types'; | ||
|
||
let subscription: Subscription = new Initialized(); | ||
|
||
export const unsubscribe = () => { | ||
if (subscription instanceof Success) { | ||
subscription.data(); | ||
} | ||
}; | ||
|
||
export const fetchMembers = async (dispatch: Dispatch<MembersActions>) => { | ||
if (subscription instanceof Initialized) { | ||
subscription = subscribeToCollectionGroup( | ||
'members', | ||
() => dispatch({ type: FETCH_MEMBERS }), | ||
(payload: Member[]) => dispatch({ type: FETCH_MEMBERS_SUCCESS, payload }), | ||
(payload: Error) => dispatch({ type: FETCH_MEMBERS_FAILURE, payload }) | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Failure, Pending, Success } from '@abraham/remotedata'; | ||
import { initialMembersState, MembersState } from './state'; | ||
import { | ||
FETCH_MEMBERS, | ||
FETCH_MEMBERS_FAILURE, | ||
FETCH_MEMBERS_SUCCESS, | ||
MembersActions, | ||
} from './types'; | ||
|
||
export const membersReducer = ( | ||
state = initialMembersState, | ||
action: MembersActions | ||
): MembersState => { | ||
switch (action.type) { | ||
case FETCH_MEMBERS: | ||
return new Pending(); | ||
|
||
case FETCH_MEMBERS_FAILURE: | ||
return new Failure(action.payload); | ||
|
||
case FETCH_MEMBERS_SUCCESS: | ||
return new Success(action.payload); | ||
|
||
default: | ||
return state; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Initialized, Pending } from '@abraham/remotedata'; | ||
import { RootState, store } from '..'; | ||
import { fetchMembers } from './actions'; | ||
import { MembersState } from './state'; | ||
|
||
export const selectMembers = (state: RootState): MembersState => { | ||
if (state.members instanceof Initialized) { | ||
store.dispatch(fetchMembers); | ||
return new Pending(); | ||
} else { | ||
return state.members; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Initialized, RemoteData } from '@abraham/remotedata'; | ||
import { Member } from '../../models/member'; | ||
|
||
export type MembersState = RemoteData<Error, Member[]>; | ||
export const initialMembersState: MembersState = new Initialized(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Member } from '../../models/member'; | ||
|
||
export const FETCH_MEMBERS = 'FETCH_MEMBERS'; | ||
export const FETCH_MEMBERS_FAILURE = 'FETCH_MEMBERS_FAILURE'; | ||
export const FETCH_MEMBERS_SUCCESS = 'FETCH_MEMBERS_SUCCESS'; | ||
|
||
interface FetchMembersAction { | ||
type: typeof FETCH_MEMBERS; | ||
} | ||
|
||
interface FetchMembersFailureAction { | ||
type: typeof FETCH_MEMBERS_FAILURE; | ||
payload: Error; | ||
} | ||
|
||
interface FetchMembersSuccessAction { | ||
type: typeof FETCH_MEMBERS_SUCCESS; | ||
payload: Member[]; | ||
} | ||
|
||
export type MembersActions = | ||
| FetchMembersAction | ||
| FetchMembersFailureAction | ||
| FetchMembersSuccessAction; |
Oops, something went wrong.