feat: first draft

This commit is contained in:
Loic Coenen
2025-10-29 21:41:12 +01:00
commit 283b3e6885
36 changed files with 3073 additions and 0 deletions

View 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),
}

View 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';

View File

@@ -0,0 +1,4 @@
export * from './actions';
export * from './consts';
export * from './types';

View 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
View File

@@ -0,0 +1,4 @@
export * from './actions'
export * from './initialState'
export * from './reducer'
export * from './state'

View File

@@ -0,0 +1,7 @@
import { type State } from "./state";
export const initialState: State = {
stations: undefined,
error: undefined,
loading: false,
};

View File

56
src/state/reducer.ts Normal file
View 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
View File

@@ -0,0 +1,9 @@
import type { Station } from "../types";
export type State = {
loading: boolean,
stations: Station[] | undefined,
error: Error | undefined,
}