Persist the Network mock data (#1218)

Summary:
Persist the Network mock data, so it can survive after switching the plugins

Closes https://github.com/facebook/flipper/issues/1206
## Changelog

- Persist the Network mock data
- Remove the pref-filled '/' when creating a mock route
Pull Request resolved: https://github.com/facebook/flipper/pull/1218

Test Plan: <img width="807" alt="Screenshot 2020-06-02 at 11 45 15 PM" src="https://user-images.githubusercontent.com/410850/83540614-27e95000-a52b-11ea-8fc8-d7ad1373e904.png">

Reviewed By: mweststrate

Differential Revision: D21863561

Pulled By: passy

fbshipit-source-id: 4706ede721c7990a6bcc0bfe51f41e80306ffac7
This commit is contained in:
ZHANG Qichuan
2020-06-19 05:25:27 -07:00
committed by Facebook GitHub Bot
parent 7a1c9aadb9
commit ae9c07c8f3
2 changed files with 24 additions and 11 deletions

View File

@@ -129,7 +129,13 @@ function RouteRow(props: {
{props.showWarning && ( {props.showWarning && (
<Icon name="caution-triangle" color={colors.yellow} /> <Icon name="caution-triangle" color={colors.yellow} />
)} )}
{props.text.length === 0 ? (
<TextEllipsis style={{color: colors.blackAlpha50}}>
untitled
</TextEllipsis>
) : (
<TextEllipsis>{props.text}</TextEllipsis> <TextEllipsis>{props.text}</TextEllipsis>
)}
</FlexRow> </FlexRow>
{showCloseButton && ( {showCloseButton && (
<FlexRow onClick={props.handleRemoveId}> <FlexRow onClick={props.handleRemoveId}>

View File

@@ -38,6 +38,8 @@ import {URL} from 'url';
import {DefaultKeyboardAction} from 'app/src/MenuBar'; import {DefaultKeyboardAction} from 'app/src/MenuBar';
import {MockResponseDialog} from './MockResponseDialog'; import {MockResponseDialog} from './MockResponseDialog';
const LOCALSTORAGE_MOCK_ROUTE_LIST_KEY = '__NETWORK_CACHED_MOCK_ROUTE_LIST';
type PersistedState = { type PersistedState = {
requests: {[id: string]: Request}; requests: {[id: string]: Request};
responses: {[id: string]: Response}; responses: {[id: string]: Response};
@@ -206,14 +208,18 @@ export default class extends FlipperPlugin<State, any, PersistedState> {
} }
init() { init() {
this.client.supportsMethod('mockResponses').then((result) => this.client.supportsMethod('mockResponses').then((result) => {
const routes = JSON.parse(
localStorage.getItem(LOCALSTORAGE_MOCK_ROUTE_LIST_KEY) || '{}',
);
this.setState({ this.setState({
routes: {}, routes: routes,
isMockResponseSupported: result, isMockResponseSupported: result,
showMockResponseDialog: false, showMockResponseDialog: false,
}), nextRouteId: routes.length,
); });
this.informClientMockChange({}); });
this.setState(this.parseDeepLinkPayload(this.props.deepLinkPayload)); this.setState(this.parseDeepLinkPayload(this.props.deepLinkPayload));
// declare new variable to be called inside the interface // declare new variable to be called inside the interface
@@ -225,7 +231,7 @@ export default class extends FlipperPlugin<State, any, PersistedState> {
produce((draftState: State) => { produce((draftState: State) => {
const nextRouteId = draftState.nextRouteId; const nextRouteId = draftState.nextRouteId;
draftState.routes[nextRouteId.toString()] = { draftState.routes[nextRouteId.toString()] = {
requestUrl: '/', requestUrl: '',
requestMethod: 'GET', requestMethod: 'GET',
responseData: '', responseData: '',
responseHeaders: {}, responseHeaders: {},
@@ -258,10 +264,7 @@ export default class extends FlipperPlugin<State, any, PersistedState> {
}; };
} }
teardown() { teardown() {}
// Remove mock response inside client
this.informClientMockChange({});
}
onKeyboardAction = (action: string) => { onKeyboardAction = (action: string) => {
if (action === 'clear') { if (action === 'clear') {
@@ -341,6 +344,10 @@ export default class extends FlipperPlugin<State, any, PersistedState> {
if (this.state.isMockResponseSupported) { if (this.state.isMockResponseSupported) {
const routesValuesArray = Object.values(filteredRoutes); const routesValuesArray = Object.values(filteredRoutes);
localStorage.setItem(
LOCALSTORAGE_MOCK_ROUTE_LIST_KEY,
JSON.stringify(routesValuesArray),
);
this.client.call('mockResponses', { this.client.call('mockResponses', {
routes: routesValuesArray.map((route: Route) => ({ routes: routesValuesArray.map((route: Route) => ({
requestUrl: route.requestUrl, requestUrl: route.requestUrl,