Skip to content

Commit

Permalink
react-firebase-hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
MartsTech committed Mar 9, 2021
1 parent 4f3b5ab commit 01c0b8a
Show file tree
Hide file tree
Showing 8 changed files with 699 additions and 659 deletions.
6 changes: 3 additions & 3 deletions src/app/store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit';
import counterReducer from '../features/counterSlice';
import { configureStore, ThunkAction, Action } from "@reduxjs/toolkit";
import roomReducer from "../features/roomSlice";

export const store = configureStore({
reducer: {
counter: counterReducer,
room: roomReducer,
},
});

Expand Down
10 changes: 9 additions & 1 deletion src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AddIcon from "@material-ui/icons/Add";
import AppsIcon from "@material-ui/icons/Apps";
import BookmarkBorderIcon from "@material-ui/icons/BookmarkBorder";
import CreateIcon from "@material-ui/icons/Create";
Expand All @@ -12,9 +13,12 @@ import React from "react";
import styled from "styled-components";
import { SidebarOption } from "./SidebarOption";
import { StatusBadge } from "./StatusBadge";
import AddIcon from "@material-ui/icons/Add";
import { useCollection } from "react-firebase-hooks/firestore";
import { db } from "../firebase";

export const Sidebar: React.FC = () => {
const [channels] = useCollection(db.collection("rooms"));

return (
<SidebarContainer>
<SidebarHeader>
Expand Down Expand Up @@ -42,6 +46,10 @@ export const Sidebar: React.FC = () => {
<SidebarOption Icon={ExpandMoreIcon} title="Channels" />
<hr />
<SidebarOption Icon={AddIcon} title="Add Channel" addChannelOption />

{channels?.docs.map((doc: any) => (
<SidebarOption key={doc.id} id={doc.id} title={doc.data().name} />
))}
</SidebarContainer>
);
};
Expand Down
19 changes: 16 additions & 3 deletions src/components/SidebarOption.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { SvgIconTypeMap } from "@material-ui/core";
import { OverridableComponent } from "@material-ui/core/OverridableComponent";
import React from "react";
import { db } from "../firebase";
import { useDispatch } from "react-redux";
import { enterRoom } from "src/features/roomSlice";
import styled from "styled-components";
import { db } from "../firebase";

interface SidebarOptionProps {
Icon?: OverridableComponent<SvgIconTypeMap<{}, "svg">>;
id?: string;
title?: string;
addChannelOption?: boolean;
}

export const SidebarOption: React.FC<SidebarOptionProps> = ({
Icon,
id,
title,
addChannelOption,
}) => {
const dispatch = useDispatch();

const addChannel = () => {
const channelName = prompt("Please enter the channel name");

Expand All @@ -25,7 +31,11 @@ export const SidebarOption: React.FC<SidebarOptionProps> = ({
}
};

const selectChannel = () => {};
const selectChannel = () => {
if (id) {
dispatch(enterRoom(id));
}
};

return (
<SidebarOptionContainer
Expand Down Expand Up @@ -64,4 +74,7 @@ const SidebarOptionContainer = styled.div`
}
`;

const SidebarOptionChannel = styled.div``;
const SidebarOptionChannel = styled.h3`
padding: 10px 0;
font-weight: 300;
`;
50 changes: 0 additions & 50 deletions src/features/counterSlice.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/features/roomSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createSlice } from "@reduxjs/toolkit";
import { RootState } from "../app/store";

interface RoomState {
roomId: string | null;
}

const initialState: RoomState = {
roomId: null,
};

export const roomSlice = createSlice({
name: "room",
initialState,
reducers: {
enterRoom: (state, action: { payload: string }) => {
state.roomId = action.payload;
},
},
});

export const { enterRoom } = roomSlice.actions;

export const selectRoomId = (state: RootState) => state.room.roomId;

export default roomSlice.reducer;
4 changes: 3 additions & 1 deletion src/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import firebase from "firebase";
import firebase from "firebase/app";
import "firebase/auth";
import "firebase/firestore";

const firebaseConfig = {
apiKey: process.env.REACT_APP_API_KEY,
Expand Down
24 changes: 11 additions & 13 deletions src/pages/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ import { Sidebar } from "../components/Sidebar";

const App: React.FC = () => {
return (
<div className="app">
<Router>
<>
<Header />
<AppBody>
<Sidebar />
<Switch>
<Route path="/" exact />
</Switch>
</AppBody>
</>
</Router>
</div>
<Router>
<>
<Header />
<AppBody>
<Sidebar />
<Switch>
<Route path="/" exact />
</Switch>
</AppBody>
</>
</Router>
);
};

Expand Down
Loading

0 comments on commit 01c0b8a

Please sign in to comment.