feat: first draft
This commit is contained in:
26
src/state/actions/actions.ts
Normal file
26
src/state/actions/actions.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import {
|
||||
loadTrainSchedule,
|
||||
loadTrainScheduleSuccess,
|
||||
loadTrainScheduleError,
|
||||
} from './consts'
|
||||
|
||||
import {
|
||||
type LoadTrainSchedule,
|
||||
type LoadTrainScheduleSuccess,
|
||||
type LoadTrainScheduleError,
|
||||
} from './types';
|
||||
|
||||
export const actions = {
|
||||
loadTrainSchedule: (args: Omit<LoadTrainSchedule, "type">) => ({
|
||||
type: loadTrainSchedule,
|
||||
...args
|
||||
} as LoadTrainSchedule),
|
||||
loadTrainScheduleSuccess: (args: Omit<LoadTrainScheduleSuccess, "type">) => ({
|
||||
type: loadTrainScheduleSuccess,
|
||||
...args
|
||||
} as LoadTrainScheduleSuccess),
|
||||
loadTrainScheduleError: (args: Omit<LoadTrainScheduleError, "type">) => ({
|
||||
type: loadTrainScheduleError,
|
||||
...args
|
||||
} as LoadTrainScheduleError),
|
||||
}
|
||||
6
src/state/actions/consts.ts
Normal file
6
src/state/actions/consts.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { type ActionType } from './types';
|
||||
|
||||
export const loadTrainSchedule: ActionType = 'loadTrainSchedule';
|
||||
export const loadTrainScheduleSuccess: ActionType = 'loadTrainScheduleSuccess';
|
||||
export const loadTrainScheduleError: ActionType = 'loadTrainScheduleError';
|
||||
|
||||
4
src/state/actions/index.ts
Normal file
4
src/state/actions/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from './actions';
|
||||
export * from './consts';
|
||||
export * from './types';
|
||||
|
||||
23
src/state/actions/types.ts
Normal file
23
src/state/actions/types.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
export type ActionType = string;
|
||||
|
||||
import type { Station } from '../../types';
|
||||
import { loadTrainSchedule, loadTrainScheduleError, loadTrainScheduleSuccess } from './consts'
|
||||
|
||||
export type Action = {
|
||||
type: ActionType
|
||||
}
|
||||
|
||||
export type LoadTrainSchedule = {
|
||||
type: typeof loadTrainSchedule,
|
||||
}
|
||||
|
||||
export type LoadTrainScheduleSuccess = {
|
||||
type: typeof loadTrainScheduleSuccess,
|
||||
stations: Station[]
|
||||
}
|
||||
|
||||
export type LoadTrainScheduleError = {
|
||||
type: typeof loadTrainScheduleError,
|
||||
error: Error
|
||||
}
|
||||
|
||||
4
src/state/index.ts
Normal file
4
src/state/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from './actions'
|
||||
export * from './initialState'
|
||||
export * from './reducer'
|
||||
export * from './state'
|
||||
7
src/state/initialState.ts
Normal file
7
src/state/initialState.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { type State } from "./state";
|
||||
|
||||
export const initialState: State = {
|
||||
stations: undefined,
|
||||
error: undefined,
|
||||
loading: false,
|
||||
};
|
||||
0
src/state/patches/index.ts
Normal file
0
src/state/patches/index.ts
Normal file
56
src/state/reducer.ts
Normal file
56
src/state/reducer.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { type State } from './state';
|
||||
|
||||
import {
|
||||
type Action,
|
||||
loadTrainSchedule,
|
||||
loadTrainScheduleError,
|
||||
loadTrainScheduleSuccess,
|
||||
type LoadTrainScheduleError,
|
||||
type LoadTrainScheduleSuccess,
|
||||
} from './actions';
|
||||
|
||||
|
||||
//export const reducerInner = (state: State, action: Action): State => {
|
||||
export const reducer = (state: State, action: Action): State => {
|
||||
|
||||
if(action.type === loadTrainSchedule) {
|
||||
return {
|
||||
...state,
|
||||
error: undefined,
|
||||
loading: true,
|
||||
}
|
||||
}
|
||||
else if(action.type === loadTrainScheduleSuccess) {
|
||||
return {
|
||||
...state,
|
||||
stations: (action as LoadTrainScheduleSuccess).stations,
|
||||
loading: false,
|
||||
|
||||
}
|
||||
}
|
||||
else if(action.type === loadTrainScheduleError) {
|
||||
|
||||
return {
|
||||
...state,
|
||||
error: (action as LoadTrainScheduleError).error,
|
||||
loading: false,
|
||||
}
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
/*
|
||||
export const reducer = (state: State, action: Action) => {
|
||||
if(action.type !== tick) {
|
||||
console.log(`MD - ${action.type}`);
|
||||
console.log({action})
|
||||
console.log({state})
|
||||
}
|
||||
const newState = reducerInner(state, action);
|
||||
|
||||
if(action.type !== tick) {
|
||||
console.log({newState})
|
||||
}
|
||||
return newState;
|
||||
}
|
||||
*/
|
||||
9
src/state/state.ts
Normal file
9
src/state/state.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { Station } from "../types";
|
||||
|
||||
export type State = {
|
||||
loading: boolean,
|
||||
stations: Station[] | undefined,
|
||||
error: Error | undefined,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user