Skip to content

Commit

Permalink
feat/-focus-mode-(#110) (#168)
Browse files Browse the repository at this point in the history
* feat: add simple way to add tasks to focus page

* fix: remove unused function

* refactor: leveling > Leveling

* feat: add sidebar link to focus page

* style: 🎨 Leveling

* Rename leveling.ts to Leveling.ts
  • Loading branch information
Undeadlol1 authored Nov 21, 2020
1 parent e300bf1 commit 28a928a
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 37 deletions.
6 changes: 3 additions & 3 deletions src/components/ui/DevelopmentOnlyMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import React, { memo, MouseEvent, useState } from 'react';
import { useSelector } from 'react-redux';
import { useFirestore } from 'react-redux-firebase';
import { getUniqueId } from '../../helpers/getUniqueId';
import LevelingService from '../../services/leveling';
import { addPointsWithSideEffects } from "../../repositories/addPointsWithSideEffects";
import LevelingService from '../../services/Leveling';
import { addPointsWithSideEffects } from '../../repositories/addPointsWithSideEffects';
import { addPointsToUser } from '../../repositories/addPointsToUser';
import { Profile } from '../../entities/Profile';
import { createTask } from '../../repositories/createTask';
Expand Down Expand Up @@ -85,8 +85,8 @@ function DevelopmentOnlyMenu() {
function createAReward() {
firestore.collection('rewards').add({
userId: auth.uid,
name: loremIpsum({ count: random(1, 5), units: 'words' }),
points: random(0, 100),
name: loremIpsum({ count: random(1, 5), units: 'words' }),
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/NavBar/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import React, { memo } from 'react';
import { Else, If, Then } from 'react-if';
import { useDispatch } from 'react-redux';
import { Link } from 'react-router-dom';
import LevelingService from '../../../services/leveling';
import LevelingService from '../../../services/Leveling';
import { useTypedSelector } from '../../../store';
import {
authSelector,
Expand Down
13 changes: 10 additions & 3 deletions src/components/ui/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
toggleSidebar,
useTypedTranslate,
} from '../../services/index';
import FocusIcon from '@material-ui/icons/FilterCenterFocus';
import { useTranslation } from 'react-i18next';

const log = debug('Sidebar');
const useStyles = makeStyles((theme: Theme) => ({
Expand Down Expand Up @@ -57,6 +59,7 @@ const Sidebar: React.FC<{
const cx = useStyles();
const history = useHistory();
const t = useTypedTranslate();
const { t: sidebarTranslator } = useTranslation('sidebar');
log('isLoggedIn: ', isLoggedIn);
log('isOpen: ', isOpen);

Expand All @@ -71,9 +74,7 @@ const Sidebar: React.FC<{
if (!isLoggedIn) history.push('/signin');
else {
localStorage.removeItem('userId');
getFirebase()
.logout()
.catch(handleErrors);
getFirebase().logout().catch(handleErrors);
}
}

Expand All @@ -99,6 +100,12 @@ const Sidebar: React.FC<{
classes={{ paper: cx.backgroundColor }}
onClose={toggleSidebar}
>
<ListItem button onClick={redirectAndCloseSidebar('/focus')}>
<ListItemIcon>
<FocusIcon />
</ListItemIcon>
<StyledListText primary={sidebarTranslator('focus_mode')} />
</ListItem>
<List>
<ListItem button onClick={redirectAndCloseSidebar('/faq')}>
<ListItemIcon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import get from 'lodash/get';
import isUndefined from 'lodash/isUndefined';
import React, { memo } from 'react';
import { Box, Theme } from '@material-ui/core';
import LevelingService from '../../../services/leveling';
import LevelingService from '../../../services/Leveling';
import { Profile } from '../../../entities/Profile';

const log = debug('ExpirienceProgressBar');
Expand Down
3 changes: 3 additions & 0 deletions src/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,7 @@ export default {
add_any_value: `Add "{{value}}"`,
pick_or_create_a_task : "Pick or create a task"
},
sidebar: {
focus_mode: 'Focus Mode',
}
};
3 changes: 3 additions & 0 deletions src/locales/ru.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,7 @@ export default {
add_any_value: `Добавить "{{value}}"`,
pick_or_create_a_task : "Выберите или создайте задачу",
},
sidebar: {
focus_mode: 'Режим Фокусировки',
}
};
16 changes: 12 additions & 4 deletions src/pages/FocusModePage/FocusModePage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Box } from '@material-ui/core';
import React, { memo } from 'react';
import { If } from 'react-if';
import useList from 'react-use/lib/useList';
import { TasksList } from '../../components/tasks/TasksList';
import { Autocomplete } from '../../components/unsorted/Autocomplete';
import { WhatDoYouFeelSlider } from '../../components/unsorted/WhatDoYouFeelSlider';
import { Task } from '../../entities/Task';
import { If } from 'react-if';
import { Autocomplete } from '../../components/unsorted/Autocomplete';
import { getUniqueId } from '../../helpers/getUniqueId';
import { useTypedTranslate } from '../../services';

export interface FocusModePageProps {
Expand All @@ -19,6 +21,7 @@ const FocusModePage = memo(function FocusModePage({
tasksForAutoComplete = [],
}: FocusModePageProps) {
const t = useTypedTranslate();
const [tasks, { push }] = useList<Task>([]);
const autocompleteOptions = tasksForAutoComplete.map((task) => ({
value: task,
label: task.name,
Expand All @@ -29,10 +32,15 @@ const FocusModePage = memo(function FocusModePage({
<Autocomplete
options={autocompleteOptions}
label={t('pick_or_create_a_task')}
onChange={console.log}
onChange={(payload) => {
push({
id: getUniqueId(),
name: payload.label,
} as Task);
}}
/>
<Box mb={2}>
<TasksList tasks={tasksToList} loading={isLoading} />
<TasksList tasks={tasks || tasksToList} loading={isLoading} />
</Box>
<If condition={!isLoading}>
<WhatDoYouFeelSlider onChange={console.log} />
Expand Down
2 changes: 1 addition & 1 deletion src/pages/ProfilePage/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import DarkOrLightThemePicker from '../../components/ui/DarkOrLightThemePicker';
import ToggleEncouragingMessages from '../../components/ui/ToggleEncouragingMessages';
import { MyUserPoints } from '../../components/users/MyUserPoints';
import { handleErrors } from '../../services/index';
import LevelingService from '../../services/leveling';
import LevelingService from '../../services/Leveling';
import { Profile } from '../../entities/Profile';
import { upsertProfile } from '../../repositories/upsertProfile';
import { authSelector, profileSelector } from '../../store/selectors';
Expand Down
16 changes: 6 additions & 10 deletions src/repositories/addPointsWithSideEffects.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
import {
getNewlyUnlockedReward,


showLevelUpAnimation
showLevelUpAnimation,
} from '../services/index';
import LevelingService from '../services/leveling';
import LevelingService from '../services/Leveling';
import {
authSelector,
profilePointsSelector,
rewardsSelector
rewardsSelector,
} from '../store/selectors';
import { toggleRewardModal } from '../store/uiSlice';
import { addPointsToUser } from './addPointsToUser';
import { store } from '../store/index';


export function addPointsWithSideEffects(
userId: string,
points: number
points: number,
): Promise<void> {
const state = store.getState();
const auth = authSelector(state);
const profilePoints = profilePointsSelector(state);
const nextReward = getNewlyUnlockedReward(
profilePoints,
points,
rewardsSelector(state)
rewardsSelector(state),
);
// TODO refactor
if (nextReward)
store.dispatch(toggleRewardModal());
if (nextReward) store.dispatch(toggleRewardModal());
if (LevelingService.willUserLevelUp(profilePoints, points)) {
showLevelUpAnimation();
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/leveling.ts → src/services/Leveling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class LevelingService {
}

static calculatePointsToNextLevel(level: number) {
const baseXP = 30
const baseXP = 30;
const exponent = 1.1;
return baseXP * ((level ^ exponent) | 1);
}
Expand All @@ -45,4 +45,4 @@ export default class LevelingService {
);
return levelAfterAddingPoints > currentLevel;
}
}
}
16 changes: 4 additions & 12 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,14 @@ export function initializeFirebase() {
} else {
// Use Firestore emulator for local development
firebase.firestore().settings({
// ssl: false,
// host: 'localhost:8080',
ssl: false,
host: 'localhost:8080',
});
}

return firebase;
}

export function normalizeQueryResponse(
snapshot: firebase.firestore.QuerySnapshot,
) {
if (snapshot.empty) return [];
return snapshot.docs.map((document) => ({
id: document.id,
...document.data(),
}));
}

export function handleErrors(
e: Error | undefined | firebase.auth.Error,
) {
Expand Down Expand Up @@ -241,6 +231,8 @@ export function initializeI18n() {
lng: 'ru',
debug: false,
fallbackLng: 'en',
// ns: ['translation', 'sidebar'],
// defaultNS: 'translation',
interpolation: {
// not needed for react as it escapes by default
escapeValue: false,
Expand Down

0 comments on commit 28a928a

Please sign in to comment.