refactor: remove redux, some rearrangement

This commit is contained in:
Ivan Shmidt
2020-05-14 11:53:25 +02:00
parent 7b424c0337
commit 6a3cc27048
6 changed files with 77 additions and 113 deletions
-3
View File
@@ -18,15 +18,12 @@
"@material-ui/core": "^4.9.1",
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "4.0.0-alpha.45",
"@rematch/core": "^1.4.0",
"@types/react-lazylog": "^4.5.0",
"@types/react-redux": "^7.1.8",
"circleci-api": "^4.0.0",
"moment": "^2.25.3",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-lazylog": "^4.5.2",
"react-redux": "^7.2.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-use": "^13.0.0"
@@ -13,40 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { FC, useReducer, Dispatch } from 'react';
import React, { FC, useReducer, Dispatch, Reducer } from 'react';
import { circleCIApiRef } from '../../api';
import { BuildSummary, BuildWithSteps } from 'circleci-api';
import { State, PollingState, Action, SettingsState } from './types';
export { SettingsState };
export const AppContext = React.createContext<[AppState, Dispatch<Action>]>(
export const AppContext = React.createContext<[State, Dispatch<Action>]>(
[] as any,
);
type SettingsState = {
owner: string;
repo: string;
token: string;
};
export enum PollingState {
Polling,
Idle,
}
export type BuildsState = {
builds: BuildSummary[];
pollingIntervalId: number | null;
pollingState: PollingState;
};
export const STORAGE_KEY = `${circleCIApiRef.id}.settings`;
export type AppState = {
settings: SettingsState;
builds: BuildsState;
buildsWithSteps: BuildsWithStepsState;
};
const initialState: AppState = {
const initialState: State = {
settings: {
owner: '',
repo: '',
@@ -65,45 +42,7 @@ const initialState: AppState = {
},
};
type SettingsAction = {
type: 'setCredentials';
payload: {
repo: string;
owner: string;
token: string;
};
};
type BuildsAction =
| {
type: 'setBuilds';
payload: BuildSummary[];
}
| {
type: 'setPollingIntervalId';
payload: number | null;
};
type BuildsWithStepsAction =
| {
type: 'setBuildWithSteps';
payload: BuildWithSteps;
}
| {
type: 'setPollingIntervalIdForBuildsWithSteps';
payload: number | null;
};
export type BuildsWithStepsState = {
builds: Record<number, BuildWithSteps>;
pollingIntervalId: number | null;
pollingState: PollingState;
getBuildError: Error | null;
};
type Action = SettingsAction | BuildsAction | BuildsWithStepsAction;
const reducer = (state: AppState, action: Action): AppState => {
const reducer: Reducer<State, Action> = (state, action) => {
switch (action.type) {
case 'setCredentials':
return {
@@ -0,0 +1,62 @@
import { BuildSummary, BuildWithSteps } from '../../api';
export type SettingsState = {
owner: string;
repo: string;
token: string;
};
export enum PollingState {
Polling,
Idle,
}
export type BuildsState = {
builds: BuildSummary[];
pollingIntervalId: number | null;
pollingState: PollingState;
};
export type State = {
settings: SettingsState;
builds: BuildsState;
buildsWithSteps: BuildsWithStepsState;
};
type SettingsAction = {
type: 'setCredentials';
payload: {
repo: string;
owner: string;
token: string;
};
};
type BuildsAction =
| {
type: 'setBuilds';
payload: BuildSummary[];
}
| {
type: 'setPollingIntervalId';
payload: number | null;
};
type BuildsWithStepsAction =
| {
type: 'setBuildWithSteps';
payload: BuildWithSteps;
}
| {
type: 'setPollingIntervalIdForBuildsWithSteps';
payload: number | null;
};
export type BuildsWithStepsState = {
builds: Record<number, BuildWithSteps>;
pollingIntervalId: number | null;
pollingState: PollingState;
getBuildError: Error | null;
};
export type Action = SettingsAction | BuildsAction | BuildsWithStepsAction;
@@ -17,7 +17,7 @@ import { errorApiRef, useApi } from '@backstage/core';
import { GitType } from 'circleci-api';
import { useContext, useEffect } from 'react';
import { circleCIApiRef } from '../../api/index';
import { AppContext, BuildsState } from '../../components/Store';
import { AppContext } from '../../components/Store';
export type BuildsDispatch = {
restartBuild: (buildId: number) => Promise<void>;
@@ -25,7 +25,7 @@ export type BuildsDispatch = {
const INTERVAL_AMOUNT = 3000;
export function useBuilds(): [BuildsState, BuildsDispatch] {
export function useBuilds() {
const [{ builds, settings }, dispatch] = useContext(AppContext);
const api = useApi(circleCIApiRef);
const errorApi = useApi(errorApiRef);
@@ -98,5 +98,5 @@ export function useBuilds(): [BuildsState, BuildsDispatch] {
{
restartBuild,
},
];
] as const;
}
@@ -14,19 +14,9 @@
* limitations under the License.
*/
import { useContext, useEffect } from 'react';
import { AppContext, STORAGE_KEY } from '../../components/Store';
import { AppContext, STORAGE_KEY, SettingsState } from '../../components/Store';
import { useApi, errorApiRef } from '@backstage/core';
export type SettingsDispatch = {
saveSettings: (settings: SettingsState) => void;
};
export type SettingsState = {
token: string;
owner: string;
repo: string;
};
// type Effect = {
// type: 'rehydrate',
// payload: any
@@ -35,7 +25,7 @@ export type SettingsState = {
// const effects = [];
// pushEffect, popEffect
export function useSettings(): [SettingsState, SettingsDispatch] {
export function useSettings() {
const [{ settings }, dispatch] = useContext(AppContext);
// const interpret = eff => {
@@ -69,6 +59,7 @@ export function useSettings(): [SettingsState, SettingsDispatch] {
errorApi.post(error);
}
};
useEffect(() => {
rehydrate();
}, []);
@@ -88,5 +79,5 @@ export function useSettings(): [SettingsState, SettingsDispatch] {
});
},
},
];
] as const;
}