-
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.
- Loading branch information
Showing
20 changed files
with
332 additions
and
100 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
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,46 @@ | ||
import { Failure, Initialized, Pending, RemoteData, Success } from '@abraham/remotedata'; | ||
import { collectionGroup, onSnapshot, orderBy, query, Unsubscribe } from 'firebase/firestore'; | ||
import { Dispatch } from 'redux'; | ||
import { db } from '../../firebase'; | ||
import { Member } from '../../models/member'; | ||
import { dataWithParentId } 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 = () => { | ||
if (subscription instanceof Success) { | ||
subscription.data(); | ||
} | ||
}; | ||
|
||
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>) => { | ||
if (subscription instanceof Initialized) { | ||
subscription = new Pending(); | ||
dispatch({ type: FETCH_MEMBERS }); | ||
|
||
subscription = subscribe('members', dispatch); | ||
} | ||
}; |
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; |
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,35 @@ | ||
import { Failure, Initialized, Pending, Success } from '@abraham/remotedata'; | ||
import { createSelector } from '@reduxjs/toolkit'; | ||
import { Member } from '../../models/member'; | ||
import { Team, TeamWithoutMembers } from '../../models/team'; | ||
import { selectMembers } from '../members/selectors'; | ||
import { MembersState } from '../members/state'; | ||
import { selectTeams } from '../teams/selectors'; | ||
import { TeamsState } from '../teams/state'; | ||
import { TeamsMembersState } from './state'; | ||
|
||
const mergeMembers = (team: TeamWithoutMembers, possibleMembers: Member[]): Team => { | ||
return { | ||
...team, | ||
members: possibleMembers.filter((member) => member.parentId === team.id), | ||
}; | ||
}; | ||
|
||
export const selectTeamsAndMembers = createSelector( | ||
selectTeams, | ||
selectMembers, | ||
(teams: TeamsState, members: MembersState): TeamsMembersState => { | ||
if (teams instanceof Success && members instanceof Success) { | ||
const merged = teams.data.map((team) => mergeMembers(team, members.data)); | ||
return new Success(merged); | ||
} else if (teams instanceof Pending || members instanceof Pending) { | ||
return new Pending(); | ||
} else if (teams instanceof Failure) { | ||
return new Failure(teams.error); | ||
} else if (members instanceof Failure) { | ||
return new Failure(members.error); | ||
} else { | ||
return 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,5 @@ | ||
import { Initialized, RemoteData } from '@abraham/remotedata'; | ||
import { Team } from '../../models/team'; | ||
|
||
export type TeamsMembersState = RemoteData<Error, Team[]>; | ||
export const initialTeamsMembersState: TeamsMembersState = new Initialized(); |
Oops, something went wrong.