|
@@ -3,7 +3,7 @@ import React, { Component } from "react";
|
|
|
import { ProjectType, ClusterType, CapabilityType } from "shared/types";
|
|
import { ProjectType, ClusterType, CapabilityType } from "shared/types";
|
|
|
import { pushQueryParams } from "shared/routing";
|
|
import { pushQueryParams } from "shared/routing";
|
|
|
|
|
|
|
|
-const Context = React.createContext({});
|
|
|
|
|
|
|
+const Context = React.createContext<GlobalContextType>({} as GlobalContextType);
|
|
|
|
|
|
|
|
const { Provider } = Context;
|
|
const { Provider } = Context;
|
|
|
const ContextConsumer = Context.Consumer;
|
|
const ContextConsumer = Context.Consumer;
|
|
@@ -13,7 +13,31 @@ type PropsType = {
|
|
|
location: any;
|
|
location: any;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-type StateType = any;
|
|
|
|
|
|
|
+type StateType = GlobalContextType;
|
|
|
|
|
+
|
|
|
|
|
+export interface GlobalContextType {
|
|
|
|
|
+ currentModal: string;
|
|
|
|
|
+ currentModalData: any;
|
|
|
|
|
+ setCurrentModal: (currentModal: string, currentModalData?: any) => void;
|
|
|
|
|
+ currentError: string | null;
|
|
|
|
|
+ setCurrentError: (currentError: string) => void;
|
|
|
|
|
+ currentCluster: ClusterType;
|
|
|
|
|
+ setCurrentCluster: (currentCluster: ClusterType, callback?: any) => void;
|
|
|
|
|
+ currentProject: ProjectType | null;
|
|
|
|
|
+ setCurrentProject: (
|
|
|
|
|
+ currentProject: ProjectType,
|
|
|
|
|
+ callback?: () => void
|
|
|
|
|
+ ) => void;
|
|
|
|
|
+ projects: ProjectType[];
|
|
|
|
|
+ setProjects: (projects: ProjectType[]) => void;
|
|
|
|
|
+ user: any;
|
|
|
|
|
+ setUser: (userId: number, email: string) => void;
|
|
|
|
|
+ devOpsMode: boolean;
|
|
|
|
|
+ setDevOpsMode: (devOpsMode: boolean) => void;
|
|
|
|
|
+ capabilities: CapabilityType;
|
|
|
|
|
+ setCapabilities: (capabilities: CapabilityType) => void;
|
|
|
|
|
+ clearContext: () => void;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Component managing a universal (application-wide) data store.
|
|
* Component managing a universal (application-wide) data store.
|
|
@@ -27,13 +51,13 @@ type StateType = any;
|
|
|
* 4) As a rule of thumb, Context should not be used for UI-related state
|
|
* 4) As a rule of thumb, Context should not be used for UI-related state
|
|
|
*/
|
|
*/
|
|
|
class ContextProvider extends Component<PropsType, StateType> {
|
|
class ContextProvider extends Component<PropsType, StateType> {
|
|
|
- state = {
|
|
|
|
|
- currentModal: null as string | null,
|
|
|
|
|
- currentModalData: null as any,
|
|
|
|
|
|
|
+ state: GlobalContextType = {
|
|
|
|
|
+ currentModal: null,
|
|
|
|
|
+ currentModalData: null,
|
|
|
setCurrentModal: (currentModal: string, currentModalData?: any) => {
|
|
setCurrentModal: (currentModal: string, currentModalData?: any) => {
|
|
|
this.setState({ currentModal, currentModalData });
|
|
this.setState({ currentModal, currentModalData });
|
|
|
},
|
|
},
|
|
|
- currentError: null as string | null,
|
|
|
|
|
|
|
+ currentError: null,
|
|
|
setCurrentError: (currentError: string) => {
|
|
setCurrentError: (currentError: string) => {
|
|
|
this.setState({ currentError });
|
|
this.setState({ currentError });
|
|
|
},
|
|
},
|
|
@@ -54,7 +78,7 @@ class ContextProvider extends Component<PropsType, StateType> {
|
|
|
callback && callback();
|
|
callback && callback();
|
|
|
});
|
|
});
|
|
|
},
|
|
},
|
|
|
- currentProject: null as ProjectType | null,
|
|
|
|
|
|
|
+ currentProject: null,
|
|
|
setCurrentProject: (currentProject: ProjectType, callback?: any) => {
|
|
setCurrentProject: (currentProject: ProjectType, callback?: any) => {
|
|
|
pushQueryParams(this.props, { project_id: currentProject.id.toString() });
|
|
pushQueryParams(this.props, { project_id: currentProject.id.toString() });
|
|
|
if (currentProject) {
|
|
if (currentProject) {
|
|
@@ -66,12 +90,12 @@ class ContextProvider extends Component<PropsType, StateType> {
|
|
|
callback && callback();
|
|
callback && callback();
|
|
|
});
|
|
});
|
|
|
},
|
|
},
|
|
|
- projects: [] as ProjectType[],
|
|
|
|
|
|
|
+ projects: [],
|
|
|
setProjects: (projects: ProjectType[]) => {
|
|
setProjects: (projects: ProjectType[]) => {
|
|
|
projects.sort((a: any, b: any) => (a.name > b.name ? 1 : -1));
|
|
projects.sort((a: any, b: any) => (a.name > b.name ? 1 : -1));
|
|
|
this.setState({ projects });
|
|
this.setState({ projects });
|
|
|
},
|
|
},
|
|
|
- user: null as any,
|
|
|
|
|
|
|
+ user: null,
|
|
|
setUser: (userId: number, email: string) => {
|
|
setUser: (userId: number, email: string) => {
|
|
|
this.setState({ user: { userId, email } });
|
|
this.setState({ user: { userId, email } });
|
|
|
},
|
|
},
|
|
@@ -79,7 +103,7 @@ class ContextProvider extends Component<PropsType, StateType> {
|
|
|
setDevOpsMode: (devOpsMode: boolean) => {
|
|
setDevOpsMode: (devOpsMode: boolean) => {
|
|
|
this.setState({ devOpsMode });
|
|
this.setState({ devOpsMode });
|
|
|
},
|
|
},
|
|
|
- capabilities: null as CapabilityType,
|
|
|
|
|
|
|
+ capabilities: null,
|
|
|
setCapabilities: (capabilities: CapabilityType) => {
|
|
setCapabilities: (capabilities: CapabilityType) => {
|
|
|
this.setState({ capabilities });
|
|
this.setState({ capabilities });
|
|
|
},
|
|
},
|