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
export const http = async (request: RequestInfo): Promise<any> => { | |
return new Promise(resolve => { | |
fetch(request) | |
.then(response => response.json()) | |
.then(body => { | |
resolve(body); | |
}); | |
}); | |
}; |
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
export const http = <T>(request: RequestInfo): Promise<T> => { | |
return new Promise((resolve) => { | |
fetch(request) | |
.then(response => response.json()) | |
.then(body => { | |
resolve(body); | |
}); | |
}); | |
}; | |
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
interface IHttpResponse<T> extends Response { | |
parsedBody?: T; | |
} | |
export const http = <T>(request: RequestInfo): Promise<IHttpResponse<T>> => { | |
let response: IHttpResponse<T>; | |
return new Promise(resolve => { | |
fetch(request) | |
.then(res => { | |
response = res; | |
return res.json(); |
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
export interface IHttpResponse<T> extends Response { | |
parsedBody?: T; | |
} | |
export const http = <T>(request: RequestInfo): Promise<IHttpResponse<T>> => { | |
return new Promise((resolve, reject) => { | |
let response: IHttpResponse<T>; | |
fetch(request) | |
.then(res => { | |
response = res; |
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
const response = await http<{ id: number }>( | |
new Request("https://jsonplaceholder.typicode.com/posts", { | |
method: "post", | |
body: JSON.stringify({ title: "my post", body: "some content" }) | |
}) | |
); |
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
interface IPeopleState { | |
readonly people: IPerson[]; | |
readonly loading: boolean; | |
readonly posting: boolean; | |
} | |
export interface IAppState { | |
readonly peopleState: IPeopleState; | |
} |
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
export interface IGettingPeopleAction extends Action<'GettingPeople'> {} | |
export interface IGotPeopleAction extends Action<'GotPeople'> { | |
people: IPerson[]; | |
} | |
export interface IPostingPersonAction extends Action<'PostingPerson'> { | |
type: 'PostingPerson'; | |
} |
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
const peopleReducer: Reducer<IPeopleState, PeopleActions> = ( | |
state = initialPeopleState, | |
action, | |
) => { | |
switch (action.type) { | |
case 'GettingPeople': { | |
return { | |
...state, | |
loading: true, | |
}; |
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
export function configureStore(): Store<IAppState> { | |
const store = createStore(rootReducer, undefined, applyMiddleware(thunk)); | |
return store; | |
} |
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
interface IProps { | |
getPeople: () => Promise<IGotPeopleAction>; | |
people: IPerson[]; | |
peopleLoading: boolean; | |
postPerson: (person: IPostPerson) => Promise<IPostedPersonAction>; | |
personPosting: boolean; | |
} | |
const App: FC<IProps> = ({ | |
getPeople, |
OlderNewer