Skip to content
This repository was archived by the owner on Aug 8, 2019. It is now read-only.

Commit 82c1195

Browse files
committed
Connect API with frontend to Owner Clubs view
1 parent 34465e7 commit 82c1195

12 files changed

Lines changed: 268 additions & 77 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ client/npm-debug.log*
4848
client/yarn-debug.log*
4949
client/yarn-error.log*
5050

51+
.env
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class ClubsController < ApplicationController
2+
before_action :set_club, only: [:show, :update, :destroy]
3+
4+
def index
5+
render json: Club.all
6+
end
7+
8+
def show
9+
render json: @club
10+
end
11+
12+
rescue_from ActiveRecord::RecordNotFound do |e|
13+
render json: { message: e.message }, status: :not_found
14+
end
15+
16+
private
17+
def set_club
18+
@club = Club.find(params[:id])
19+
end
20+
21+
def club_params
22+
params.permit(:name, :email, :city, :country, :address, :image, gallery: [])
23+
end
24+
25+
end

api/config/routes.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
Rails.application.routes.draw do
22
scope '/api' do
3-
# sessions routes
43
post '/login', to: 'sessions#login'
54
post '/register', to: 'sessions#register'
6-
75
delete '/logout', to: 'sessions#destroy'
8-
6+
resources :clubs
7+
resources :sport_fields
98
end
109
end

api/db/seeds.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
User.destroy_all
22
User.create(name: 'Lian Nivin', email: 'liam@kampu.pe', role: "regular", password: '123456')
3-
User.create(name: 'Cristian Berly', email: 'berli@kampu.pe', role: "owner", password: '123456')
3+
User.create(name: 'Cristian Berly', email: 'berli@kampu.pe', role: "owner", password: '123456')
4+
5+
clubs = Club.create([{name: "Club #1"}, {name: "Club #2"}, {name: "Club #3"}])
6+
SportField.create(name: "SportField #1", club_id: 1);
7+
SportField.create(name: "SportField #2", club_id: 2);
8+
SportField.create(name: "SportField #3", club_id: 1);

client/src/actions/action-hooks.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from "react";
2+
import { useDispatch } from "react-redux";
3+
4+
import { setClubs, setSportFields } from "./actions";
5+
6+
export function useSetClubs() {
7+
const dispatch = useDispatch();
8+
return React.useCallback(clubs => dispatch(setClubs(clubs)), [dispatch]);
9+
}
10+
11+
export function useSetSportFields() {
12+
const dispatch = useDispatch();
13+
return React.useCallback(
14+
sportFields => dispatch(setSportFields(sportFields)),
15+
[dispatch]
16+
);
17+
}

client/src/actions/actions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function setClubs(clubs) {
2+
return { type: "SET_CLUBS", payload: clubs };
3+
}
4+
5+
export function setSportFields(sportFields) {
6+
return { type: "SET_SPORTFIELDS", payload: sportFields };
7+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/** @jsx jsx */
2+
import React from "react";
3+
import { jsx } from "@emotion/core";
4+
5+
function OwnerCreateButton() {
6+
const [active, setActive] = React.useState(false);
7+
8+
const styleButtonContainer = {
9+
position: "fixed",
10+
bottom: "2em",
11+
right: "1em",
12+
background: "#C3C4C3",
13+
width: "3em",
14+
height: "3em",
15+
display: "flex",
16+
justifyContent: "center",
17+
alignItems: "center",
18+
borderRadius: "50%",
19+
20+
transition: "all 0.2s ease"
21+
};
22+
23+
const styleIcon = {
24+
background: "none",
25+
color: "inherit",
26+
border: "none",
27+
padding: "0",
28+
font: "inherit",
29+
cursor: "pointer",
30+
outline: "inherit",
31+
fontSize: "2em",
32+
margin: 0,
33+
transition: "all 0.2s ease"
34+
};
35+
36+
const styleIconActive = {
37+
...styleIcon,
38+
transform: "rotate(45deg)",
39+
position: "absolute",
40+
bottom: "0.5em",
41+
right: "0.5em"
42+
};
43+
44+
const styleListContainer = {
45+
...styleButtonContainer,
46+
width: "12em",
47+
height: "10em",
48+
borderRadius: "15px"
49+
};
50+
51+
const styleButtonsContainer = {
52+
display: "none"
53+
};
54+
55+
const styleButtonsContainerActive = {
56+
...styleButtonsContainer,
57+
display: "block"
58+
};
59+
60+
const styleButton = {
61+
background: "none",
62+
color: "inherit",
63+
border: "none",
64+
padding: "0",
65+
font: "inherit",
66+
cursor: "pointer",
67+
outline: "inherit",
68+
fontSize: "1.3em",
69+
margin: 0,
70+
width: "100%",
71+
"&:hover": {
72+
background: "#a6a6a6"
73+
}
74+
};
75+
76+
function handleClick() {
77+
setActive(!active);
78+
}
79+
80+
return (
81+
<div css={active ? styleListContainer : styleButtonContainer}>
82+
<div css={active ? styleButtonsContainerActive : styleButtonsContainer}>
83+
<button css={styleButton}>Create Club</button>
84+
<button css={styleButton}>Create Sport Field</button>
85+
</div>
86+
<button css={active ? styleIconActive : styleIcon} onClick={handleClick}>
87+
+
88+
</button>
89+
</div>
90+
);
91+
}
92+
93+
export default OwnerCreateButton;

client/src/reducers/index.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
1-
const initialState = {
2-
appName: "Kampu"
1+
import { combineReducers } from "redux";
2+
3+
export const initialState = {
4+
appName: "Kampu",
5+
clubs: [],
6+
sportFields: [],
7+
favoriteClubs: [],
8+
user: {}
39
};
410

5-
function reducer(state = initialState, action) {
11+
export function clubsReducer(state = initialState.clubs, action) {
612
switch (action.type) {
13+
case "SET_CLUBS": {
14+
return action.payload;
15+
}
716
default: {
817
return state;
918
}
1019
}
1120
}
1221

22+
export function sportFieldsReducer(state = initialState.sportFields, action) {
23+
switch (action.type) {
24+
case "SET_SPORTFIELDS": {
25+
return action.payload;
26+
}
27+
default: {
28+
return state;
29+
}
30+
}
31+
}
32+
33+
const reducer = combineReducers({
34+
clubs: clubsReducer,
35+
sportFields: sportFieldsReducer
36+
});
37+
1338
export default reducer;

client/src/selectors/selectors.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useSelector, shallowEqual } from "react-redux";
2+
3+
function useClubs() {
4+
return useSelector(state => state.clubs, shallowEqual);
5+
}
6+
7+
function useSportFields() {
8+
return useSelector(state => state.sportFields, shallowEqual);
9+
}
10+
11+
export { useClubs, useSportFields };

client/src/services/club.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { apiUrl } from "../utils";
2+
3+
async function getClubs() {
4+
const response = await fetch(`${apiUrl}/clubs`, {
5+
credentials: "include"
6+
});
7+
8+
if (!response.ok) throw new Error(response.statusText);
9+
return response.json();
10+
}
11+
12+
export { getClubs };

0 commit comments

Comments
 (0)