Skip to content

Commit

Permalink
refactoring types
Browse files Browse the repository at this point in the history
Signed-off-by: Ramakrishna Chilaka <[email protected]>
  • Loading branch information
RamakrishnaChilaka committed Mar 25, 2024
1 parent 2c37acf commit 078bd72
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 29 deletions.
3 changes: 2 additions & 1 deletion public/pages/Aliases/containers/Aliases/Aliases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const defaultFilter = {

class Aliases extends MDSEnabledComponent<AliasesProps, AliasesState> {
static contextType = CoreServicesContext;

constructor(props: AliasesProps) {
super(props);
const {
Expand Down Expand Up @@ -482,7 +483,7 @@ class Aliases extends MDSEnabledComponent<AliasesProps, AliasesState> {
}
}

export default function AliasContainer(props: Omit<AliasesProps, "commonService">) {
export default function AliasContainer(props: Omit<AliasesProps, "commonService" | keyof DataSourceMenuProperties>) {
const context = useContext(ServicesContext);
const dataSourceMenuProps = useContext(DataSourceMenuContext);
return <Aliases {...props} commonService={context?.commonService as CommonService} {...dataSourceMenuProps} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class ComposableTemplates extends MDSEnabledComponent<ComposableTemplatesProps,
}

export default function ComposableTemplatesContainer(
props: Omit<ComposableTemplatesProps, "commonService" | "loading" | "componentMapTemplate">
props: Omit<ComposableTemplatesProps, "commonService" | "loading" | "componentMapTemplate" | keyof DataSourceMenuProperties>
) {
const context = useContext(ServicesContext);
const dataSourceMenuProps = useContext(DataSourceMenuContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class CreateComposableTemplate extends Component<CreateComposableTemplateProps>
}
}

export default function (props: CreateComposableTemplateProps) {
export default function (props: Omit<CreateComposableTemplateProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
useUpdateUrlWithDataSourceProperties();
return <CreateComposableTemplate {...props} {...dataSourceMenuProps} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CreateDataStream extends Component<CreateDataStreamProps> {
}
}

export default function (props: CreateDataStreamProps) {
export default function (props: Omit<CreateDataStreamProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
useUpdateUrlWithDataSourceProperties();
return <CreateDataStream {...props} {...dataSourceMenuProps} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { RouteComponentProps } from "react-router-dom";
import IndexForm from "../IndexForm";
import { BREADCRUMBS, IndicesUpdateMode, ROUTES } from "../../../../utils/constants";
import { CoreServicesContext } from "../../../../components/core_services";
import { CommonService } from "../../../../services/index";
import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext";
import { useUpdateUrlWithDataSourceProperties } from "../../../../components/MDSEnabledComponent";

interface CreateIndexProps extends RouteComponentProps<{ index?: string; mode?: IndicesUpdateMode }>, DataSourceMenuProperties {
interface CreateIndexPropsBase extends RouteComponentProps<{ index?: string; mode?: IndicesUpdateMode }> {
isEdit?: boolean;
commonService: CommonService;
}

interface CreateIndexProps extends CreateIndexPropsBase, DataSourceMenuProperties {}

export class CreateIndex extends Component<CreateIndexProps> {
static contextType = CoreServicesContext;

Expand Down Expand Up @@ -63,7 +63,7 @@ export class CreateIndex extends Component<CreateIndexProps> {
}
}

export default function (props: CreateIndexProps) {
export default function (props: CreateIndexPropsBase) {
const dataSourceMenuProperties = useContext(DataSourceMenuContext);
useUpdateUrlWithDataSourceProperties();
return <CreateIndex {...props} {...dataSourceMenuProperties} />;
Expand Down
4 changes: 3 additions & 1 deletion public/pages/CreateIndex/containers/IndexForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export const getAliasActionsByDiffArray = (
}, [] as IAliasAction[]);
};

export interface IndexFormProps extends Pick<IndexDetailProps, "readonly" | "sourceIndices">, DataSourceMenuProperties {
export interface IndexFormProps
extends Pick<IndexDetailProps, "readonly" | "sourceIndices">,
Pick<DataSourceMenuProperties, "dataSourceId"> {
index?: string;
value?: Partial<IndexItemRemote>;
mode?: IndicesUpdateMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import { CoreServicesContext } from "../../../../components/core_services";
import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext";
import { useUpdateUrlWithDataSourceProperties } from "../../../../components/MDSEnabledComponent";

interface CreateIndexTemplateProps extends RouteComponentProps<{ template?: string; mode?: string }>, DataSourceMenuProperties {}
interface CreateIndexTemplateProps
extends RouteComponentProps<{
template?: string;
mode?: string;
}>,
DataSourceMenuProperties {}

class CreateIndexTemplate extends Component<CreateIndexTemplateProps> {
static contextType = CoreServicesContext;
Expand Down Expand Up @@ -74,7 +79,7 @@ class CreateIndexTemplate extends Component<CreateIndexTemplateProps> {
}
}

export default function (props: CreateIndexTemplateProps) {
export default function (props: Omit<CreateIndexTemplateProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
useUpdateUrlWithDataSourceProperties();
return <CreateIndexTemplate {...props} {...dataSourceMenuProps} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ class DataStreams extends MDSEnabledComponent<DataStreamsProps, DataStreamsState
}
}

export default function DataStreamsContainer(props: Omit<DataStreamsProps, "commonService">) {
export default function DataStreamsContainer(props: Omit<DataStreamsProps, "commonService" | keyof DataSourceMenuProperties>) {
const context = useContext(ServicesContext);
const dataSourceMenuProperties = useContext(DataSourceMenuContext);
return <DataStreams {...props} commonService={context?.commonService as CommonService} {...dataSourceMenuProperties} />;
Expand Down
18 changes: 12 additions & 6 deletions public/pages/Indices/containers/Indices/Indices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ interface IndicesState extends DataSourceMenuProperties {

export class Indices extends MDSEnabledComponent<IndicesProps, IndicesState> {
static contextType = CoreServicesContext;

constructor(props: IndicesProps) {
super(props);
const { from, size, search, sortField, sortDirection, showDataStreams } = getURLQueryParams(this.props.location);
Expand Down Expand Up @@ -101,14 +102,14 @@ export class Indices extends MDSEnabledComponent<IndicesProps, IndicesState> {
}

async componentDidUpdate(prevProps: IndicesProps, prevState: IndicesState) {
const prevQuery = Indices.getQueryObjectFromState(prevState);
const currQuery = Indices.getQueryObjectFromState(this.state);
const prevQuery = this.getQueryObjectFromState(prevState);
const currQuery = this.getQueryObjectFromState(this.state);
if (!_.isEqual(prevQuery, currQuery)) {
await this.getIndices();
}
}

static getQueryObjectFromState({
getQueryObjectFromState({
from,
size,
search,
Expand All @@ -117,14 +118,19 @@ export class Indices extends MDSEnabledComponent<IndicesProps, IndicesState> {
showDataStreams,
dataSourceId,
}: IndicesState): IndicesQueryParams {
return { from, size, search, sortField, sortDirection, showDataStreams, dataSourceId };
const queryObj = { from, size, search, sortField, sortDirection, showDataStreams };
if (!this.props.multiDataSourceEnabled) {
// don't send dataSourceId, if MDS is not enabled
return queryObj;
}
return { ...queryObj, dataSourceId };
}

getIndices = async (): Promise<void> => {
this.setState({ loadingIndices: true });
try {
const { indexService, history } = this.props;
const queryObject = Indices.getQueryObjectFromState(this.state);
const queryObject = this.getQueryObjectFromState(this.state);
const queryParamsString = queryString.stringify({ ...queryObject, dataSourceLabel: this.state.dataSourceLabel });
history.replace({ ...this.props.location, search: queryParamsString });

Expand Down Expand Up @@ -313,7 +319,7 @@ export class Indices extends MDSEnabledComponent<IndicesProps, IndicesState> {
}
}

export default function (props: IndicesProps) {
export default function (props: Omit<IndicesProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
return <Indices {...props} {...dataSourceMenuProps} />;
}
2 changes: 1 addition & 1 deletion public/pages/Indices/models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export interface IndicesQueryParams {
sortField: keyof ManagedCatIndex;
sortDirection: Direction;
showDataStreams: boolean;
dataSourceId: string;
dataSourceId?: string;
}
10 changes: 5 additions & 5 deletions public/pages/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ export default class Main extends Component<MainProps, MainState> {
};

if (this.props.multiDataSourceEnabled && this.isDataSourceEnabledForPath(pathname)) {
services.indexService = new IndexService(http, this.state.dataSourceId);
services.commonService = new CommonService(http, this.state.dataSourceId);
services.indexService = new IndexService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled);
services.commonService = new CommonService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled);
}
return services;
}
Expand Down Expand Up @@ -311,7 +311,7 @@ export default class Main extends Component<MainProps, MainState> {
const { landingPage } = this.props;

const ROUTE_STYLE = { padding: "25px 25px" };
const DataSourceMenu = this.props.dataSourceManagement.ui.DataSourceMenu;
const DataSourceMenu = this.props.dataSourceManagement?.ui?.DataSourceMenu;

return (
<CoreServicesConsumer>
Expand Down Expand Up @@ -626,7 +626,7 @@ export default class Main extends Component<MainProps, MainState> {
path={`${ROUTES.CREATE_INDEX}/:index/:mode`}
render={(props: RouteComponentProps) => (
<div style={ROUTE_STYLE}>
<CreateIndex {...props} commonService={services.commonService} />
<CreateIndex {...props} />
</div>
)}
/>
Expand All @@ -642,7 +642,7 @@ export default class Main extends Component<MainProps, MainState> {
path={ROUTES.CREATE_INDEX}
render={(props: RouteComponentProps) => (
<div style={ROUTE_STYLE}>
<CreateIndex {...props} commonService={services.commonService} />
<CreateIndex {...props} />
</div>
)}
/>
Expand Down
2 changes: 1 addition & 1 deletion public/pages/Templates/containers/Templates/Templates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class Templates extends MDSEnabledComponent<TemplatesProps, TemplatesState> {
}
}

export default function TemplatesContainer(props: Omit<TemplatesProps, "commonService">) {
export default function TemplatesContainer(props: Omit<TemplatesProps, "commonService" | keyof DataSourceMenuProperties>) {
const context = useContext(ServicesContext);
const dataSourceMenuProps = useContext(DataSourceMenuContext);
return <Templates {...props} commonService={context?.commonService as CommonService} {...dataSourceMenuProps} />;
Expand Down
12 changes: 8 additions & 4 deletions public/services/MDSEnabledClientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import { HttpFetchQuery, HttpSetup } from "opensearch-dashboards/public";
export abstract class MDSEnabledClientService {
httpClient: HttpSetup;
dataSourceId: string;
mdsEnabled: boolean;

constructor(httpClient: HttpSetup, dataSourceId: string = "") {
constructor(httpClient: HttpSetup, dataSourceId: string = "", mdsEnabled: boolean = false) {
this.httpClient = httpClient;
this.dataSourceId = dataSourceId;
this.mdsEnabled = mdsEnabled;
}

patchQueryObjectWithDataSourceId(queryObject: HttpFetchQuery) {
queryObject = queryObject || {};
queryObject.dataSourceId = this.dataSourceId;
patchQueryObjectWithDataSourceId(queryObject: HttpFetchQuery): HttpFetchQuery {
if (this.mdsEnabled) {
queryObject = queryObject || {};
queryObject.dataSourceId = this.dataSourceId;
}
return queryObject;
}
}

0 comments on commit 078bd72

Please sign in to comment.