Skip to content

Instantly share code, notes, and snippets.

@ypcode
Created November 17, 2019 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypcode/ba8978cbc3634afb191c399608aa5a69 to your computer and use it in GitHub Desktop.
Save ypcode/ba8978cbc3634afb191c399608aa5a69 to your computer and use it in GitHub Desktop.
import * as React from 'react';
import styles from './UserSettingsSample.module.scss';
import { IUserSettingsSampleProps } from './IUserSettingsSampleProps';
import { ISuperHero, SUPER_HEROES } from '../data/superheroes';
import { UserPreferencesServiceKey, IUserPreferences } from '../../../services/UserPreferencesService';
export interface IUserSettingsSampleState {
favorite: string;
}
export default class UserSettingsSample extends React.Component<IUserSettingsSampleProps, IUserSettingsSampleState> {
private userPreferences: IUserPreferences = null;
constructor(props: IUserSettingsSampleProps) {
super(props);
this.userPreferences = this.props.serviceScope.consume(UserPreferencesServiceKey);
this.state = {
favorite: this.userPreferences.favoriteSuperHero
};
}
private _isFavorite(superHero: ISuperHero): boolean {
return this.userPreferences.favoriteSuperHero == superHero.name;
}
private _toggleFavorite(superHero: ISuperHero) {
console.log("Toggle favorite super hero: ", superHero);
if (this._isFavorite(superHero)) {
this.userPreferences.favoriteSuperHero = null;
} else {
this.userPreferences.favoriteSuperHero = superHero.name;
}
this.setState({ favorite: this.userPreferences.favoriteSuperHero });
}
private _renderSuperHero(key: string, superHero: ISuperHero): JSX.Element {
return <div key={key} className={`${styles.superHero} ${this._isFavorite(superHero) ? styles.favorite : ""}`} onClick={() => this._toggleFavorite(superHero)}>
<img className={styles.picture} src={`${superHero.picture}`} />
<p className={styles.title}>{superHero.name}</p>
</div>;
}
public render(): React.ReactElement<IUserSettingsSampleProps> {
return (
<div className={styles.userSettingsSample}>
<div className={styles.container}>
<h1>What is your favorite super hero ?</h1>
{SUPER_HEROES.map((superHero, ndx) => this._renderSuperHero(`super_hero_${ndx}`, superHero))}
</div>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment