forked from green-fox-academy/jsa-bloodstone-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSAB2-67 Connect troops frontend to backend (green-fox-academy#22)
* JSAB2-67, add action creator and reducer * JSAB2-67, fetch troop in component * JASB2-67, add loader and error handler * JASB2-67, fix lint problem * JSAB2-67, remove env file * JSAB2-67, rename action * JSAB2-67, apply dotenv * JSAB2-67, fix lint problem * JSAB2-67, rename const url to uppercase * JSAB2-67, move url definition * JSAB2-67, fix lint problem * JSAB2-67, fix troop info not updating * JSAB2-67, add more troops in endpoint * JSAB2-67, improve string style * JSAB2-67, fix spelling Co-Authored-By: evasimonyi <30794349+evasimonyi@users.noreply.github.com> * JSAB2-67, fix spelling Co-Authored-By: evasimonyi <30794349+evasimonyi@users.noreply.github.com> * JSAB2-67, fix spelling Co-Authored-By: evasimonyi <30794349+evasimonyi@users.noreply.github.com> * JSAB2-67, fix spelling Co-Authored-By: evasimonyi <30794349+evasimonyi@users.noreply.github.com> * JSAB2-67, change initial state of isLoading Co-Authored-By: evasimonyi <30794349+evasimonyi@users.noreply.github.com> * JSAB2-67, use destructuring for state Co-Authored-By: evasimonyi <30794349+evasimonyi@users.noreply.github.com> * JSAB2-67, remove unnecessary state Co-Authored-By: evasimonyi <30794349+evasimonyi@users.noreply.github.com> * JSAB2-67, use destructuring for state Co-Authored-By: evasimonyi <30794349+evasimonyi@users.noreply.github.com> * JSAB2-67, fix spelling * JSAB2-67, adjust fetch error * JSAB2-67, change naming * JSAB2-67, improve error message to user * JSAB2-67, fix mocked troops id * JSAB2-67, adjust reducer flow and use common color * JSAB2-67, change naming * JSAB2-67, fix naming * JSAB2-67, change naming * JSAB2-67, change error text Co-Authored-By: evasimonyi <30794349+evasimonyi@users.noreply.github.com>
- Loading branch information
1 parent
5b69089
commit b591f24
Showing
6 changed files
with
147 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { SERVER_URL } from 'react-native-dotenv'; | ||
|
||
export const FETCH_TROOPS_SUCCESS = 'fetchTroopsSuccess'; | ||
export const FETCH_TROOPS_REQUEST = 'fetchTroopsRequest'; | ||
export const FETCH_TROOPS_FAILURE = 'fetchTroopsFailure'; | ||
|
||
const URL = `http://${SERVER_URL}/kingdom/troops`; | ||
|
||
export function fetchTroops() { | ||
return (dispatch) => { | ||
dispatch({ type: FETCH_TROOPS_REQUEST }); | ||
fetch(URL) | ||
.then((response) => { | ||
if (response.status === 200) { | ||
return response.json(); | ||
} | ||
throw new Error(response.status); | ||
}) | ||
.then((response) => dispatch({ type: FETCH_TROOPS_SUCCESS, payload: response.troops })) | ||
.catch((error) => dispatch({ type: FETCH_TROOPS_FAILURE, payload: error })); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { FETCH_TROOPS_SUCCESS, FETCH_TROOPS_REQUEST, FETCH_TROOPS_FAILURE } from './actionCreator'; | ||
|
||
const initialState = { | ||
listOfTroops: [], | ||
isLoading: false, | ||
error: undefined, | ||
}; | ||
|
||
export default function troop(state = initialState, action) { | ||
switch (action.type) { | ||
case FETCH_TROOPS_REQUEST: | ||
return { ...state, isLoading: true }; | ||
case FETCH_TROOPS_SUCCESS: | ||
return { ...state, listOfTroops: action.payload, isLoading: false }; | ||
case FETCH_TROOPS_FAILURE: | ||
return { ...state, isLoading: false, error: action.payload }; | ||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters