Skip to content

Commit

Permalink
feat: add feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
TomatoVan committed Apr 8, 2024
1 parent 958b794 commit a04902e
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 67 deletions.
132 changes: 67 additions & 65 deletions json-server/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"id": "1",
"title": "Javascript news СВЕЖАЯ",
"subtitle": "Что нового в JS за 2022 год?",
"img": "https://teknotower.com/wp-content/uploads/2020/11/js.png",
"img": "https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Unofficial_JavaScript_logo_2.svg/1024px-Unofficial_JavaScript_logo_2.svg.png",
"views": 1022,
"createdAt": "26.04.2022",
"userId": "1",
Expand Down Expand Up @@ -110,7 +110,7 @@
"id": "2",
"title": "Python news",
"subtitle": "Что нового в JS за 2022 год?",
"img": "https://zsfond.ru/wp-content/uploads/2021/03/piton-1-1024x578.jpg",
"img": "https://fiverr-res.cloudinary.com/images/t_main1,q_auto,f_auto,q_auto,f_auto/gigs2/113415626/original/a17667d573bf34559bf0d35993ed76e57d43ad00/program-python-scripts-for-automation-and-data-mining.png",
"views": 5204,
"createdAt": "26.02.2022",
"userId": "1",
Expand Down Expand Up @@ -158,7 +158,7 @@
"id": "4",
"title": "Scala news",
"subtitle": "Что нового в JS за 2022 год?",
"img": "https://coursefreedl.com/wp-content/uploads/2020/01/Scala-Zero-To-Hero-Complete-Guide.jpg",
"img": "https://pluspng.com/img-png/scala-logo-png-scala-logo-1200x675.png",
"views": 10222,
"createdAt": "24.01.2022",
"userId": "1",
Expand All @@ -182,7 +182,7 @@
"id": "5",
"title": "Golang news",
"subtitle": "Что нового в JS за 2022 год?",
"img": "https://cdn-front.kwork.ru/pics/t3/88/16350941-1630390388.jpg",
"img": "https://www.itsec.ru/hs-fs/hubfs/ISR/Golang.png?width=750&name=Golang.png",
"views": 10222,
"createdAt": "26.02.2020",
"userId": "1",
Expand Down Expand Up @@ -302,7 +302,7 @@
"id": "10",
"title": "Python news",
"subtitle": "Что нового в JS за 2022 год?",
"img": "https://zsfond.ru/wp-content/uploads/2021/03/piton-1-1024x578.jpg",
"img": "https://fiverr-res.cloudinary.com/images/t_main1,q_auto,f_auto,q_auto,f_auto/gigs2/113415626/original/a17667d573bf34559bf0d35993ed76e57d43ad00/program-python-scripts-for-automation-and-data-mining.png",
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
Expand Down Expand Up @@ -350,7 +350,7 @@
"id": "12",
"title": "Python news",
"subtitle": "Что нового в JS за 2022 год?",
"img": "https://zsfond.ru/wp-content/uploads/2021/03/piton-1-1024x578.jpg",
"img": "https://fiverr-res.cloudinary.com/images/t_main1,q_auto,f_auto,q_auto,f_auto/gigs2/113415626/original/a17667d573bf34559bf0d35993ed76e57d43ad00/program-python-scripts-for-automation-and-data-mining.png",
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
Expand Down Expand Up @@ -398,7 +398,7 @@
"id": "14",
"title": "Python news",
"subtitle": "Что нового в JS за 2022 год?",
"img": "https://zsfond.ru/wp-content/uploads/2021/03/piton-1-1024x578.jpg",
"img": "https://fiverr-res.cloudinary.com/images/t_main1,q_auto,f_auto,q_auto,f_auto/gigs2/113415626/original/a17667d573bf34559bf0d35993ed76e57d43ad00/program-python-scripts-for-automation-and-data-mining.png",
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
Expand Down Expand Up @@ -446,7 +446,7 @@
"id": "16",
"title": "Python news",
"subtitle": "Что нового в JS за 2022 год?",
"img": "https://zsfond.ru/wp-content/uploads/2021/03/piton-1-1024x578.jpg",
"img": "https://fiverr-res.cloudinary.com/images/t_main1,q_auto,f_auto,q_auto,f_auto/gigs2/113415626/original/a17667d573bf34559bf0d35993ed76e57d43ad00/program-python-scripts-for-automation-and-data-mining.png",
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
Expand Down Expand Up @@ -518,7 +518,7 @@
"id": "18",
"title": "Научная статья - Биология",
"subtitle": "БиологиЯ",
"img": "https://avatars.mds.yandex.net/get-zen_doc/2746556/pub_5f50dd7e1a1ddf4776aa5569_5f50decd2506f211d1de6284/scale_1200",
"img": "https://kartinkin.net/uploads/posts/2022-05/1652214942_9-kartinkin-net-p-biologiya-krasivie-kartinki-10.jpg",
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
Expand All @@ -541,7 +541,7 @@
{
"title": "Научная статья - Биология",
"subtitle": "БиологиЯ",
"img": "https://avatars.mds.yandex.net/get-zen_doc/2746556/pub_5f50dd7e1a1ddf4776aa5569_5f50decd2506f211d1de6284/scale_1200",
"img": "https://kartinkin.net/uploads/posts/2022-05/1652214942_9-kartinkin-net-p-biologiya-krasivie-kartinki-10.jpg",
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
Expand All @@ -554,7 +554,7 @@
{
"title": "Научная статья - Биология",
"subtitle": "БиологиЯ",
"img": "https://avatars.mds.yandex.net/get-zen_doc/2746556/pub_5f50dd7e1a1ddf4776aa5569_5f50decd2506f211d1de6284/scale_1200",
"img": "https://i.ebayimg.com/00/s/NjE0WDc2OA==/z/PhIAAOSw~oFZvBcK/$_57.JPG?set_id=8800005007",
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
Expand All @@ -563,45 +563,6 @@
],
"blocks": [],
"id": "6f91vPH"
},
{
"title": "TESTING ARTICLE",
"subtitle": "Subtitle text",
"img": "https://avatars.mds.yandex.net/get-zen_doc/2746556/pub_5f50dd7e1a1ddf4776aa5569_5f50decd2506f211d1de6284/scale_1200",
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": [
"SCIENCE"
],
"blocks": [],
"id": "y1D5B71"
},
{
"title": "TESTING ARTICLE",
"subtitle": "Subtitle text",
"img": "https://avatars.mds.yandex.net/get-zen_doc/2746556/pub_5f50dd7e1a1ddf4776aa5569_5f50decd2506f211d1de6284/scale_1200",
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": [
"SCIENCE"
],
"blocks": [],
"id": "3u59urr"
},
{
"title": "TESTING ARTICLE",
"subtitle": "Subtitle text",
"img": "https://avatars.mds.yandex.net/get-zen_doc/2746556/pub_5f50dd7e1a1ddf4776aa5569_5f50decd2506f211d1de6284/scale_1200",
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": [
"SCIENCE"
],
"blocks": [],
"id": "PhhG_hW"
}
],
"comments": [
Expand Down Expand Up @@ -760,6 +721,24 @@
"userId": "1",
"text": "123",
"id": "KmtWJR_"
},
{
"articleId": "8",
"userId": "2",
"text": "asdasd",
"id": "eB1A-7c"
},
{
"articleId": "2",
"userId": "1",
"text": "class",
"id": "wAZ_hJM"
},
{
"articleId": "2",
"userId": "1",
"text": "asfasf",
"id": "SygCNYE"
}
],
"users": [
Expand All @@ -770,7 +749,11 @@
"roles": [
"ADMIN"
],
"avatar": "https://yakutsk.ru/wp-content/uploads/2022/07/12/bystrov_haker.jpeg"
"features": {
"isArticleRatingEnabled": true,
"isCounterEnabled": false
},
"avatar": "https://mobimg.b-cdn.net/v3/fetch/22/2207633df03a819cd72889249c8361a8.jpeg?w=1470&r=0.5625"
},
{
"id": "2",
Expand All @@ -779,7 +762,11 @@
"roles": [
"USER"
],
"avatar": "https://xakep.ru/wp-content/uploads/2018/05/171485/KuroiSH-hacker.jpg"
"features": {
"isArticleRatingEnabled": false,
"isCounterEnabled": false
},
"avatar": "https://i.pinimg.com/originals/9a/e0/2d/9ae02d4b4288396108ef77830a59e060.jpg"
},
{
"id": "3",
Expand All @@ -788,7 +775,11 @@
"roles": [
"MANAGER"
],
"avatar": "https://xakep.ru/wp-content/uploads/2018/05/171485/KuroiSH-hacker.jpg"
"features": {
"isArticleRatingEnabled": false,
"isCounterEnabled": true
},
"avatar": "https://s1.1zoom.ru/big3/992/367659-alexfas01.jpg"
},
{
"id": "4",
Expand All @@ -797,20 +788,24 @@
"roles": [
"ADMIN"
],
"avatar": "https://xakep.ru/wp-content/uploads/2018/05/171485/KuroiSH-hacker.jpg"
"features": {
"isArticleRatingEnabled": true,
"isCounterEnabled": true
},
"avatar": "https://s1.1zoom.ru/big3/992/367659-alexfas01.jpg"
}
],
"profile": [
{
"id": "1",
"first": "timur",
"first": "timurasdasd",
"lastname": "ulbi",
"age": 465,
"currency": "RUB",
"country": "Belarus",
"city": "Moscow",
"username": "admin213",
"avatar": "https://yt3.ggpht.com/ytc/AKedOLTYUJxG8Hu036PQ_TXpMLq2fG8Kj8NZI4h0lbn_3g=s900-c-k-c0x00ffffff-no-rj"
"avatar": "https://mobimg.b-cdn.net/v3/fetch/22/2207633df03a819cd72889249c8361a8.jpeg?w=1470&r=0.5625"
},
{
"id": "2",
Expand All @@ -821,7 +816,7 @@
"country": "Ukraine",
"city": "Moscow",
"username": "ulbi tv",
"avatar": "https://xakep.ru/wp-content/uploads/2018/05/171485/KuroiSH-hacker.jpg"
"avatar": "https://i.pinimg.com/originals/9a/e0/2d/9ae02d4b4288396108ef77830a59e060.jpg"
},
{
"id": "4",
Expand All @@ -843,13 +838,6 @@
"userId": "1",
"articleId": "1"
},
{
"id": "2",
"rate": 4,
"feedback": "Хорошая статья",
"userId": "1",
"articleId": "2"
},
{
"userId": "1",
"articleId": "3",
Expand Down Expand Up @@ -877,6 +865,20 @@
"rate": 3,
"feedback": "",
"id": "bcPnbbZ"
},
{
"userId": "2",
"articleId": "1",
"rate": 4,
"feedback": "asdasdasdad",
"id": "1I3E5gT"
},
{
"userId": "1",
"articleId": "2",
"rate": 2,
"feedback": "asdasdasd",
"id": "xbwnNBF"
}
],
"profile-ratings": [
Expand All @@ -895,4 +897,4 @@
"profileId": "2"
}
]
}
}
6 changes: 5 additions & 1 deletion src/entities/User/model/slice/userSlice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { USER_LOCALSTORAGE_KEY } from '@/shared/const/localstorage';
import { User, UserSchema } from '../types/user';
import { setFeatureFlags } from '@/shared/lib/features';

const initialState: UserSchema = {
_inited: false,
Expand All @@ -12,11 +13,14 @@ export const userSlice = createSlice({
reducers: {
setAuthData: (state, action: PayloadAction<User>) => {
state.authData = action.payload;
setFeatureFlags(action.payload.features);
},
initAuthData: (state) => {
const user = localStorage.getItem(USER_LOCALSTORAGE_KEY);
if (user) {
state.authData = JSON.parse(user);
const json = JSON.parse(user) as User;
state.authData = json;
setFeatureFlags(json.features);
}
state._inited = true;
},
Expand Down
2 changes: 2 additions & 0 deletions src/entities/User/model/types/user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { UserRole } from '../../model/consts/consts';
import { FeatureFlags } from '@/shared/types/featureFlags';

export interface User {
id: string;
username: string;
avatar?: string;
roles?: UserRole[];
features?: FeatureFlags;
}

export interface UserSchema {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import { articleDetailsPageReducer } from '../../model/slices';
import { ArticleDetails } from '../../../../entities/Article';
import { ArticleRating } from '@/features/articleRating';
import { Page } from '@/widgets/Page';
import { getFeatureFlag } from '@/shared/lib/features';
import { Counter } from '@/entities/Counter';

const reducers: ReducersList = {
articleDetailsPage: articleDetailsPageReducer,
};

const ArticlesDetailsPage = (props: any) => {
const { id } = useParams<{ id: string }>();
const isArticleRatingEnabled = getFeatureFlag('isArticleRatingEnabled');
const isCounterEnabled = getFeatureFlag('isCounterEnabled');

if (!id) {
return null;
Expand All @@ -31,7 +35,8 @@ const ArticlesDetailsPage = (props: any) => {
<VStack gap="16" max>
<ArticlesDetailsPageHeader />
<ArticleDetails id={id} />
<ArticleRating articleId={id} />
{isCounterEnabled && <Counter />}
{isArticleRatingEnabled && <ArticleRating articleId={id} />}
<ArticleRecommendationsList />
<ArticleDetailsComments id={id} />
</VStack>
Expand Down
1 change: 1 addition & 0 deletions src/shared/lib/features/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { setFeatureFlags, getFeatureFlag } from './setGetFeatures';
14 changes: 14 additions & 0 deletions src/shared/lib/features/setGetFeatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { FeatureFlags } from '@/shared/types/featureFlags';

// features does not change on session, only after
let featureFlags: FeatureFlags;

export function setFeatureFlags(newFeatureFlags?: FeatureFlags) {
if (newFeatureFlags) {
featureFlags = newFeatureFlags;
}
}

export function getFeatureFlag(flag: keyof FeatureFlags) {
return featureFlags[flag] ?? false;
}
4 changes: 4 additions & 0 deletions src/shared/types/featureFlags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface FeatureFlags {
isArticleRatingEnabled?: boolean;
isCounterEnabled?: boolean;
}

0 comments on commit a04902e

Please sign in to comment.