Skip to content

Commit

Permalink
Create subscribeToCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham committed Jan 4, 2022
1 parent c76d790 commit a5e9caa
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 231 deletions.
36 changes: 16 additions & 20 deletions src/store/blog/actions.ts
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')
);
}
};
36 changes: 16 additions & 20 deletions src/store/gallery/actions.ts
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')
);
}
};
35 changes: 9 additions & 26 deletions src/store/members/actions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Failure, Initialized, Pending, RemoteData, Success } from '@abraham/remotedata';
import { collectionGroup, onSnapshot, orderBy, query, Unsubscribe } from 'firebase/firestore';
import { Initialized, Success } from '@abraham/remotedata';
import { Dispatch } from 'redux';
import { db } from '../../firebase';
import { Member } from '../../models/member';
import { dataWithParentId } from '../../utils/firestore';
import { subscribeToCollectionGroup, Subscription } from '../../utils/firestore';
import {
FETCH_MEMBERS,
FETCH_MEMBERS_FAILURE,
FETCH_MEMBERS_SUCCESS,
MembersActions,
} from './types';

type Subscription = RemoteData<Error, Unsubscribe>;
let subscription: Subscription = new Initialized();

export const unsubscribe = () => {
Expand All @@ -20,27 +17,13 @@ export const unsubscribe = () => {
}
};

const subscribe = (path: string, dispatch: Dispatch<MembersActions>) => {
const unsubscribe = onSnapshot(
query(collectionGroup(db, path), orderBy('name', 'asc')),
(snapshot) => {
const payload = snapshot.docs.map<Member>(dataWithParentId);
dispatch({ type: FETCH_MEMBERS_SUCCESS, payload });
},
(payload) => {
subscription = new Failure(payload);
dispatch({ type: FETCH_MEMBERS_FAILURE, payload });
}
);

return new Success(unsubscribe);
};

export const fetchMembers = () => async (dispatch: Dispatch<MembersActions>) => {
export const fetchMembers = async (dispatch: Dispatch<MembersActions>) => {
if (subscription instanceof Initialized) {
subscription = new Pending();
dispatch({ type: FETCH_MEMBERS });

subscription = subscribe('members', dispatch);
subscription = subscribeToCollectionGroup(
'members',
() => dispatch({ type: FETCH_MEMBERS }),
(payload: Member[]) => dispatch({ type: FETCH_MEMBERS_SUCCESS, payload }),
(payload: Error) => dispatch({ type: FETCH_MEMBERS_FAILURE, payload })
);
}
};
2 changes: 1 addition & 1 deletion src/store/members/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MembersState } from './state';

export const selectMembers = (state: RootState): MembersState => {
if (state.members instanceof Initialized) {
store.dispatch(fetchMembers());
store.dispatch(fetchMembers);
return new Pending();
} else {
return state.members;
Expand Down
38 changes: 14 additions & 24 deletions src/store/previous-speakers/actions.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
import { collection, getDocs, orderBy, query } from 'firebase/firestore';
import { Initialized, Success } from '@abraham/remotedata';
import { Dispatch } from 'redux';
import { db } from '../../firebase';
import { PreviousSpeaker } from '../../models/previous-speaker';
import { mergeDataAndId } from '../../utils/firestore';
import { subscribeToCollection, Subscription } from '../../utils/firestore';
import {
FETCH_PREVIOUS_SPEAKERS,
FETCH_PREVIOUS_SPEAKERS_FAILURE,
FETCH_PREVIOUS_SPEAKERS_SUCCESS,
PreviousSpeakersActions,
} from './types';

const getPreviousSpeakers = async (): Promise<PreviousSpeaker[]> => {
const { docs } = await getDocs(
query(collection(db, 'previousSpeakers'), orderBy('order', 'asc'))
);
let subscription: Subscription = new Initialized();

return docs.map<PreviousSpeaker>(mergeDataAndId).sort((speakerA, speakerB) => {
return speakerA.name.localeCompare(speakerB.name);
});
export const unsubscribe = () => {
if (subscription instanceof Success) {
subscription.data();
}
};

export const fetchPreviousSpeakers = async (dispatch: Dispatch<PreviousSpeakersActions>) => {
dispatch({
type: FETCH_PREVIOUS_SPEAKERS,
});

try {
dispatch({
type: FETCH_PREVIOUS_SPEAKERS_SUCCESS,
payload: await getPreviousSpeakers(),
});
} catch (error) {
dispatch({
type: FETCH_PREVIOUS_SPEAKERS_FAILURE,
payload: error,
});
if (subscription instanceof Initialized) {
subscription = subscribeToCollection(
'previousSpeakers',
() => dispatch({ type: FETCH_PREVIOUS_SPEAKERS }),
(payload: PreviousSpeaker[]) => dispatch({ type: FETCH_PREVIOUS_SPEAKERS_SUCCESS, payload }),
(payload: Error) => dispatch({ type: FETCH_PREVIOUS_SPEAKERS_FAILURE, payload })
);
}
};
35 changes: 16 additions & 19 deletions src/store/schedule/actions.ts
Original file line number Diff line number Diff line change
@@ -1,34 +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 { Day } from '../../models/day';
import { subscribeToCollection, Subscription } from '../../utils/firestore';
import {
FETCH_SCHEDULE,
FETCH_SCHEDULE_FAILURE,
FETCH_SCHEDULE_SUCCESS,
ScheduleActions,
} from './types';

const getSchedule = async (): Promise<Day[]> => {
const { docs } = await getDocs(query(collection(db, 'generatedSchedule')));
let subscription: Subscription = new Initialized();

return docs.map<Day>((doc) => doc.data() as Day).sort((a, b) => a.date.localeCompare(b.date));
export const unsubscribe = () => {
if (subscription instanceof Success) {
subscription.data();
}
};

export const fetchSchedule = async (dispatch: Dispatch<ScheduleActions>) => {
dispatch({
type: FETCH_SCHEDULE,
});

try {
dispatch({
type: FETCH_SCHEDULE_SUCCESS,
payload: await getSchedule(),
});
} catch (error) {
dispatch({
type: FETCH_SCHEDULE_FAILURE,
payload: error,
});
if (subscription instanceof Initialized) {
subscription = subscribeToCollection(
'generatedSchedule',
() => dispatch({ type: FETCH_SCHEDULE }),
(payload: Day[]) => dispatch({ type: FETCH_SCHEDULE_SUCCESS, payload }),
(payload: Error) => dispatch({ type: FETCH_SCHEDULE_FAILURE, payload }),
orderBy('date')
);
}
};
37 changes: 17 additions & 20 deletions src/store/sessions/actions.ts
Original file line number Diff line number Diff line change
@@ -1,34 +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 { Session } from '../../models/session';
import { mergeDataAndId } from '../../utils/firestore';
import { subscribeToCollection, Subscription } from '../../utils/firestore';
import {
FETCH_SESSIONS,
FETCH_SESSIONS_FAILURE,
FETCH_SESSIONS_SUCCESS,
SessionsActions,
} from './types';

const getSessions = async () => {
const { docs } = await getDocs(query(collection(db, 'generatedSessions')));
return docs.map<Session>(mergeDataAndId);
let subscription: Subscription = new Initialized();

export const unsubscribe = () => {
if (subscription instanceof Success) {
subscription.data();
}
};

export const fetchSessions = async (dispatch: Dispatch<SessionsActions>) => {
dispatch({
type: FETCH_SESSIONS,
});

try {
dispatch({
type: FETCH_SESSIONS_SUCCESS,
payload: await getSessions(),
});
} catch (error) {
dispatch({
type: FETCH_SESSIONS_FAILURE,
payload: error,
});
if (subscription instanceof Initialized) {
subscription = subscribeToCollection(
'generatedSessions',
() => dispatch({ type: FETCH_SESSIONS }),
(payload: Session[]) => dispatch({ type: FETCH_SESSIONS_SUCCESS, payload }),
(payload: Error) => dispatch({ type: FETCH_SESSIONS_FAILURE, payload }),
orderBy('day')
);
}
};
33 changes: 8 additions & 25 deletions src/store/speakers/actions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Failure, Initialized, Pending, RemoteData, Success } from '@abraham/remotedata';
import { collection, onSnapshot, orderBy, query, Unsubscribe } from 'firebase/firestore';
import { Initialized, Success } from '@abraham/remotedata';
import { Dispatch } from 'redux';
import { db } from '../../firebase';
import { SpeakerWithTags } from '../../models/speaker';
import { mergeDataAndId } from '../../utils/firestore';
import { subscribeToCollection, Subscription } from '../../utils/firestore';
import {
FETCH_SPEAKERS,
FETCH_SPEAKERS_FAILURE,
FETCH_SPEAKERS_SUCCESS,
SpeakerActions,
} from './types';

type Subscription = RemoteData<Error, Unsubscribe>;
let subscription: Subscription = new Initialized();

export const unsubscribe = () => {
Expand All @@ -20,27 +17,13 @@ export const unsubscribe = () => {
}
};

const subscribe = (path: string, dispatch: Dispatch<SpeakerActions>) => {
const unsubscribe = onSnapshot(
query(collection(db, path), orderBy('name', 'asc')),
(snapshot) => {
const payload = snapshot.docs.map<SpeakerWithTags>(mergeDataAndId);
dispatch({ type: FETCH_SPEAKERS_SUCCESS, payload });
},
(payload) => {
subscription = new Failure(payload);
dispatch({ type: FETCH_SPEAKERS_FAILURE, payload });
}
);

return new Success(unsubscribe);
};

export const fetchSpeakers = async (dispatch: Dispatch<SpeakerActions>) => {
if (subscription instanceof Initialized) {
subscription = new Pending();
dispatch({ type: FETCH_SPEAKERS });

subscription = subscribe('generatedSpeakers', dispatch);
subscription = subscribeToCollection(
'generatedSpeakers',
() => dispatch({ type: FETCH_SPEAKERS }),
(payload: SpeakerWithTags[]) => dispatch({ type: FETCH_SPEAKERS_SUCCESS, payload }),
(payload: Error) => dispatch({ type: FETCH_SPEAKERS_FAILURE, payload })
);
}
};
Loading

0 comments on commit a5e9caa

Please sign in to comment.