Skip to content

Add/refresh data #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/param-builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const ParamBuilder = ( { title, params, values = {}, onChange } ) => {
parameter={ parameter }
id={ `param-${ paramKey }` }
name={ paramKey }
position="right"
position="bottom"
/>
</td>
</tr>
Expand Down
8 changes: 6 additions & 2 deletions src/components/results/header/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import ResultsViewSelector from '../results-view-selector';
import Refresh from './refresh';

import './style.css';

const RequestHeader = ( { result: { loading, request: { path, method, apiName, version }, duration, response }, view, onViewChange } ) => {
const RequestHeader = ( { result: { id, loading, request: { path, method, apiName, version }, duration, response }, view, onViewChange } ) => {
const filename = path
.replace( /^\//, '' )
.replace( /\//g, '-' ) + '.json';
Expand All @@ -25,7 +26,10 @@ const RequestHeader = ( { result: { loading, request: { path, method, apiName, v
</span>
) }
{ duration && (
<span className="duration">{ `${ duration }ms` }</span>
<div>
<span className="duration">{`${ duration }ms`}</span>
<Refresh id={ id } />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small icon to trigger the refresh of the row

</div>
) }
{ response && !! response.body && (
<div>
Expand Down
19 changes: 19 additions & 0 deletions src/components/results/header/refresh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { connect } from 'react-redux';

import { refresh } from '../../../state/results/actions';

const Refresh = ( { refreshResult } ) => (
<span
onClick={ refreshResult }
className="refresh"
title="Refresh"
>
<i className="refresh-icon">↺</i>
</span> );

const mapDispatchToProps = ( dispatch, { id } ) => {
return { refreshResult: () => dispatch( refresh( id ) ) };
};

export default connect( undefined, mapDispatchToProps )( Refresh );
14 changes: 14 additions & 0 deletions src/components/results/header/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@
content: "\1F4CB\FE0E";
}

.request-header .refresh {
padding: 2px 4px 0;
font-size: 18px;
border-radius: 2px;
text-decoration: none;
display: inline-block;
vertical-align: middle;
margin: 2px 0 2px 8px;
border: 1px solid #95b7d0;
color: #95b7d0;
height: 18px;
line-height: 12px;
}

.request-header .error {
white-space: nowrap;
font-size: 12px;
Expand Down
41 changes: 33 additions & 8 deletions src/state/results/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getRequestMethod, getCompleteQueryUrl, getBodyParams } from '../request
import { getSelectedApi, getSelectedVersion } from '../ui/selectors';
import { get } from '../../api';
import { getResponseStatus, getResponseError } from '../../lib/api';
import { getResultById } from './selectors';

window.responses = [];
const recordResponse = response => {
Expand Down Expand Up @@ -44,10 +45,10 @@ const receiveResults = ( {
};
};

const triggerRequest = ( { id, version, apiName, method, path } ) => {
const triggerRequest = ( { id, request } ) => {
return {
type: REQUEST_TRIGGER,
payload: { id, version, apiName, method, path },
payload: { id, request },
};
};

Expand All @@ -60,16 +61,13 @@ export const request = () => ( dispatch, getState ) => {
const api = get( apiName );
const body = getBodyParams( state );
const start = new Date().getTime();
const request = api.buildRequest( version, method, path, body );
const request = { apiName, api, version, method, path, body };
dispatch( triggerRequest( {
id: start,
version,
apiName,
method,
path,
request,
} ) );

return api.authProvider.request( request )
return api.authProvider.request( api.buildRequest( request.version, request.method, request.path, request.body ) )
.then( ( { status, body, error } ) => {
const end = new Date().getTime();
dispatch( receiveResults( {
Expand All @@ -85,3 +83,30 @@ export const request = () => ( dispatch, getState ) => {
} ) );
} );
};

export const refresh = id => ( dispatch, getState ) => {
const state = getState();
const result = getResultById( state, id );
const request = result.request;
const start = new Date().getTime();
dispatch( triggerRequest( {
id,
request,
} ) );

return request.api.authProvider.request( request.api.buildRequest( request.version, request.method, request.path, request.body ) )
.then( ( { status, body, error } ) => {
const end = new Date().getTime();
dispatch( receiveResults( {
id,
apiVersion: request.apiVersion,
apiName: request.apiName,
method: request.method,
path: request.path,
status: getResponseStatus( status, body, error ),
body,
error: getResponseError( status, body, error, true ),
duration: end - start,
} ) );
} );
};
4 changes: 2 additions & 2 deletions src/state/results/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { createReducer } from '../../lib/redux/create-reducer';
import { REQUEST_RESULTS_RECEIVE, REQUEST_TRIGGER } from '../actions';

const reducer = createReducer( {}, {
[ REQUEST_TRIGGER ]: ( state, { payload: { id, version, apiName, method, path } } ) => {
[ REQUEST_TRIGGER ]: ( state, { payload: { id, request } } ) => {
return ( {
...state,
[ id ]: { id, loading: true, request: { version, apiName, method, path } },
[ id ]: { id, loading: true, request },
} );
},
[ REQUEST_RESULTS_RECEIVE ]: ( state, { payload: { id, status, body, error, duration } } ) => {
Expand Down
5 changes: 5 additions & 0 deletions src/state/results/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ export const getResults = state => {
.map( key => results[ key ] )
.sort( ( a, b ) => b.id - a.id );
};

export const getResultById = ( state, id ) => {
const results = getResults( state );
return results.filter( result => result.id === id )[ 0 ];
};