Skip to content

Commit

Permalink
RL-207: redux updates
Browse files Browse the repository at this point in the history
  • Loading branch information
NoodleOfDeath committed Feb 2, 2024
1 parent 3bbb213 commit 0b97b7e
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/core/src/client/contexts/storage/StorageContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
SyncableIoOut,
ViewableFeature,
} from './types';
import { UserData } from '../../store/reducers/user/UserData';
import { UserData } from './UserData';

import {
API,
Expand Down
68 changes: 68 additions & 0 deletions src/core/src/client/contexts/storage/UserData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
LoginResponse,
Profile,
UserStats,
WrappedJwt,
} from '~/api';

export type UserDataProps = LoginResponse & {
tokens?: WrappedJwt | WrappedJwt[];
};

export class UserData implements UserDataProps {

userId: number;
tokens: WrappedJwt[] = [];
profile?: Profile;
unlinked?: boolean;

get token() {
if (this.tokens.length === 0) {
return undefined;
}
return this.tokens[0];
}

get tokenString() {
return this.token?.signed;
}

get expired() {
return this.tokens.length > 0 && this.tokens.every((t) => UserData.tokenHasExpired(t));
}

get valid() {
return this.profile && !this.expired;
}

static tokenHasExpired(token: WrappedJwt) {
return token.expiresAt < Date.now();
}

constructor({
userId = -1,
token,
tokens = token ? [token] : [],
profile,
unlinked,
}: Partial<UserDataProps> = {}) {
this.userId = userId;
this.tokens = (Array.isArray(tokens) ? tokens : [tokens]).filter((t) => !UserData.tokenHasExpired(t)).sort((a, b) => b.priority - a.priority);
this.profile = profile;
this.unlinked = unlinked;
}

addToken(token: WrappedJwt) {
this.tokens = [...this.tokens, token].filter((t) => !UserData.tokenHasExpired(t)).sort((a, b) => b.priority - a.priority);
return this;
}

updateStats(stats: UserStats) {
this.profile = {
...this.profile,
stats,
};
return this;
}

}
2 changes: 1 addition & 1 deletion src/core/src/client/contexts/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './types';
export * from '../../store/reducers/user/UserData';
export * from './UserData';
export * from './StorageContext';
31 changes: 20 additions & 11 deletions src/core/src/client/store/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { combineReducers, configureStore } from '@reduxjs/toolkit';

import categorySlice from './reducers/category';
import preferenceSlice from './reducers/preference';
import publisherSlice from './reducers/publisher';
import summarySlice from './reducers/summary';
import userSlice from './reducers/user';
import {
categoryInitialState,
categorySlice,
preferenceInitialState,
preferenceSlice,
publisherInitialState,
publisherSlice,
summaryInitialState,
summarySlice,
} from './reducers';

// test kernels with HSM
// consolidate the shadows
Expand All @@ -16,13 +21,17 @@ import userSlice from './reducers/user';
//

const store = configureStore({
preloadedState: {},
preloadedState: {
category: categoryInitialState,
preference: preferenceInitialState,
publisher: publisherInitialState,
summary: summaryInitialState,
},
reducer: combineReducers({
category: categorySlice,
preference: preferenceSlice,
publisher: publisherSlice,
summary: summarySlice,
user: userSlice,
category: categorySlice.reducer,
preference: preferenceSlice.reducer,
publisher: publisherSlice.reducer,
summary: summarySlice.reducer,
}),
});

Expand Down
26 changes: 16 additions & 10 deletions src/core/src/client/store/reducers/category/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@ import { StoreState } from '../../types';

export type CategorySliceState = Pick<StoreState, 'excludedCategories' | 'favoritedCategories' | 'followedCategories'>;

const categorySlice = createSlice({
initialState: null as CategorySliceState | null,
const initialState: CategorySliceState = {
excludedCategories: {},
favoritedCategories: {},
followedCategories: {},
};

export { initialState as categoryInitialState };

export const categorySlice = createSlice({
initialState,
name: 'category',
reducers: {
setExcludedCategories: (state, action: PayloadAction<CategorySliceState>) => {
setExcludedCategories: (state, action: PayloadAction<StoreState['excludedCategories']>) => {
if (state) {
state.excludedCategories = action.payload.excludedCategories;
state.excludedCategories = action.payload;
}
},
setFavoritedCategories: (state, action: PayloadAction<CategorySliceState>) => {
setFavoritedCategories: (state, action: PayloadAction<StoreState['favoritedCategories']>) => {
if (state) {
state.favoritedCategories = action.payload.favoritedCategories;
state.favoritedCategories = action.payload;
}
},
setFollowedCategories: (state, action: PayloadAction<CategorySliceState>) => {
setFollowedCategories: (state, action: PayloadAction<StoreState['followedCategories']>) => {
if (state) {
state.followedCategories = action.payload.followedCategories;
state.followedCategories = action.payload;
}
},
},
Expand All @@ -31,5 +39,3 @@ export const {
setFavoritedCategories,
setFollowedCategories,
} = categorySlice.actions;

export default categorySlice.reducer;
9 changes: 4 additions & 5 deletions src/core/src/client/store/reducers/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import userReducer from './user';

const rootReducer = { user: userReducer };

export default rootReducer;
export * from './category';
export * from './preference';
export * from './publisher';
export * from './summary';
Loading

0 comments on commit 0b97b7e

Please sign in to comment.