Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use createSlice with teams #2111

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/pages/team-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import 'plastic-image';
import '../elements/shared-styles';
import { ReduxMixin } from '../mixins/redux-mixin';
import { RootState, store } from '../store';
import { fetchTeams } from '../store/teams/actions';
import { initialTeamsState } from '../store/teams/state';
import { fetchTeams, initialTeamsState } from '../store/teams';

@customElement('team-page')
export class TeamPage extends ReduxMixin(PolymerElement) {
@property({ type: Object })
teams = initialTeamsState;
teams = initialTeamsState.value;

@computed('teams')
get pending() {
Expand Down Expand Up @@ -199,7 +198,7 @@ export class TeamPage extends ReduxMixin(PolymerElement) {
}

override stateChanged(state: RootState) {
this.teams = state.teams;
this.teams = state.teams.value;
}

override connectedCallback() {
Expand Down
2 changes: 1 addition & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { scheduleReducer } from './schedule/reducers';
import { sessionsReducer } from './sessions/reducers';
import { speakersReducer } from './speakers/reducers';
import { subscribeReducer } from './subscribe/reducers';
import { teamsReducer } from './teams/reducers';
import teamsReducer from './teams';
import { ticketsReducer } from './tickets/reducers';
import { toastReducer } from './toast/reducers';
import { uiReducer } from './ui/reducers';
Expand Down
46 changes: 0 additions & 46 deletions src/store/teams/actions.ts

This file was deleted.

54 changes: 54 additions & 0 deletions src/store/teams/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Failure, Initialized, Pending, RemoteData, Success } from '@abraham/remotedata';
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { collection, getDocs, query } from 'firebase/firestore';
import { db } from '../../firebase';
import { Member } from '../../models/member';
import { Team, TeamWithoutMembers } from '../../models/team';
import { mergeDataAndId } from '../../utils/firestore';

type TeamsState = { value: RemoteData<Error, Team[]> };

export const initialTeamsState = { value: new Initialized() } as TeamsState;

const getTeamIds = async (): Promise<TeamWithoutMembers[]> => {
const { docs } = await getDocs(query(collection(db, 'team')));

return docs.map<TeamWithoutMembers>(mergeDataAndId);
};

const getTeamMembers = async (team: TeamWithoutMembers): Promise<Team> => {
const { docs } = await getDocs(query(collection(db, 'team', team.id, 'members')));
const members = docs.map<Member>(mergeDataAndId);

return {
...team,
members,
};
};

const getTeams = async (): Promise<Team[]> => {
const teamIds = await getTeamIds();
return Promise.all(teamIds.map(getTeamMembers));
};

export const fetchTeams = createAsyncThunk('teams/fetch', async () => getTeams());

export const teamSlice = createSlice({
name: 'teams',
initialState: initialTeamsState,
reducers: {},
extraReducers: (builder) => {
builder
.addCase(fetchTeams.pending, (state) => {
state.value = new Pending();
})
.addCase(fetchTeams.rejected, (state, action) => {
state.value = new Failure(action.payload);
})
.addCase(fetchTeams.fulfilled, (state, action) => {
state.value = new Success(action.payload);
});
},
});

export default teamSlice.reducer;
19 changes: 0 additions & 19 deletions src/store/teams/reducers.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/store/teams/state.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/store/teams/types.ts

This file was deleted.