@@ -2,7 +2,7 @@ import { Dispatch } from "redux";
22import { createAction , handleActions } from "redux-actions" ;
33import axios from "axios" ;
44export type AddessFromPostalCodeState = {
5- address ?: string [ ] ;
5+ address ?: object [ ] ;
66} ;
77
88const API_POSTALCODE_ENDPOINT = "https://zipcloud.ibsnet.co.jp/api" ;
@@ -28,15 +28,19 @@ export const GetPostalCodeAction = createAction(GET_ADDRESS_REQUEST);
2828export const GetGAddressFromZipSuccessAction =
2929 createAction ( GET_ADDRESS_SUCCESS ) ;
3030
31- export const ClearPostalCodeAction = createAction < void > ( CLEAR_ADDRESS ) ;
31+ export const GetGAddressFromZipFailureAction =
32+ createAction ( GET_ADDRESS_FAILURE ) ;
33+
34+ export const ClearPostalCodeAction = createAction ( CLEAR_ADDRESS ) ;
3235
3336export const GetAddressFromZip =
3437 ( zipCode : string | number ) => async ( dispatch : Dispatch ) => {
35- dispatch ( GetPostalCodeAction ( ) ) ;
3638 try {
37- const result = await axios . get (
38- API_POSTALCODE_ENDPOINT + "/search?zipcode=" + zipCode
39- ) ;
39+ dispatch ( GetPostalCodeAction ( ) ) ;
40+ const result = await axios
41+ . get ( API_POSTALCODE_ENDPOINT + "/search?zipcode=" + zipCode )
42+ . catch ( GetGAddressFromZipFailureAction ( ) ) ;
43+
4044 if ( ! result ) {
4145 return ;
4246 }
@@ -47,6 +51,9 @@ export const GetAddressFromZip =
4751 dispatch ( GetGAddressFromZipSuccessAction ( { address : result . data } ) ) ;
4852 } catch ( e ) {
4953 console . error ( e ) ;
54+ dispatch (
55+ GetGAddressFromZipFailureAction ( { address : undefined , error : e } )
56+ ) ;
5057 }
5158 } ;
5259
@@ -73,12 +80,23 @@ export default handleActions(
7380 address,
7481 } ;
7582 } ,
76- // [CLEAR_ADDRESS]: (state) => {
77- // return {
78- // ...state,
79- // address: undefined,
80- // };
81- // },
83+ [ GET_ADDRESS_FAILURE ] : ( state , action ) => {
84+ const error = action . payload as Parameters <
85+ typeof GetGAddressFromZipFailureAction
86+ > [ 0 ] ;
87+
88+ return {
89+ ...state ,
90+ address : undefined ,
91+ error,
92+ } ;
93+ } ,
94+ [ CLEAR_ADDRESS ] : ( state ) => {
95+ return {
96+ ...state ,
97+ address : undefined ,
98+ } ;
99+ } ,
82100 } ,
83101 addressInitialState
84102) ;
0 commit comments