This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
227 additions
and
107 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,82 +1,67 @@ | ||
import * as React from 'react'; | ||
import { Action } from 'redux'; | ||
import { connect, Dispatch } from 'react-redux'; | ||
|
||
export interface OptionsState { | ||
favoriteColor: string; | ||
likesColor: boolean; | ||
status: string; | ||
import { RootState } from './store'; | ||
import { State, updateFavoriteColor, updateLikesColor } from './module'; | ||
|
||
interface OptionsProps { | ||
values: State; | ||
actions: ActionDispatcher; | ||
} | ||
|
||
export class Options extends React.Component<{}, OptionsState> { | ||
constructor(props: {}) { | ||
super(props); | ||
this.state = { | ||
favoriteColor: 'red', | ||
likesColor: false, | ||
status: '', | ||
}; | ||
} | ||
componentDidMount() { | ||
chrome.storage.sync.get( | ||
{ | ||
favoriteColor: 'red', | ||
likesColor: false, | ||
}, | ||
items => { | ||
this.setState({ | ||
favoriteColor: items.favoriteColor, | ||
likesColor: items.likesColor, | ||
}); | ||
} | ||
); | ||
} | ||
changeFavoriteColor(event: React.ChangeEvent<HTMLSelectElement>) { | ||
this.setState({ | ||
favoriteColor: event.currentTarget.value, | ||
}); | ||
} | ||
changeLikesColor(event: React.ChangeEvent<HTMLInputElement>) { | ||
this.setState({ | ||
likesColor: event.currentTarget.checked, | ||
}); | ||
} | ||
save(event: React.MouseEvent<HTMLButtonElement>) { | ||
chrome.storage.sync.set( | ||
{ | ||
favoriteColor: this.state.favoriteColor, | ||
likesColor: this.state.likesColor, | ||
}, | ||
() => { | ||
// Update status to let user know options were saved. | ||
this.setState({ | ||
status: chrome.i18n.getMessage('optionsStatus'), | ||
}); | ||
setTimeout(() => { | ||
this.setState({ | ||
status: '', | ||
}); | ||
}, 750); | ||
} | ||
); | ||
class ActionDispatcher { | ||
constructor(private dispatch: (action: Action) => void) {} | ||
updateFavoriteColor(favoriteColor: string) { | ||
this.dispatch(updateFavoriteColor(favoriteColor)); | ||
} | ||
render() { | ||
return ( | ||
<section> | ||
{chrome.i18n.getMessage('optionsFavoriteColor')} | ||
<select id="color" value={this.state.favoriteColor} onChange={e => this.changeFavoriteColor(e)}> | ||
<option value="red">{chrome.i18n.getMessage('optionsColorsRed')}</option> | ||
<option value="green">{chrome.i18n.getMessage('optionsColorsGreen')}</option> | ||
<option value="blue">{chrome.i18n.getMessage('optionsColorsBlue')}</option> | ||
<option value="yellow">{chrome.i18n.getMessage('optionsColorsYellow')}</option> | ||
</select> | ||
<label> | ||
<input type="checkbox" id="like" checked={this.state.likesColor} onChange={e => this.changeLikesColor(e)} /> | ||
{chrome.i18n.getMessage('optionsLikesColor')} | ||
</label> | ||
<div id="status">{this.state.status}</div> | ||
<button id="save" onClick={e => this.save(e)}> | ||
{chrome.i18n.getMessage('optionsSave')} | ||
</button> | ||
</section> | ||
); | ||
updateLikesColor(likesColor: boolean) { | ||
this.dispatch(updateLikesColor(likesColor)); | ||
} | ||
} | ||
|
||
const FavoriteColor: React.SFC<OptionsProps> = props => { | ||
return ( | ||
<> | ||
{chrome.i18n.getMessage('optionsFavoriteColor')} | ||
<select | ||
id="color" | ||
value={props.values.favoriteColor} | ||
onChange={e => props.actions.updateFavoriteColor(e.currentTarget.value)} | ||
> | ||
<option value="red">{chrome.i18n.getMessage('optionsColorsRed')}</option> | ||
<option value="green">{chrome.i18n.getMessage('optionsColorsGreen')}</option> | ||
<option value="blue">{chrome.i18n.getMessage('optionsColorsBlue')}</option> | ||
<option value="yellow">{chrome.i18n.getMessage('optionsColorsYellow')}</option> | ||
</select> | ||
</> | ||
); | ||
}; | ||
|
||
const LikesColor: React.SFC<OptionsProps> = props => { | ||
return ( | ||
<label> | ||
<input | ||
type="checkbox" | ||
id="like" | ||
checked={props.values.likesColor} | ||
onChange={e => props.actions.updateLikesColor(e.currentTarget.checked)} | ||
/> | ||
{chrome.i18n.getMessage('optionsLikesColor')} | ||
</label> | ||
); | ||
}; | ||
|
||
const Options: React.SFC<OptionsProps> = props => { | ||
return ( | ||
<section> | ||
<FavoriteColor {...props} /> | ||
<LikesColor {...props} /> | ||
</section> | ||
); | ||
}; | ||
|
||
export const options = connect( | ||
(state: RootState) => ({ values: state.options }), | ||
(dispatch: Dispatch<Action>) => ({ actions: new ActionDispatcher(dispatch) }) | ||
)(Options); |
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,20 @@ | ||
import { createStore } from 'redux'; | ||
import * as module from '../module'; | ||
|
||
let store; | ||
|
||
describe('options module', () => { | ||
beforeEach(() => { | ||
store = createStore(module.reducer); | ||
}); | ||
it('should update favoriteColor only', () => { | ||
expect(store.getState()).toEqual(module.INITIAL_STATE); | ||
store.dispatch(module.updateFavoriteColor('yellow')); | ||
expect(store.getState()).toEqual({ favoriteColor: 'yellow', likesColor: false }); | ||
}); | ||
it('should update likesColor only', () => { | ||
expect(store.getState()).toEqual(module.INITIAL_STATE); | ||
store.dispatch(module.updateLikesColor(true)); | ||
expect(store.getState()).toEqual({ favoriteColor: 'red', likesColor: true }); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,6 +1,16 @@ | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import { Provider } from 'react-redux'; | ||
import { PersistGate } from 'redux-persist/integration/react'; | ||
|
||
import { Options } from './Options'; | ||
import { store, persistor } from './store'; | ||
import { options as Options } from './Options'; | ||
|
||
ReactDOM.render(<Options />, document.getElementById('app')); | ||
ReactDOM.render( | ||
<Provider store={store}> | ||
<PersistGate loading={null} persistor={persistor}> | ||
<Options /> | ||
</PersistGate> | ||
</Provider>, | ||
document.getElementById('app') | ||
); |
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,21 @@ | ||
import actionCreatorFactory from 'typescript-fsa'; | ||
import { reducerWithInitialState } from 'typescript-fsa-reducers'; | ||
const actionCreator = actionCreatorFactory(); | ||
|
||
export interface State { | ||
favoriteColor: string; | ||
likesColor: boolean; | ||
} | ||
|
||
export const INITIAL_STATE: State = { | ||
favoriteColor: 'red', | ||
likesColor: false, | ||
}; | ||
|
||
export const updateFavoriteColor = actionCreator<string>('UPDATE_FAVORITE_COLOR'); | ||
export const updateLikesColor = actionCreator<boolean>('UPDATE_LIKES_COLOR'); | ||
|
||
export const reducer = reducerWithInitialState(INITIAL_STATE) | ||
.case(updateFavoriteColor, (state, favoriteColor) => ({ ...state, favoriteColor })) | ||
.case(updateLikesColor, (state, likesColor) => ({ ...state, likesColor })) | ||
.build(); |
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,24 @@ | ||
import { Reducer, createStore, combineReducers } from 'redux'; | ||
import { persistStore, persistReducer } from 'redux-persist'; | ||
import storage from 'redux-persist/lib/storage'; | ||
|
||
import { reducer as options, State } from './module'; | ||
|
||
export type RootState = { | ||
options: State; | ||
}; | ||
|
||
const rootReducer: Reducer<RootState | void> = combineReducers({ | ||
options, | ||
}); | ||
|
||
const persistConfig = { | ||
key: 'root', | ||
storage, | ||
}; | ||
|
||
const persistedReducer = persistReducer(persistConfig, rootReducer); | ||
|
||
export const store = createStore(persistedReducer); | ||
|
||
export const persistor = persistStore(store); |
Oops, something went wrong.