add redux in csc

This commit is contained in:
Tykayn 2025-08-08 11:31:27 +02:00 committed by tykayn
parent a89007a81b
commit 3534919502
14 changed files with 277 additions and 12 deletions

View file

@ -1,9 +1,10 @@
import { Component, signal } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import {SaeLib} from '../../../my-workspace/projects/sae-lib/src/public-api';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
imports: [RouterOutlet, SaeLib],
templateUrl: './app.html',
styleUrl: './app.scss'
})

View file

@ -0,0 +1,18 @@
// Define initial state
import {StateInterface} from './reducers';
export const initialState: StateInterface = {
app: {
backendAPIRoot: "",
demoMode: true, // selon l'environnement
},
currentLang: "fr_FR",
currentTheme: "light",
langsList: ["fr_FR", "en_US"],
themesList: ["light", "dark"],
user: {
isAuthenticated: false,
id: "",
},
};

View file

@ -0,0 +1,172 @@
import {isDevMode} from '@angular/core';
import {ActionReducerMap, MetaReducer} from '@ngrx/store';
import {initialState} from '../initialState';
// Define action types
export enum ActionTypes {
UPDATE_USER = '[User] Update User',
UPDATE_APP = '[App] Update App',
UPDATE_CONVERSATIONS_LIST = '[Conversations] Update Conversations List',
UPDATE_ACTIVE_CONVERSATION = '[Conversations] Update Active Conversation',
POST_MESSAGE_TO_ACTIVE_CONVERSATION = '[Conversations] Post message to Active Conversation',
UPDATE_LAST_SEEN_MESSAGE = '[Conversations] Update Last Seen Message',
TOGGLE_CONVERSATIONS_LIST_PANEL = '[App] displayConversationListPanelLarge',
UPDATE_SINGLE_CONVERSATION = '[Conversations] Update Single Conversation',
SEND_USER_FEEDBACK = '[App] Send User Feedback',
SWITCH_TO_NEXT_THEME = '[App] Switch To Next Theme',
SWITCH_TO_NEXT_LANGUAGE = '[App] Switch To Next Language'
}
// Define action interfaces
export interface UpdateUserAction {
type: ActionTypes.UPDATE_USER;
payload: {
isAuthenticated: boolean;
id: string;
};
}
export interface UpdateAppAction {
type: ActionTypes.UPDATE_APP;
payload: {
backendAPIRoot?: string;
displayConversationListPanelLarge?: boolean;
displayFeedBackPanel?: boolean;
displayErrorResponse?: boolean;
displaySourcesPanelLarge?: boolean;
displayTour?: boolean;
displayWarningAIMaturing?: boolean;
feedBackInput?: string;
input?: string;
lang?: string;
notifications?: any[];
ollamaAvailable?: boolean;
searchInput?: string;
version?: string;
};
}
export interface SendUserFeedbackAction {
type: ActionTypes.SEND_USER_FEEDBACK;
payload: {
feedback: string;
};
}
export interface SwitchToNextThemeAction {
type: ActionTypes.SWITCH_TO_NEXT_THEME;
}
export interface SwitchToNextLanguageAction {
type: ActionTypes.SWITCH_TO_NEXT_LANGUAGE;
}
type Theme = 'light' | 'dark';
type Lang = 'fr_FR' | 'en_US';
export type AppActions =
| UpdateUserAction
| UpdateAppAction
| SendUserFeedbackAction
| SwitchToNextThemeAction
| SwitchToNextLanguageAction;
// Define the State interface
export interface StateInterface {
user: {
isAuthenticated: boolean;
id: string;
};
app: {
demoMode:boolean;
backendAPIRoot: string;
};
themesList: Array<Theme>;
currentTheme: Theme;
langsList: Array<Lang>;
currentLang: Lang;
}
// Define reducers
function userReducer(state = initialState.user, action: AppActions) {
switch (action.type) {
case ActionTypes.UPDATE_USER:
return {
...state,
...action.payload
};
default:
return state;
}
}
function appReducer(state = initialState.app, action: AppActions) {
switch (action.type) {
case ActionTypes.UPDATE_APP:
return {
...state,
...action.payload
};
default:
return state;
}
}
function themesListReducer(state = initialState.themesList, action: AppActions) {
switch (action.type) {
default:
return state;
}
}
function currentThemeReducer(state = initialState.currentTheme, action: AppActions) {
switch (action.type) {
case ActionTypes.SWITCH_TO_NEXT_THEME:
// Get the themesList from the store
const themesList = initialState.themesList;
// Find the index of the current theme
const currentThemeIndex = themesList.indexOf(state);
// Calculate the index of the next theme (or go back to the first theme if at the end)
const nextThemeIndex = (currentThemeIndex + 1) % themesList.length;
// Get the next theme
return themesList[nextThemeIndex];
default:
return state;
}
}
function langsListReducer(state = initialState.langsList, action: AppActions) {
switch (action.type) {
default:
return state;
}
}
function currentLangReducer(state = initialState.currentLang, action: AppActions) {
switch (action.type) {
case ActionTypes.SWITCH_TO_NEXT_LANGUAGE:
// Get the langsList from the store
const langsList = initialState.langsList;
// Find the index of the current language
const currentLangIndex = langsList.indexOf(state);
// Calculate the index of the next language (or go back to the first language if at the end)
const nextLangIndex = (currentLangIndex + 1) % langsList.length;
// Get the next language
return langsList[nextLangIndex];
default:
return state;
}
}
// Add reducers to ActionReducerMap
export const reducers: ActionReducerMap<StateInterface, AppActions> = {
user: userReducer,
app: appReducer,
themesList: themesListReducer,
currentTheme: currentThemeReducer,
langsList: langsListReducer,
currentLang: currentLangReducer
};
export const metaReducers: MetaReducer<StateInterface, AppActions>[] = isDevMode() ? [] : [];

View file

@ -1,13 +1,5 @@
// libs
@use 'remixicon/fonts/remixicon.scss';
@use 'bulma/sass/base/minireset.scss';
@use 'bulma/sass/grid/_index.scss';
@use 'bulma/sass/components/navbar.scss';
// dev tools
//@use '_app.scss';
@use '_global.scss';
@use '_debug.scss';
@use '_variables.scss';

View file

@ -2,4 +2,6 @@
// sass lang utils
@use "sass:color";
@use "app/styles/app.scss";
//@use "../../my-workspace/projects/sae-lib/app/styles/app.scss";
// lib SAE Aero styles
//@use 'sae-lib/src/styles/index.scss';
@use '../../my-workspace/projects/sae-lib/src/styles/index.scss';