From 69bef12d44b528c191a0bc168b072e5d4034938b Mon Sep 17 00:00:00 2001 From: Ash Keel Date: Thu, 30 Sep 2021 12:26:59 +0200 Subject: [PATCH] Split reducer in two files --- frontend/src/store/api/reducer.ts | 234 ++++++++---------------------- frontend/src/store/api/types.ts | 117 +++++++++++++++ 2 files changed, 176 insertions(+), 175 deletions(-) create mode 100644 frontend/src/store/api/types.ts diff --git a/frontend/src/store/api/reducer.ts b/frontend/src/store/api/reducer.ts index 050eb03..40cf4dd 100644 --- a/frontend/src/store/api/reducer.ts +++ b/frontend/src/store/api/reducer.ts @@ -1,4 +1,3 @@ -/* eslint-disable camelcase */ /* eslint-disable no-param-reassign */ import { AsyncThunk, @@ -9,160 +8,12 @@ import { PayloadAction, } from '@reduxjs/toolkit'; import KilovoltWS from '@strimertul/kilovolt-client'; - -// Storage -const moduleConfigKey = 'stul-meta/modules'; -const httpConfigKey = 'http/config'; -const twitchConfigKey = 'twitch/config'; -const twitchBotConfigKey = 'twitch/bot-config'; -const twitchBotCommandsKey = 'twitch/bot-custom-commands'; -const stulbeConfigKey = 'stulbe/config'; -const loyaltyConfigKey = 'loyalty/config'; -const loyaltyPointsPrefix = 'loyalty/points/'; -const loyaltyRewardsKey = 'loyalty/rewards'; -const loyaltyGoalsKey = 'loyalty/goals'; -const loyaltyRedeemQueueKey = 'loyalty/redeem-queue'; - -// RPCs -const loyaltyCreateRedeemKey = 'loyalty/@create-redeem'; -const loyaltyRemoveRedeemKey = 'loyalty/@remove-redeem'; - -interface ModuleConfig { - configured: boolean; - kv: boolean; - static: boolean; - twitch: boolean; - stulbe: boolean; - loyalty: boolean; -} - -interface HTTPConfig { - bind: string; - path: string; -} - -interface TwitchConfig { - enable_bot: boolean; - api_client_id: string; - api_client_secret: string; -} - -interface TwitchBotConfig { - username: string; - oauth: string; - channel: string; - chat_keys: boolean; - chat_history: number; -} - -type AccessLevelType = 'everyone' | 'vip' | 'moderators' | 'streamer'; - -export interface TwitchBotCustomCommand { - description: string; - access_level: AccessLevelType; - response: string; - enabled: boolean; -} - -type TwitchBotCustomCommands = Record; - -interface StulbeConfig { - endpoint: string; - username: string; - auth_key: string; -} - -interface LoyaltyConfig { - currency: string; - points: { - interval: number; - amount: number; - activity_bonus: number; - }; - banlist: string[]; -} - -export interface LoyaltyPointsEntry { - points: number; -} - -export type LoyaltyStorage = Record; - -export interface LoyaltyReward { - enabled: boolean; - id: string; - name: string; - description: string; - image: string; - price: number; - required_info?: string; - cooldown: number; -} - -export interface LoyaltyGoal { - enabled: boolean; - id: string; - name: string; - description: string; - image: string; - total: number; - contributed: number; - contributors: Record; -} - -export interface LoyaltyRedeem { - username: string; - display_name: string; - when: Date; - reward: LoyaltyReward; - request_text: string; -} - -export interface APIState { - client: KilovoltWS; - connected: boolean; - initialLoadComplete: boolean; - loyalty: { - users: LoyaltyStorage; - rewards: LoyaltyReward[]; - goals: LoyaltyGoal[]; - redeemQueue: LoyaltyRedeem[]; - }; - twitchBot: { - commands: TwitchBotCustomCommands; - }; - moduleConfigs: { - moduleConfig: ModuleConfig; - httpConfig: HTTPConfig; - twitchConfig: TwitchConfig; - twitchBotConfig: TwitchBotConfig; - stulbeConfig: StulbeConfig; - loyaltyConfig: LoyaltyConfig; - }; -} - -const initialState: APIState = { - client: null, - connected: false, - initialLoadComplete: false, - loyalty: { - users: null, - rewards: null, - goals: null, - redeemQueue: null, - }, - twitchBot: { - commands: null, - }, - moduleConfigs: { - moduleConfig: null, - httpConfig: null, - twitchConfig: null, - twitchBotConfig: null, - stulbeConfig: null, - loyaltyConfig: null, - }, -}; +import { + APIState, + LoyaltyPointsEntry, + LoyaltyRedeem, + LoyaltyStorage, +} from './types'; function makeGetterThunk(key: string) { return async (_: void, { getState }) => { @@ -215,6 +66,14 @@ function makeModule( // eslint-disable-next-line import/no-mutable-exports, @typescript-eslint/ban-types export let setupClientReconnect: AsyncThunk; +// Storage +const loyaltyPointsPrefix = 'loyalty/points/'; +const loyaltyRewardsKey = 'loyalty/rewards'; + +// RPCs +const loyaltyCreateRedeemKey = 'loyalty/@create-redeem'; +const loyaltyRemoveRedeemKey = 'loyalty/@remove-redeem'; + export const createWSClient = createAsyncThunk( 'api/createClient', async (address: string, { dispatch }) => { @@ -232,7 +91,9 @@ export const getUserPoints = createAsyncThunk( const keys = await api.client.getKeysByPrefix(loyaltyPointsPrefix); const userpoints: LoyaltyStorage = {}; Object.entries(keys).forEach(([k, v]) => { - userpoints[k.substr(loyaltyPointsPrefix.length)] = JSON.parse(v); + userpoints[k.substr(loyaltyPointsPrefix.length)] = JSON.parse( + v as string, + ); }); return userpoints; }, @@ -258,71 +119,71 @@ export const setUserPoints = createAsyncThunk( ); export const modules = { - moduleConfig: makeModule( - moduleConfigKey, + moduleConfig: makeModule( + 'stul-meta/modules', (state) => state.moduleConfigs?.moduleConfig, (state, { payload }) => { state.moduleConfigs.moduleConfig = payload; }, ), - httpConfig: makeModule( - httpConfigKey, + httpConfig: makeModule( + 'http/config', (state) => state.moduleConfigs?.httpConfig, (state, { payload }) => { state.moduleConfigs.httpConfig = payload; }, ), - twitchConfig: makeModule( - twitchConfigKey, + twitchConfig: makeModule( + 'twitch/config', (state) => state.moduleConfigs?.twitchConfig, (state, { payload }) => { state.moduleConfigs.twitchConfig = payload; }, ), - twitchBotConfig: makeModule( - twitchBotConfigKey, + twitchBotConfig: makeModule( + 'twitch/bot-config', (state) => state.moduleConfigs?.twitchBotConfig, (state, { payload }) => { state.moduleConfigs.twitchBotConfig = payload; }, ), - twitchBotCommands: makeModule( - twitchBotCommandsKey, + twitchBotCommands: makeModule( + 'twitch/bot-custom-commands', (state) => state.twitchBot?.commands, (state, { payload }) => { state.twitchBot.commands = payload; }, ), - stulbeConfig: makeModule( - stulbeConfigKey, + stulbeConfig: makeModule( + 'stulbe/config', (state) => state.moduleConfigs?.stulbeConfig, (state, { payload }) => { state.moduleConfigs.stulbeConfig = payload; }, ), - loyaltyConfig: makeModule( - loyaltyConfigKey, + loyaltyConfig: makeModule( + 'loyalty/config', (state) => state.moduleConfigs?.loyaltyConfig, (state, { payload }) => { state.moduleConfigs.loyaltyConfig = payload; }, ), - loyaltyRewards: makeModule( + loyaltyRewards: makeModule( loyaltyRewardsKey, (state) => state.loyalty.rewards, (state, { payload }) => { state.loyalty.rewards = payload; }, ), - loyaltyGoals: makeModule( - loyaltyGoalsKey, + loyaltyGoals: makeModule( + 'loyalty/goals', (state) => state.loyalty.goals, (state, { payload }) => { state.loyalty.goals = payload; }, ), - loyaltyRedeemQueue: makeModule( - loyaltyRedeemQueueKey, + loyaltyRedeemQueue: makeModule( + 'loyalty/redeem-queue', (state) => state.loyalty.redeemQueue, (state, { payload }) => { state.loyalty.redeemQueue = payload; @@ -356,6 +217,29 @@ const moduleChangeReducers = Object.fromEntries( (state: APIState, action: PayloadAction) => never >; +const initialState: APIState = { + client: null, + connected: false, + initialLoadComplete: false, + loyalty: { + users: null, + rewards: null, + goals: null, + redeemQueue: null, + }, + twitchBot: { + commands: null, + }, + moduleConfigs: { + moduleConfig: null, + httpConfig: null, + twitchConfig: null, + twitchBotConfig: null, + stulbeConfig: null, + loyaltyConfig: null, + }, +}; + const apiReducer = createSlice({ name: 'api', initialState, diff --git a/frontend/src/store/api/types.ts b/frontend/src/store/api/types.ts new file mode 100644 index 0000000..161a724 --- /dev/null +++ b/frontend/src/store/api/types.ts @@ -0,0 +1,117 @@ +/* eslint-disable camelcase */ + +import KilovoltWS from '@strimertul/kilovolt-client'; + +interface ModuleConfig { + configured: boolean; + kv: boolean; + static: boolean; + twitch: boolean; + stulbe: boolean; + loyalty: boolean; +} + +interface HTTPConfig { + bind: string; + path: string; +} + +interface TwitchConfig { + enable_bot: boolean; + api_client_id: string; + api_client_secret: string; +} + +interface TwitchBotConfig { + username: string; + oauth: string; + channel: string; + chat_keys: boolean; + chat_history: number; +} + +type AccessLevelType = 'everyone' | 'vip' | 'moderators' | 'streamer'; + +export interface TwitchBotCustomCommand { + description: string; + access_level: AccessLevelType; + response: string; + enabled: boolean; +} + +type TwitchBotCustomCommands = Record; + +interface StulbeConfig { + endpoint: string; + username: string; + auth_key: string; +} + +interface LoyaltyConfig { + currency: string; + points: { + interval: number; + amount: number; + activity_bonus: number; + }; + banlist: string[]; +} + +export interface LoyaltyPointsEntry { + points: number; +} + +export type LoyaltyStorage = Record; + +export interface LoyaltyReward { + enabled: boolean; + id: string; + name: string; + description: string; + image: string; + price: number; + required_info?: string; + cooldown: number; +} + +export interface LoyaltyGoal { + enabled: boolean; + id: string; + name: string; + description: string; + image: string; + total: number; + contributed: number; + contributors: Record; +} + +export interface LoyaltyRedeem { + username: string; + display_name: string; + when: Date; + reward: LoyaltyReward; + request_text: string; +} + +export interface APIState { + client: KilovoltWS; + connected: boolean; + initialLoadComplete: boolean; + loyalty: { + users: LoyaltyStorage; + rewards: LoyaltyReward[]; + goals: LoyaltyGoal[]; + redeemQueue: LoyaltyRedeem[]; + }; + twitchBot: { + commands: TwitchBotCustomCommands; + }; + moduleConfigs: { + moduleConfig: ModuleConfig; + httpConfig: HTTPConfig; + twitchConfig: TwitchConfig; + twitchBotConfig: TwitchBotConfig; + stulbeConfig: StulbeConfig; + loyaltyConfig: LoyaltyConfig; + }; +}