feat: train, news and weather

This commit is contained in:
Loic Coenen
2025-10-30 23:19:03 +01:00
parent 283b3e6885
commit b2a5031056
24 changed files with 796 additions and 41 deletions

View File

@@ -2,12 +2,24 @@ import {
loadTrainSchedule,
loadTrainScheduleSuccess,
loadTrainScheduleError,
loadWeatherError,
loadWeatherSuccess,
loadWeather,
loadNewsError,
loadNewsSuccess,
loadNews,
} from './consts'
import {
type LoadTrainSchedule,
type LoadTrainScheduleSuccess,
type LoadTrainScheduleError,
type LoadWeatherError,
type LoadWeatherSuccess,
type LoadWeather,
type LoadNewsError,
type LoadNewsSuccess,
type LoadNews,
} from './types';
export const actions = {
@@ -23,4 +35,28 @@ export const actions = {
type: loadTrainScheduleError,
...args
} as LoadTrainScheduleError),
loadNews: (args: Omit<LoadNews, "type">) => ({
type: loadNews,
...args
} as LoadNews),
loadNewsSuccess: (args: Omit<LoadNewsSuccess, "type">) => ({
type: loadNewsSuccess,
...args
} as LoadNewsSuccess),
loadNewsError: (args: Omit<LoadNewsError, "type">) => ({
type: loadNewsError,
...args
} as LoadNewsError),
loadWeather: (args: Omit<LoadWeather, "type">) => ({
type: loadWeather,
...args
} as LoadWeather),
loadWeatherSuccess: (args: Omit<LoadWeatherSuccess, "type">) => ({
type: loadWeatherSuccess,
...args
} as LoadWeatherSuccess),
loadWeatherError: (args: Omit<LoadWeatherError, "type">) => ({
type: loadWeatherError,
...args
} as LoadWeatherError),
}

View File

@@ -4,3 +4,10 @@ export const loadTrainSchedule: ActionType = 'loadTrainSchedule';
export const loadTrainScheduleSuccess: ActionType = 'loadTrainScheduleSuccess';
export const loadTrainScheduleError: ActionType = 'loadTrainScheduleError';
export const loadNews: ActionType = 'loadNews';
export const loadNewsSuccess: ActionType = 'loadNewsSuccess';
export const loadNewsError: ActionType = 'loadNewsError';
export const loadWeather: ActionType = 'loadWeather';
export const loadWeatherSuccess: ActionType = 'loadWeatherSuccess';
export const loadWeatherError: ActionType = 'loadWeatherError';

View File

@@ -1,7 +1,19 @@
export type ActionType = string;
import type { Station } from '../../types';
import { loadTrainSchedule, loadTrainScheduleError, loadTrainScheduleSuccess } from './consts'
import type {Article} from '../../types/article';
import type { LiveBoard } from '../../types/liveboard';
import type {WeatherData} from '../../types/weather';
import {
loadTrainSchedule,
loadTrainScheduleError,
loadTrainScheduleSuccess,
loadNews,
loadNewsError,
loadNewsSuccess,
loadWeather,
loadWeatherError,
loadWeatherSuccess,
} from './consts'
export type Action = {
type: ActionType
@@ -13,7 +25,7 @@ export type LoadTrainSchedule = {
export type LoadTrainScheduleSuccess = {
type: typeof loadTrainScheduleSuccess,
stations: Station[]
liveboard: LiveBoard
}
export type LoadTrainScheduleError = {
@@ -21,3 +33,32 @@ export type LoadTrainScheduleError = {
error: Error
}
export type LoadNews = {
type: typeof loadNews,
}
export type LoadNewsSuccess = {
type: typeof loadNewsSuccess,
news: Article[]
}
export type LoadNewsError = {
type: typeof loadNewsError,
error: Error
}
export type LoadWeather = {
type: typeof loadWeather,
}
export type LoadWeatherSuccess = {
type: typeof loadWeatherSuccess,
weather: WeatherData
}
export type LoadWeatherError = {
type: typeof loadWeatherError,
error: Error
}

View File

@@ -1,7 +1,13 @@
import { type State } from "./state";
export const initialState: State = {
stations: undefined,
error: undefined,
loading: false,
liveboard: undefined,
trainScheduleError: undefined,
trainScheduleLoading: false,
news: undefined,
newsError: undefined,
newsLoading: false,
weather: undefined,
weatherError: undefined,
weatherLoading: false,
};

View File

@@ -5,52 +5,96 @@ import {
loadTrainSchedule,
loadTrainScheduleError,
loadTrainScheduleSuccess,
loadNews,
loadNewsSuccess,
loadNewsError,
loadWeatherError,
loadWeatherSuccess,
loadWeather,
type LoadTrainScheduleError,
type LoadTrainScheduleSuccess,
type LoadWeatherError,
type LoadWeatherSuccess,
type LoadNewsError,
type LoadNewsSuccess,
} from './actions';
//export const reducerInner = (state: State, action: Action): State => {
export const reducer = (state: State, action: Action): State => {
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,
trainScheduleError: undefined,
trainScheduleLoading: true,
}
}
else if(action.type === loadTrainScheduleSuccess) {
return {
...state,
stations: (action as LoadTrainScheduleSuccess).stations,
loading: false,
liveboard: (action as LoadTrainScheduleSuccess).liveboard,
trainScheduleLoading: false,
}
}
else if(action.type === loadTrainScheduleError) {
return {
...state,
error: (action as LoadTrainScheduleError).error,
loading: false,
trainScheduleError: (action as LoadTrainScheduleError).error,
trainScheduleLoading: false,
}
}
if(action.type === loadNews) {
return {
...state,
newsError: undefined,
newsLoading: true,
}
}
else if(action.type === loadNewsSuccess) {
return {
...state,
news: (action as LoadNewsSuccess).news,
newsLoading: false,
}
}
else if(action.type === loadNewsError) {
return {
...state,
newsError: (action as LoadNewsError).error,
newsLoading: false,
}
}
if(action.type === loadWeather) {
return {
...state,
weatherError: undefined,
weatherLoading: true,
}
}
else if(action.type === loadWeatherSuccess) {
return {
...state,
weather: (action as LoadWeatherSuccess).weather,
weatherLoading: false,
}
}
else if(action.type === loadWeatherError) {
return {
...state,
weatherError: (action as LoadWeatherError).error,
weatherLoading: 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})
}
console.log(`TS - ${action.type}`);
console.log({action})
console.log({state})
const newState = reducerInner(state, action);
if(action.type !== tick) {
console.log({newState})
}
console.log({newState})
return newState;
}
*/

View File

@@ -1,9 +1,17 @@
import type { Station } from "../types";
import type { Article } from "../types/article";
import type { LiveBoard } from "../types/liveboard";
import type { WeatherData } from "../types/weather";
export type State = {
loading: boolean,
stations: Station[] | undefined,
error: Error | undefined,
trainScheduleLoading: boolean,
liveboard: LiveBoard | undefined,
trainScheduleError: Error | undefined,
weather: WeatherData | undefined,
weatherLoading: boolean,
weatherError: Error | undefined,
news: Article[] | undefined,
newsLoading: boolean,
newsError: Error | undefined,
}