Add basic 'workloads' Redux app

This commit is contained in:
James Greenaway
2019-01-28 18:21:58 +00:00
parent e1a3f27d8f
commit 986cf86403
13 changed files with 231 additions and 210 deletions

1
src/state/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from './reducers';

18
src/state/reducers.ts Normal file
View File

@@ -0,0 +1,18 @@
import { combineReducers } from 'redux';
import {
Store as WorkloadsStore,
Action as WorkloadActions,
reducer as workloadReducer,
} from './workloads';
export type Action = WorkloadActions;
export interface Store {
workloads: WorkloadsStore;
}
export const reducers = combineReducers<Store, Action>({
workloads: workloadReducer,
});

View File

@@ -0,0 +1,70 @@
import { Status } from './types';
export type Action = {
type: 'WORKLOAD_SUBMIT';
payload: {
complexity: number;
};
} | {
type: 'WORKLOAD_CREATE';
payload: {
workloadId: number;
complexity: number;
completeDate: Date;
};
} | {
type: 'WORKLOAD_CANCEL';
payload: {
workloadId: number;
};
} | {
type: 'WORKLOAD_CHECK_STATUS';
payload: {
workloadId: number;
};
} | {
type: 'WORKLOAD_UPDATE_STATUS';
payload: {
workloadId: number;
status: Status;
};
};
export const submit = ({ complexity }: { complexity: number }): Action => ({
type: 'WORKLOAD_SUBMIT',
payload: {
complexity,
},
});
export const create = ({ workloadId, complexity, completeDate }: { workloadId: number, complexity: number, completeDate: Date }): Action => ({
type: 'WORKLOAD_CREATE',
payload: {
workloadId,
completeDate,
complexity,
},
});
export const cancel = ({ workloadId }: { workloadId: number }): Action => ({
type: 'WORKLOAD_CANCEL',
payload: {
workloadId,
},
});
export const checkStatus = ({ workloadId }: { workloadId: number }): Action => ({
type: 'WORKLOAD_CHECK_STATUS',
payload: {
workloadId,
},
});
export const updateStatus = ({ workloadId, status }: { workloadId: number, status: Status }): Action => ({
type: 'WORKLOAD_UPDATE_STATUS',
payload: {
workloadId,
status,
},
});

View File

@@ -0,0 +1,2 @@
export * from './actions';
export * from './reducers';

View File

@@ -0,0 +1,47 @@
import { Action } from './actions';
import { Status } from './types';
interface Entry<id extends number> {
id: id;
completeDate: Date;
status: Status;
}
export type Store = {
[id in number]: Entry<id>;
};
const initialState: Store = {};
export const reducer = (state: Store = initialState, action: Action): Store => {
switch (action.type) {
case 'WORKLOAD_CREATE':
return {
[action.payload.workloadId]: {
id: action.payload.workloadId,
completeDate: action.payload.completeDate,
status: 'WORKING',
},
};
case 'WORKLOAD_CANCEL':
return {
[action.payload.workloadId]: {
...state[action.payload.workloadId],
status: 'CANCELED',
},
}
case 'WORKLOAD_UPDATE_STATUS':
return {
[action.payload.workloadId]: {
...state[action.payload.workloadId],
status: action.payload.status,
},
}
default:
return state;
}
}

View File

@@ -0,0 +1 @@
export type Status = 'WORKING' | 'SUCCESS' | 'FAILURE' | 'CANCELED';