Skip to content
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

Fix: Persist snippet tabs #1958

Merged
merged 9 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
7 changes: 2 additions & 5 deletions src/app/services/actions/query-action-creator-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,11 @@ function createAuthenticatedRequest(
authProvider,
msalAuthOptions
)
const graphRequest = GraphClient.getInstance()
return GraphClient.getInstance()
.api(encodeHashCharacters(query))
.middlewareOptions([middlewareOptions])
.headers(sampleHeaders)
.responseType(ResponseType.RAW);

return graphRequest;
}

export function makeGraphRequest(scopes: string[]): Function {
Expand Down Expand Up @@ -212,8 +210,7 @@ export async function generateResponseDownloadUrl(
if (fileContents) {
const buffer = await response.arrayBuffer();
const blob = new Blob([buffer], { type: contentType });
const downloadUrl = URL.createObjectURL(blob);
return downloadUrl;
return URL.createObjectURL(blob);
}
} catch (error) {
return null;
Expand Down
10 changes: 9 additions & 1 deletion src/app/services/actions/snippet-action-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { parseSampleUrl } from '../../utils/sample-url-generation';
import {
GET_SNIPPET_ERROR,
GET_SNIPPET_PENDING,
GET_SNIPPET_SUCCESS
GET_SNIPPET_SUCCESS,
SET_SNIPPET_TAB_SUCCESS
} from '../redux-constants';

export function getSnippetSuccess(response: string): IAction {
Expand All @@ -28,6 +29,13 @@ export function getSnippetPending(): any {
};
}

export function setSnippetTabSuccess(response: string): any {
return {
type: SET_SNIPPET_TAB_SUCCESS,
response
}
}

export function getSnippet(language: string): Function {
return async (dispatch: Function, getState: Function) => {
const { devxApi, sampleQuery } = getState();
Expand Down
16 changes: 14 additions & 2 deletions src/app/services/reducers/snippet-reducer.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
import { IAction } from '../../../types/action';
import { GET_SNIPPET_ERROR, GET_SNIPPET_PENDING, GET_SNIPPET_SUCCESS } from '../redux-constants';
import {
GET_SNIPPET_ERROR, GET_SNIPPET_PENDING, GET_SNIPPET_SUCCESS,
SET_SNIPPET_TAB_SUCCESS
} from '../redux-constants';
import { ISnippet } from '../../../types/snippets';

const initialState: ISnippet = {
pending: false,
data: {},
error: null
error: null,
snippetTab: 'csharp'
};

export function snippets(state = initialState, action: IAction): any {
switch (action.type) {
case GET_SNIPPET_SUCCESS:
return {
...state,
pending: false,
data: action.response as object,
error: null
};
case GET_SNIPPET_ERROR:
return {
...state,
pending: false,
data: null,
error: action.response as object
};
case GET_SNIPPET_PENDING:
return {
...state,
pending: true,
data: null,
error: null
};
case SET_SNIPPET_TAB_SUCCESS:
return {
...state,
snippetTab: action.response
}
default:
return state;
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/services/redux-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export const GET_POLICY_ERROR = 'GET_POLICY_ERROR';
export const GET_POLICY_PENDING = 'GET_POLICY_PENDING';
export const RESOURCEPATHS_ADD_SUCCESS = 'RESOURCEPATHS_ADD_SUCCESS';
export const RESOURCEPATHS_DELETE_SUCCESS = 'RESOURCEPATHS_DELETE_SUCCESS';
export const BULK_ADD_HISTORY_ITEMS_SUCCESS = 'BULK_ADD_HISTORY_ITEMS_SUCCESS'
export const BULK_ADD_HISTORY_ITEMS_SUCCESS = 'BULK_ADD_HISTORY_ITEMS_SUCCESS';
export const SET_SNIPPET_TAB_SUCCESS = 'SET_SNIPPET_TAB_SUCCESS';
6 changes: 3 additions & 3 deletions src/app/views/query-response/QueryResponse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { sanitizeQueryUrl } from '../../utils/query-url-sanitization';
import { expandResponseArea } from '../../services/actions/response-expanded-action-creator';
import { translateMessage } from '../../utils/translate-messages';
import { copy } from '../common/copy';
import { getPivotItems, onPivotItemClick } from './pivot-items/pivot-items';
import { GetPivotItems, onPivotItemClick } from './pivot-items/pivot-items';
import './query-response.scss';
import { IRootState } from '../../../types/root';
import { CopyButton } from '../common/copy/CopyButton';
Expand Down Expand Up @@ -124,7 +124,7 @@ const QueryResponse = (props: IQueryResponseProps) => {
className={'pivot-response'}
selectedKey={currentTab}
>
{getPivotItems()}
{GetPivotItems()}
<PivotItem
headerText='Expand'
key='expand'
Expand Down Expand Up @@ -158,7 +158,7 @@ const QueryResponse = (props: IQueryResponseProps) => {
<Pivot className='pivot-response'
onLinkClick={(pivotItem) => onModalPivotItemClicked(pivotItem)}
selectedKey={currentTab}>
{getPivotItems()}
{GetPivotItems()}
</Pivot>
</Modal>
}
Expand Down
3 changes: 1 addition & 2 deletions src/app/views/query-response/pivot-items/pivot-items.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { queryResponseStyles } from '../queryResponse.styles';
import { Response } from '../response';
import { Snippets } from '../snippets';

export const getPivotItems = () => {
export const GetPivotItems = () => {
millicentachieng marked this conversation as resolved.
Show resolved Hide resolved

const { graphExplorerMode: mode, sampleQuery, graphResponse: { body } } = useSelector((state: IRootState) => state);

Expand Down Expand Up @@ -90,7 +90,6 @@ export const getPivotItems = () => {
<div id={'response-headers-tab'}><ResponseHeaders/></div>
</PivotItem>
];

if (mode === Mode.Complete) {
pivotItems.push(
<PivotItem
Expand Down
22 changes: 20 additions & 2 deletions src/app/views/query-response/snippets/Snippets.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Pivot } from '@fluentui/react';
import { Pivot, PivotItem } from '@fluentui/react';
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { componentNames, telemetry } from '../../../../telemetry';
import { IRootState } from '../../../../types/root';
import { setSnippetTabSuccess } from '../../../services/actions/snippet-action-creator';
import { renderSnippets } from './snippets-helper';

function GetSnippets() {
const dispatch = useDispatch();
const { snippets } = useSelector((state: IRootState) => state);
const supportedLanguages = {
'CSharp': {
sdkDownloadLink: 'https://aka.ms/csharpsdk',
Expand All @@ -28,7 +33,20 @@ function GetSnippets() {
}
};

return <Pivot>{renderSnippets(supportedLanguages)}</Pivot>;
const handlePivotItemClick = (pivotItem?: PivotItem) => {
if (!pivotItem) {
return;
}
dispatch(setSnippetTabSuccess(pivotItem.props.itemKey!))
}

return <Pivot
className={'pivot-response'}
selectedKey={snippets.snippetTab}
onLinkClick={handlePivotItemClick}
>
{renderSnippets(supportedLanguages)}
</Pivot>;
}
export const Snippets = telemetry.trackReactComponent(
GetSnippets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function renderSnippets(supportedLanguages: ISupportedLanguages) {
headerButtonProps={{
'aria-controls': `${language}-tab`
}}
itemKey={language}
>
<Snippet language={language} snippetInfo={supportedLanguages} />
</PivotItem>
Expand Down
1 change: 1 addition & 0 deletions src/types/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface ISnippet extends IApiResponse {
pending: boolean;
data: any;
error: any | null;
snippetTab?: string;
}