From 3d59d3648400ac87a582b2229f7b420489a35228 Mon Sep 17 00:00:00 2001 From: uinstinct Date: Sat, 22 Aug 2020 04:10:41 +0530 Subject: [PATCH] post and get of character working --- .vs/card_builder/v16/.suo | Bin 40448 -> 40448 bytes .vs/slnx.sqlite | Bin 90112 -> 90112 bytes src/Components/Card.css | 2 +- src/Components/Card.js | 8 ++++---- src/Containers/Main.js | 12 ++++++++++-- src/Containers/stylesheets/Main.css | 2 +- src/Services/apiCalls.js | 23 ++++++++++++++++++++++- src/Store/actionTypes.js | 6 +++++- src/Store/actions/actionCreators.js | 9 +++++++-- src/Store/actions/doGetAll.js | 13 +++++++++++++ src/Store/actions/doPostOne.js | 28 ++++++++++++++++++++++++++++ src/Store/reducers/getAll.js | 1 - src/Store/reducers/getOne.js | 15 +++++++++++++++ src/Store/reducers/postOne.js | 13 +++++++++++++ src/Store/reducers/rootReducer.js | 3 ++- 15 files changed, 121 insertions(+), 14 deletions(-) create mode 100644 src/Store/actions/doGetAll.js create mode 100644 src/Store/actions/doPostOne.js create mode 100644 src/Store/reducers/getOne.js create mode 100644 src/Store/reducers/postOne.js diff --git a/.vs/card_builder/v16/.suo b/.vs/card_builder/v16/.suo index b9cf8f5fae736a33dd194b18006309fd60a252b0..4387a546ea7f36a6c876a253601fa882004a2602 100644 GIT binary patch delta 2461 zcmdUxeN0nV6u{qo6e-YFl!9QPw)6>DP@q)4q@~3wA1a9WwTU{%hp0plLHuA76^O~$ z20c?)HiMfkMyE6O4dxa`iaN8zs7M@S!saJ_Y;L*L_gd4z)^xCq$;9`RE{c_OC)aIYxAv0B3=TbPXSb85{4g6qb_Sv0Z=1~~LpcqZ zgh&wqB1gQD2n1d1LxppHM2L7IQxGpC1_?lv$UMXk2}WpaACyy(KqM035JxzrO@WFU z5h3G|83?@?g)$uBt%Bo3V(>Ec3;vbBI80z7`utY{G=^&e^v2wgP~>F^&~>f}P~DjT zojVht$>6?9TNqAWo`CQ;3ngOF%IoNPV{Do27RuYmJ!BBMi_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qhM8RchFJww9me<})DMr=FFd-Bm+W;Q^P4 zxG2V4{fu3W3^1t~<&g=dm_-C+fxBeF8f2D8^8F#)*6^BeVtr#}<`=-YeV&_XNDJj` z1p%?C@tF#_xg|_Dk7sb-xOH^_zs9X_OXB0^{hX0T5Q1S4RWg#uAP6fN5`T7OL*!FmdLEI>fc5S2G)YNP>3nQOHE_fY(GUiZSmheO@39FhxkZI+HZPRHtDO$tEpu zx0W0r?CijtB z6b0i5=M=0IfGk0QD3U(MFfn)km{6vmh>WyrUF&h7R6$`GnJd2ji3lYM3dtyKa?09{ zv!<(#J9NvgOHYL&rF|&3g#olNC72a7DkIh9{?BjSv6Pq1N&zJNXOS;`lXHGFYxF1Q+$o&^iQp)5F|j%T9(bh zu5pZPuJ3tkx3z5E?$*o7=KB1#<~p;UNJ*6#|?B$o+^+j3486kGDGc@xwF>o@1ZM#xwpvElDCp?*bfj-dqyS$Ty*BRTpzBX$9 z!6K-_#=ykM$i~pe4D_de*Q*a5g33^#E|Ac)IX#mm2r5B^Izd9gnLD$4CbzDeCFvTh z)VFQBpaN936(p;v&7v+e*{ability} + listAbilities.push(listAbility); } return ( -
+
diff --git a/src/Containers/Main.js b/src/Containers/Main.js index fd950c1..c83d88a 100644 --- a/src/Containers/Main.js +++ b/src/Containers/Main.js @@ -25,7 +25,7 @@ function produceGrid(n, handleClick, cards) { for (let i = 0; i < n; i++) { let column = - + ; grid.push(column); } @@ -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 ( -
+
+ {grid.map(g => g)} diff --git a/src/Containers/stylesheets/Main.css b/src/Containers/stylesheets/Main.css index 927a39b..030d297 100644 --- a/src/Containers/stylesheets/Main.css +++ b/src/Containers/stylesheets/Main.css @@ -1,4 +1,4 @@ -.container{ +.main.container{ margin:0 1.5rem; margin-bottom: 2rem; } \ No newline at end of file diff --git a/src/Services/apiCalls.js b/src/Services/apiCalls.js index 0853292..e5d83f1 100644 --- a/src/Services/apiCalls.js +++ b/src/Services/apiCalls.js @@ -4,4 +4,25 @@ const BASE_URL = "http://localhost:9000/"; export function getAll() { return axios.get(BASE_URL); -} \ No newline at end of file +} + +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(); \ No newline at end of file diff --git a/src/Store/actionTypes.js b/src/Store/actionTypes.js index 8700281..f852c6f 100644 --- a/src/Store/actionTypes.js +++ b/src/Store/actionTypes.js @@ -1,2 +1,6 @@ export const GET_ALL = 'GET ALL'; -export const UPDATE_ONE = 'UPDATE ONE CHARACTER'; \ No newline at end of file +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'; \ No newline at end of file diff --git a/src/Store/actions/actionCreators.js b/src/Store/actions/actionCreators.js index 1482236..83f2ba8 100644 --- a/src/Store/actions/actionCreators.js +++ b/src/Store/actions/actionCreators.js @@ -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 @@ -12,4 +15,6 @@ export function doGetAll() { .catch(e => console.log(e)); } -} \ No newline at end of file +}*/ + +export { doGetAll, doPostOne }; diff --git a/src/Store/actions/doGetAll.js b/src/Store/actions/doGetAll.js new file mode 100644 index 0000000..0d180fd --- /dev/null +++ b/src/Store/actions/doGetAll.js @@ -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)); + } +} \ No newline at end of file diff --git a/src/Store/actions/doPostOne.js b/src/Store/actions/doPostOne.js new file mode 100644 index 0000000..89de9a1 --- /dev/null +++ b/src/Store/actions/doPostOne.js @@ -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') + + } +} \ No newline at end of file diff --git a/src/Store/reducers/getAll.js b/src/Store/reducers/getAll.js index d95642a..1e6085c 100644 --- a/src/Store/reducers/getAll.js +++ b/src/Store/reducers/getAll.js @@ -9,7 +9,6 @@ export default function (state = initialState, action) { const newState = action.payload; return newState; default: - console.log('no case matched'); return state; } } \ No newline at end of file diff --git a/src/Store/reducers/getOne.js b/src/Store/reducers/getOne.js new file mode 100644 index 0000000..de8ffa6 --- /dev/null +++ b/src/Store/reducers/getOne.js @@ -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; + } +} \ No newline at end of file diff --git a/src/Store/reducers/postOne.js b/src/Store/reducers/postOne.js new file mode 100644 index 0000000..015f5c4 --- /dev/null +++ b/src/Store/reducers/postOne.js @@ -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; + } +} \ No newline at end of file diff --git a/src/Store/reducers/rootReducer.js b/src/Store/reducers/rootReducer.js index b1b2691..9881277 100644 --- a/src/Store/reducers/rootReducer.js +++ b/src/Store/reducers/rootReducer.js @@ -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; \ No newline at end of file