Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/s 10 user settings #19

Merged
merged 20 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"coverage": "jest --coverage --config=./src/jest.config.js"
},
"dependencies": {
"@types/react-color": "^2.17.4",
"algolia-places-react": "^1.5.1",
"antd": "^4.6.6",
"axios": "~0.20.0",
Expand All @@ -32,6 +33,7 @@
"qs": "^6.9.4",
"react": "~16.13.1",
"react-chartjs-2": "^2.9.0",
"react-color": "^2.19.3",
"react-dom": "~16.13.1",
"react-gauge-chart": "git+https://github.com/BossBele/react-gauge-chart.git",
"react-masonry-css": "^1.0.14",
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GithubAvatar } from 'components/GithubAvatar';
import css from 'styled-jsx/css';

import { EyeOutlined, EditOutlined, LogoutOutlined, SaveTwoTone } from '@ant-design/icons';
import UserSettings from './UserSettings/UserSettings';

type Props = {
username: string;
Expand Down Expand Up @@ -67,6 +68,7 @@ export function Header(props: Props) {
<b>{props.title}</b> {props.courseName}
</div>
<div className="profile">
{props.title === 'Schedule' && <UserSettings />}
{isSaveButtonVisible && (
<Button danger ghost size="large" style={{ marginRight: 16, height: 38 }} onClick={props.onSaveClick}>
<SaveTwoTone twoToneColor={['#f5222d', '#fff1f0']} />
Expand Down
53 changes: 53 additions & 0 deletions client/src/components/UserSettings/SettingsTagColor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think TagColor file name will be a better option. It will avoid duplications and shorten import paths at least.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it's done too

import { Collapse, Tag } from 'antd';
import { BgColorsOutlined } from '@ant-design/icons';
import { GithubPicker } from 'react-color';
import { pickerColors, mockedTags as tags, setTagColor, getTagColor, DEFAULT_COLOR } from './userSettingsHandlers';
import { useLocalStorage } from 'react-use';

const SettingsTagColor: React.FC = () => {
const { Panel } = Collapse;
const defaultTagColor = JSON.stringify({ default: DEFAULT_COLOR });
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the source code of useLocalStorage, there is no need to serialize/deserialize data. It comes out of the box: https://github.com/streamich/react-use/blob/master/src/useLocalStorage.ts

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, removed unnecessary things

const [storedTagColors, setStoredTagColors] = useLocalStorage<string>('tagColors', defaultTagColor);

const collapseTags = (
<Collapse accordion ghost>
{tags.map((item) => {
return (
<Panel
header={
<Tag
style={{
cursor: 'pointer',
borderColor: getTagColor(item.name, storedTagColors),
color: getTagColor(item.name, storedTagColors),
backgroundColor: `${getTagColor(item.name, storedTagColors)}10`,
}}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move these 4 lines of the code to it's own function and call getTagColor only once

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a new function. It returns an object with styles borderColor, color, backgroundColor, it can also accept any other css styles

>
{item.name}
</Tag>
}
key={item.name}
>
<GithubPicker
colors={pickerColors}
triangle="hide"
width={'138px'}
onChange={(e) => setTagColor(e, item.name, setStoredTagColors, storedTagColors)}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's consider using useCallback for onChange handler

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/>
</Panel>
);
})}
</Collapse>
);

return (
<Collapse expandIcon={() => <BgColorsOutlined style={{ fontSize: '20px', color: '#08c' }} />}>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about moving () => <BgColorsOutlined style={{ fontSize: '20px', color: '#08c' }} /> to it's own component?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made a new component TagColorIcon. It contains all the magick :)

<Panel header="Change Tag Color" key="tags">
{collapseTags}
</Panel>
</Collapse>
);
};

export default SettingsTagColor;
27 changes: 27 additions & 0 deletions client/src/components/UserSettings/UserSettings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { useState } from 'react';
import { Button, Drawer } from 'antd';
import { SettingOutlined } from '@ant-design/icons';
import SettingsTagColor from './SettingsTagColor';


const UserSettings: React.FC = () => {
const [isVisible, setIsVisible] = useState(false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's replace with isOpen

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it's done

const showDrawer = () => {
setIsVisible(true);
};
const onClose = () => {
setIsVisible(false);
};

return (
<>
<Button icon={<SettingOutlined />} title='User settings' size='large' type="primary" onClick={showDrawer}/>

<Drawer title="User Settings" placement="right" closable={false} onClose={onClose} visible={isVisible}>
<SettingsTagColor />
</Drawer>
</>
);
};

export default UserSettings;
49 changes: 49 additions & 0 deletions client/src/components/UserSettings/userSettingsHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ColorState as IColorState } from 'react-color';

export const pickerColors = [
'#ff0000',
'#FF6900',
'#FCCB00',
'#37D67A',
'#308e00',
'#2db7f5',
'#004DCF',
'#a326f4',
'#FA28FF',
'#F78DA7',
];

export const DEFAULT_COLOR = '#308e00';

export function setTagColor(e: IColorState, tagName: string, localStorageHook: (value: string) => void, storedTagColors: string | undefined) {
const parsedTagColors = JSON.parse(<string>storedTagColors) || {};
parsedTagColors[tagName] = e.hex;
localStorageHook(JSON.stringify(parsedTagColors));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be just: localStorageHook({ ... storedTagColors, [tagName]: e.hex })

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! Done

}

export function getTagColor(tagName: string, storedTagColors: string | undefined) {
const parsedTagColors = JSON.parse(<string>(storedTagColors)) || {};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove JSON.parse and JSON.stringify here too. Also let's move getter and setter to TagSettings file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed unnecessary things. Why should we move getter and setter to TagColor?

return parsedTagColors[tagName] || DEFAULT_COLOR;
}

export const mockedTags = [
{ name: 'deadline' },
{ name: 'test' },
{ name: 'jstask' },
{ name: 'htmltask' },
{ name: 'externaltask' },
{ name: 'selfeducation' },
{ name: 'codewars' },
{ name: 'codejam' },
{ name: 'newtask' },
{ name: 'lecture' },
{ name: 'lecture_online' },
{ name: 'lecture_offline' },
{ name: 'lecture_mixed' },
{ name: 'lecture_self_study' },
{ name: 'info' },
{ name: 'warmup' },
{ name: 'meetup' },
{ name: 'workshop' },
{ name: 'interview' },
];