-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(story-tags): add tags to story list items (#343)
- Loading branch information
Showing
16 changed files
with
186 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const SET_SELECTED_STORY_TAGS = 'SET_SELECTED_STORY_TAGS'; | ||
|
||
export interface SetSelectedStoryTagsAction { | ||
type: typeof SET_SELECTED_STORY_TAGS; | ||
tags: string[]; | ||
} | ||
|
||
const setSelectedStoryTagsAction = ( | ||
tags: string[] | ||
): SetSelectedStoryTagsAction => ({ | ||
type: SET_SELECTED_STORY_TAGS, | ||
tags | ||
}); | ||
|
||
export default setSelectedStoryTagsAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@require '../../../variables.styl' | ||
|
||
.tags | ||
display: flex | ||
|
||
.tag | ||
margin-right: 0.5em | ||
padding: 0.2em 1em | ||
border: 1px solid $darkGrey4 | ||
border-radius: 12px | ||
text-transform: uppercase | ||
font-size: 0.8em | ||
|
||
&:hover | ||
background: $darkGrey2 | ||
|
||
.selected, .selected:hover | ||
background: $darkGrey4 | ||
color: $textColor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React, {FunctionComponent} from 'react'; | ||
import {useDispatch} from 'react-redux'; | ||
import cx from 'classnames'; | ||
|
||
import setSelectedStoryTags from '../../actions/set-selected-story-tags'; | ||
|
||
import styles from './story-tags.styl'; | ||
|
||
interface Props { | ||
tags: string[]; | ||
selected: string[]; | ||
} | ||
|
||
const StoryTags: FunctionComponent<Props> = ({tags, selected}) => { | ||
const dispatch = useDispatch(); | ||
const toggleTag = (tag: string) => { | ||
const newTags = selected.includes(tag) | ||
? selected.filter(oldTag => oldTag !== tag) | ||
: selected.concat([tag]); | ||
|
||
dispatch(setSelectedStoryTags(newTags)); | ||
}; | ||
const getTagClasses = (tag: string) => | ||
cx(styles.tag, selected.includes(tag) && styles.selected); | ||
|
||
return ( | ||
<div className={styles.tags}> | ||
{tags.map(tag => ( | ||
<span | ||
className={getTagClasses(tag)} | ||
key={tag} | ||
onClick={(event: React.MouseEvent<HTMLSpanElement>) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
toggleTag(tag); | ||
}}> | ||
{tag} | ||
</span> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
export default StoryTags; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import intersection from 'lodash.intersection'; | ||
|
||
import {StoryList} from '../types/story-list'; | ||
|
||
// filter stories by tags - keep story if at lease one of the tag matches | ||
export function filterStories(stories: StoryList, tags: string[]) { | ||
if (tags.length === 0) { | ||
return stories; | ||
} | ||
|
||
return stories.filter(story => intersection(story.tags, tags).length > 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const char = 'I'; | ||
|
||
// parses window.location and reads the story tags from query params | ||
// | ||
// note: we do not use the location.search prop here because the HashRouter | ||
// stores the query parameters in the location.hash prop | ||
export function parseUrl(): string[] { | ||
const {hash} = location; | ||
// only take the query portion of the hash string | ||
const queryString = hash.substr(hash.indexOf('?')); | ||
const urlParams = new URLSearchParams(queryString); | ||
const tagsParam = urlParams.get('tags'); | ||
|
||
if (!tagsParam) { | ||
return []; | ||
} | ||
|
||
return tagsParam.split(char); | ||
} | ||
|
||
export function getParamString(tags: string[]): string | null { | ||
if (tags.length === 0) { | ||
return null; | ||
} | ||
|
||
return tags.join(char); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { | ||
SET_SELECTED_STORY_TAGS, | ||
SetSelectedStoryTagsAction | ||
} from '../../actions/set-selected-story-tags'; | ||
import {parseUrl} from '../../libs/tags-url-parameter'; | ||
|
||
function selectedStoryTagsReducer( | ||
tagsState: string[] = parseUrl(), | ||
action: SetSelectedStoryTagsAction | ||
): string[] { | ||
switch (action.type) { | ||
case SET_SELECTED_STORY_TAGS: | ||
return action.tags; | ||
default: | ||
return tagsState; | ||
} | ||
} | ||
|
||
export default selectedStoryTagsReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {State} from '../../reducers/index'; | ||
|
||
export function selectedTagsSelector(state: State) { | ||
return state.stories.selectedTags; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters