Add basic 'workloads' Redux app
This commit is contained in:
1
src/state/index.ts
Normal file
1
src/state/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './reducers';
|
||||
18
src/state/reducers.ts
Normal file
18
src/state/reducers.ts
Normal 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,
|
||||
});
|
||||
70
src/state/workloads/actions.ts
Normal file
70
src/state/workloads/actions.ts
Normal 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,
|
||||
},
|
||||
});
|
||||
|
||||
2
src/state/workloads/index.ts
Normal file
2
src/state/workloads/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './actions';
|
||||
export * from './reducers';
|
||||
47
src/state/workloads/reducers.ts
Normal file
47
src/state/workloads/reducers.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
1
src/state/workloads/types.ts
Normal file
1
src/state/workloads/types.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type Status = 'WORKING' | 'SUCCESS' | 'FAILURE' | 'CANCELED';
|
||||
Reference in New Issue
Block a user