summaryrefslogtreecommitdiff
path: root/server/web/src/redux
diff options
context:
space:
mode:
Diffstat (limited to 'server/web/src/redux')
-rw-r--r--server/web/src/redux/actions.ts55
-rw-r--r--server/web/src/redux/allReducer.ts83
-rw-r--r--server/web/src/redux/store.ts4
3 files changed, 142 insertions, 0 deletions
diff --git a/server/web/src/redux/actions.ts b/server/web/src/redux/actions.ts
new file mode 100644
index 0000000..887b808
--- /dev/null
+++ b/server/web/src/redux/actions.ts
@@ -0,0 +1,55 @@
+import { IActivity } from '../interfaces/Activity.interface';
+import { IClient } from '../interfaces/Client.interface';
+
+export type ClientsLoadType = { type: 'CLIENTS_LOAD'; payload: Map<string, IClient>; };
+export type SessionLoadType = { type: 'SESSION_LOAD'; payload: Set<string>; };
+export type SessionAllType = { type: 'SESSION_ALL'; };
+export type SessionCloseType = { type: 'SESSION_CLOSE'; };
+export type SessionAddType = { type: 'SESSION_ADD'; payload: string; };
+export type SessionRemoveType = { type: 'SESSION_REMOVE'; payload: string; };
+export type ClientAddType = { type: 'CLIENT_ADD'; payload: { unique_id: string, client: IClient; }; };
+export type ClientRemoveType = { type: 'CLIENT_REMOVE'; payload: string; };
+export type ActivityUpdateType = { type: 'ACTIVITY_UPDATE'; payload: IActivity; };
+
+export const clientsLoad = (clients: Map<string, IClient>): ClientsLoadType => ({
+ type: 'CLIENTS_LOAD',
+ payload: clients
+});
+
+export const sessionLoad = (session: Set<string>): SessionLoadType => ({
+ type: 'SESSION_LOAD',
+ payload: session
+});
+
+export const sessionAll = (): SessionAllType => ({
+ type: 'SESSION_ALL'
+});
+
+export const sessionClose = (): SessionCloseType => ({
+ type: 'SESSION_CLOSE'
+});
+
+export const sessionAdd = (unique_id: string): SessionAddType => ({
+ type: 'SESSION_ADD',
+ payload: unique_id
+});
+
+export const sessionRemove = (unique_id: string): SessionRemoveType => ({
+ type: 'SESSION_REMOVE',
+ payload: unique_id
+});
+
+export const clientAdd = (unique_id: string, client: IClient): ClientAddType => ({
+ type: 'CLIENT_ADD',
+ payload: { unique_id, client }
+});
+
+export const clientRemove = (unique_id: string): ClientRemoveType => ({
+ type: 'CLIENT_REMOVE',
+ payload: unique_id
+});
+
+export const activityUpdate = (activity: IActivity): ActivityUpdateType => ({
+ type: 'ACTIVITY_UPDATE',
+ payload: activity
+});
diff --git a/server/web/src/redux/allReducer.ts b/server/web/src/redux/allReducer.ts
new file mode 100644
index 0000000..fe72005
--- /dev/null
+++ b/server/web/src/redux/allReducer.ts
@@ -0,0 +1,83 @@
+import { IAllReducer } from '../interfaces/AllReducer.interface';
+import { IClient } from '../interfaces/Client.interface';
+import {
+ ClientsLoadType,
+ SessionLoadType,
+ SessionAllType,
+ SessionCloseType,
+ SessionAddType,
+ SessionRemoveType,
+ ClientAddType,
+ ClientRemoveType,
+ ActivityUpdateType
+} from './actions';
+
+const initialState: IAllReducer = {
+ clients: new Map<string, IClient>(),
+ session: new Set<string>()
+};
+
+export const allReducer = (
+ state: IAllReducer = initialState,
+ action:
+ | ClientsLoadType
+ | SessionLoadType
+ | SessionAllType
+ | SessionCloseType
+ | SessionAddType
+ | SessionRemoveType
+ | ClientAddType
+ | ClientRemoveType
+ | ActivityUpdateType
+) => {
+ switch (action.type) {
+ case 'CLIENTS_LOAD': {
+ return { ...state, clients: new Map<string, IClient>(action.payload) };
+ }
+ case 'SESSION_LOAD': {
+ return { ...state, session: new Set<string>(action.payload) };
+ }
+ case 'SESSION_ALL': {
+ return { ...state, session: new Set<string>(state.clients.keys()) };
+ }
+ case 'SESSION_CLOSE': {
+ return { ...state, session: new Set<string>() };
+ }
+ case 'SESSION_ADD': {
+ const updatedSession = new Set<string>(state.session);
+ updatedSession.add(action.payload);
+ return { ...state, session: updatedSession };
+ }
+ case 'SESSION_REMOVE': {
+ const updatedSession = new Set<string>(state.session);
+ updatedSession.delete(action.payload);
+ return { ...state, session: updatedSession };
+ }
+ case 'CLIENT_ADD': {
+ const updatedClients = new Map<string, IClient>(state.clients);
+ updatedClients.set(action.payload.unique_id, action.payload.client);
+ return { ...state, clients: updatedClients };
+ }
+ case 'CLIENT_REMOVE': {
+ const updatedClients = new Map<string, IClient>(state.clients);
+ updatedClients.delete(action.payload);
+ return { ...state, clients: updatedClients };
+ }
+ case 'ACTIVITY_UPDATE': {
+ const { unique_id, active_window, idle_time, resource_usage } = action.payload;
+ const updatedClients = new Map<string, IClient>(state.clients);
+ const client = updatedClients.get(unique_id);
+
+ if (client !== undefined) {
+ client.active_window = active_window;
+ client.idle_time = idle_time;
+ client.resource_usage = resource_usage;
+ updatedClients.set(unique_id, client);
+ }
+
+ return { ...state, clients: updatedClients };
+ }
+ default:
+ return state;
+ }
+};
diff --git a/server/web/src/redux/store.ts b/server/web/src/redux/store.ts
new file mode 100644
index 0000000..8321e8e
--- /dev/null
+++ b/server/web/src/redux/store.ts
@@ -0,0 +1,4 @@
+import { createStore } from 'redux';
+import { allReducer } from './allReducer';
+
+export const store = createStore(allReducer);