Skip to content
This repository has been archived by the owner on Sep 8, 2021. It is now read-only.

Commit

Permalink
post and get of character working
Browse files Browse the repository at this point in the history
  • Loading branch information
uinstinct committed Aug 21, 2020
1 parent 3bc3cb7 commit 3d59d36
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 14 deletions.
Binary file modified .vs/card_builder/v16/.suo
Binary file not shown.
Binary file modified .vs/slnx.sqlite
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Components/Card.css
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
color: white;
font-size: 100px;
position: relative;
width: 400px;
width: 350px;
margin: 0 auto;
font-family: 'Bangers', cursive;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Components/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ function randomColour() {
}

function Card(props) {
let { name, shows, abilities, img } = props;
let { name, shows, abilities, img, id } = props;
abilities = Array.isArray(abilities) ? abilities : abilities.split(',');
let listAbilities = [];
let listAbilities = [], counter = 0;
for (const ability of abilities) {
const listAbility =
<label className={"class-badge badge-" + randomColour()}>{ability}</label>
<label key={counter++} className={"class-badge badge-" + randomColour()}>{ability}</label>
listAbilities.push(listAbility);
}
return (
<div className={"character-card-v2 card-"+randomColour()}>
<div className={"character-card-v2 card-" + randomColour()}>
<div className="portrait" style={{ backgroundImage: 'url(' + img + ')' }}>
</div>
<div className="details">
Expand Down
12 changes: 10 additions & 2 deletions src/Containers/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function produceGrid(n, handleClick, cards) {
for (let i = 0; i < n; i++) {
let column =
<Grid.Column floated='left' key={cards[i].id} onClick={handleClick} >
<BuiltCard {...cards[i]} />
<BuiltCard {...cards[i]} key={cards[i].id} />
</Grid.Column>;
grid.push(column);
}
Expand All @@ -44,8 +44,16 @@ class Main extends Component {
render() {
const { handleClick, cards } = this.props;
let grid = produceGrid(cards.length, handleClick, cards);

const inputs = {
name: 'last one',
abilities: 'hit,shot',
shows: 'agent 7',
img: 'goimg'
}
return (
<div className='container'>
<div className='main container'>
<Button onClick={() => this.props.doPostOne(inputs)}>CLICK THIS</Button>
<Grid stackable columns={3} centered>
{grid.map(g => g)}
</Grid>
Expand Down
2 changes: 1 addition & 1 deletion src/Containers/stylesheets/Main.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.container{
.main.container{
margin:0 1.5rem;
margin-bottom: 2rem;
}
23 changes: 22 additions & 1 deletion src/Services/apiCalls.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,25 @@ const BASE_URL = "http://localhost:9000/";

export function getAll() {
return axios.get(BASE_URL);
}
}

export function postOne(data) {
return axios({
method: 'POST',
url: BASE_URL+'characters',
data: data
})
}

export function getOne(id) {
console.log(id, 'is the value of id');
return axios.get(BASE_URL + id);
}

async function test() {
getOne(51)
.then(resp => resp.data)
.then(data => console.log(data))
.catch(e=>console.log(e))
}
test();
6 changes: 5 additions & 1 deletion src/Store/actionTypes.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export const GET_ALL = 'GET ALL';
export const UPDATE_ONE = 'UPDATE ONE CHARACTER';
export const UPDATE_ONE = 'UPDATE ONE CHARACTER';
export const GET_ONE = 'GET ONE';
export const POST_ONE = 'POST ONE';

export const ERR = 'AN ERROR OCCURRED';
9 changes: 7 additions & 2 deletions src/Store/actions/actionCreators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { GET_ALL, UPDATE_ONE } from '../actionTypes';

import * as apicalls from '../../Services/apiCalls';

export function doGetAll() {
import doGetAll from './doGetAll';
import doPostOne from './doPostOne';

/*export function doGetAll() {
return (dispatch, getState) => { // we are returning a function
console.log('we have getState as', getState()); // notice getState is a function
//let num = 50 + getState().num; // be careful of the name of the variables inside the state
Expand All @@ -12,4 +15,6 @@ export function doGetAll() {
.catch(e => console.log(e));
}
}
}*/

export { doGetAll, doPostOne };
13 changes: 13 additions & 0 deletions src/Store/actions/doGetAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getAll } from '../../Services/apiCalls';
import { GET_ALL, UPDATE_ONE } from '../actionTypes';

export default function doGetAll() {
return (dispatch, getState) => { // we are returning a function
console.log('we have getState as', getState()); // notice getState is a function
//let num = 50 + getState().num; // be careful of the name of the variables inside the state
getAll()
.then(response => response.data)
.then(data => dispatch({ type: GET_ALL, payload: data }))
.catch(e => console.log(e));
}
}
28 changes: 28 additions & 0 deletions src/Store/actions/doPostOne.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { POST_ONE, GET_ONE, ERR } from '../actionTypes';

import { postOne, getOne } from '../../Services/apiCalls';

export default function doPostOne(inputs) {
return async (dispatch, getState) => {
const resPostOne = await postOne(inputs).catch(e => console.log(e, 'at 1'));
const dataPostOne = resPostOne.data;

if ('code' in dataPostOne && dataPostOne.code === '23505')
return dispatch({ type: ERR, payload: "That Character already exists!" });
else if ('code' in dataPostOne)
return dispatch({ type: ERR, payload: "Oops! Something went wrong while submitting your Character." })

const character_id = dataPostOne.character_id;
console.log('got tha char id as', character_id, dataPostOne);

if (character_id) {
await new Promise(resolve => setTimeout(resolve, 1000)); // needs at least 1 second to get the character
console.log(character_id, 'is the character id');
const resp = await getOne(character_id).catch('Please check your network connectivity!');
const newCharacter = resp.data;
console.log('got this', newCharacter);
return dispatch({ type: POST_ONE, payload: { newCharacter, cards: getState().cards } });
}else console.log('did not happen')

}
}
1 change: 0 additions & 1 deletion src/Store/reducers/getAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export default function (state = initialState, action) {
const newState = action.payload;
return newState;
default:
console.log('no case matched');
return state;
}
}
15 changes: 15 additions & 0 deletions src/Store/reducers/getOne.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { GET_ONE } from '../actionTypes';

const initialState = ''; // acts as placeholder

export default function (state = initialState, action) {
switch (action.type) {
case GET_ONE:
console.log('at getone, we have the intial state as ', state, '\n and the action as ', action);
const { cards, newCard } = action.payload;
const newState = cards.unshift(newCard);
return newState;
default:
return state;
}
}
13 changes: 13 additions & 0 deletions src/Store/reducers/postOne.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { POST_ONE } from '../actionTypes';

const initialState = [];

export default function (state = initialState, action) {
switch (action.type) {
case POST_ONE: console.log('reached postone', action, state);
return state;
break;
default:
return state;
}
}
3 changes: 2 additions & 1 deletion src/Store/reducers/rootReducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { combineReducers } from 'redux';

import getAll from './getAll';
import postOne from './postOne';

const rootReducer = combineReducers({ cards : getAll }); // getAll is the name of the reducer and we are changing it's name to num
const rootReducer = combineReducers({ cards : getAll, postOne }); // getAll is the name of the reducer and we are changing it's name to num

export default rootReducer;

0 comments on commit 3d59d36

Please sign in to comment.