Skip to content

Commit

Permalink
feat: create redux slice for featureFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
frankvonhoven committed Jul 12, 2024
1 parent c707f74 commit 1c5ae0a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
65 changes: 65 additions & 0 deletions app/core/redux/slices/featureFlags/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit';

export interface FeatureFlagsState {
featureFlags: object[] | null;
loading: boolean;
error: string | null;
}

export const initialState: FeatureFlagsState = {
featureFlags: null,
loading: false,
error: null,
};

const name = 'featureFlags';

const slice = createSlice({
name,
initialState,
reducers: {
/**
* Initiates the fetching of feature flags.
* @param state - The current state of the featureFlags slice.
*/
getFeatureFlags: (state: FeatureFlagsState) => {
state.loading = true;
state.error = null;
},
/**
* Handles the successful fetching of feature flags.
* @param state - The current state of the featureFlags slice.
* @param action - An action with the fetched feature flags as payload.
*/
getFeatureFlagsSuccess: (
state: FeatureFlagsState,
action: PayloadAction<object>,
) => {
state.featureFlags = action.payload;

Check failure on line 38 in app/core/redux/slices/featureFlags/index.ts

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Type '{}' is missing the following properties from type 'object[]': length, pop, push, concat, and 29 more.
state.loading = false;
state.error = null;
},
/**
* Handles errors that occur during the fetching of feature flags.
* @param state - The current state of the featureFlags slice.
* @param action - An action with the error message as payload.
*/
getFeatureFlagsError: (
state: FeatureFlagsState,
action: PayloadAction<string>,
) => {
state.loading = false;
state.error = action.payload;
},
},
});

const { actions, reducer } = slice;

export default reducer;

export const { getFeatureFlags, getFeatureFlagsSuccess, getFeatureFlagsError } =
actions;

export const FETCH_FEATURE_FLAGS = 'getFeatureFlags';
export type FETCH_FEATURE_FLAGS = typeof FETCH_FEATURE_FLAGS;
5 changes: 5 additions & 0 deletions app/reducers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import bookmarksReducer from './bookmarks';
import browserReducer from './browser';
import engineReducer from '../core/redux/slices/engine';
import featureFlagsReducer from '../core/redux/slices/featureFlags';
import privacyReducer from './privacy';
import modalsReducer from './modals';
import settingsReducer from './settings';
Expand Down Expand Up @@ -56,6 +57,9 @@ export interface RootState {
engine: { backgroundState: EngineState };
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
featureFlags: any;
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
privacy: any;
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -132,6 +136,7 @@ const rootReducer = combineReducers<RootState, any>({
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
engine: engineReducer as any,
featureFlags: featureFlagsReducer,
privacy: privacyReducer,
bookmarks: bookmarksReducer,
browser: browserReducer,
Expand Down
3 changes: 3 additions & 0 deletions app/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ const createStoreAndPersistor = async () => {
basicFunctionalityEnabled:
store.getState().settings.basicFunctionalityEnabled,
});
store.dispatch({
type: 'FETCH_FEATURE_FLAGS',
});
EngineService.initalizeEngine(store);
Authentication.init(store);
LockManagerService.init(store);
Expand Down
38 changes: 38 additions & 0 deletions app/store/sagas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import Logger from '../../util/Logger';
import LockManagerService from '../../core/LockManagerService';
import AppConstants from '../../../app/core/AppConstants';
import { XMLHttpRequest as _XMLHttpRequest } from 'xhr2';
import {
getFeatureFlagsSuccess,
getFeatureFlagsError,
} from '../../core/redux/slices/featureFlags';

if (typeof global.XMLHttpRequest === 'undefined') {
global.XMLHttpRequest = _XMLHttpRequest;
Expand All @@ -24,6 +28,18 @@ if (typeof global.XMLHttpRequest === 'undefined') {
const originalSend = XMLHttpRequest.prototype.send;
const originalOpen = XMLHttpRequest.prototype.open;

interface GenericObject {
[key: string]: unknown;
}

type DataArray<T extends GenericObject> = T[];

interface FeatureFlagResponse {
status: string;
data: DataArray<GenericObject>;
message?: string;
}

export function* appLockStateMachine() {
let biometricsListenerTask: Task<void> | undefined;
while (true) {
Expand Down Expand Up @@ -166,8 +182,30 @@ export function* basicFunctionalityToggle() {
}
}

function* fetchFeatureFlags(): Generator {
try {
let response: FeatureFlagResponse = {
status: '',
data: [],
};
yield fetch('http://localhost:3000/launch-darkly')
.then((res) => res.json())
.then((res) => (response = res));

if (response.status !== 'ok') {
yield put(getFeatureFlagsError(response.message));

Check failure on line 196 in app/store/sagas/index.ts

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
return;
}
yield put(getFeatureFlagsSuccess(response.data));
} catch (error) {
console.error(error);
yield put(getFeatureFlagsError(error));

Check failure on line 202 in app/store/sagas/index.ts

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Argument of type 'unknown' is not assignable to parameter of type 'string'.
}
}

// Main generator function that initializes other sagas in parallel.
export function* rootSaga() {
yield fork(authStateMachine);
yield fork(basicFunctionalityToggle);
yield fork(fetchFeatureFlags);
}

0 comments on commit 1c5ae0a

Please sign in to comment.