<commit_before>'use strict';
/**
 * @file
 * Currently the main entrypoint served on /work.
 * Provides the work view for the enduser.
 */
import React, {PropTypes} from 'react';

import {find} from 'lodash';

// Stores
import LibraryStore from '../../stores/Library.store.js';
import ProfileStore from '../../stores/Profile.store.js';

// Actions
import LibraryActions from '../../actions/Library.action.js';
import ProfileActions from '../../actions/Profile.action.js';

class Library extends React.Component {
  static displayName() {
    return 'Library.component';
  }

  static propTypes() {
    return {
      id: PropTypes.string.isRequired,
      libData: PropTypes.object
    };
  }

  constructor() {
    super();

    this.state = {
      library: LibraryStore.store,
      profile: ProfileStore.getProfile()
    };

    this.unsubscribe = [
      LibraryStore.listen(
        () => this.setState({
          library: LibraryStore.store
        })
      ),
      ProfileStore.listen(
        () => this.setState({
          profile: ProfileStore.getProfile()
        })
      )
    ];
  }

  componentWillMount() {
    this.state.library.data = this.props.libData ? this.props.libData : this.state.library.data;
  }

  componentDidMount() {
    LibraryActions.libraryIdUpdated.trigger(this.props.id);
  }

  componentWillUnmount() {
    this.unsubscribe.forEach((unsubscriber) => {
      unsubscriber();
    });
  }

  shouldDisableFavoriteButton() {
    return (find(this.state.profile.favoriteLibraries, 'agencyID', this.state.library.data.branchId));
  }

  addOrRemoveFromFavorites() {
    if (this.shouldDisableFavoriteButton()) {
      ProfileActions.removeLibraryFromFavorites(this.state.library.data.branchId);
    }
    else {
      ProfileActions.addLibraryToFavorites(this.state.library.data.branchId, this.state.library.data.agencyId);
    }
  }

  goBack() {
    window.history.back();
  }

  render() {
    const shouldDisableFavoriteButton = this.shouldDisableFavoriteButton();
    const favoriteButton = this.state.profile.userIsLoggedIn ? (
      <a className={shouldDisableFavoriteButton ? 'button alert' : 'button'} onClick={this.addOrRemoveFromFavorites}>
        {shouldDisableFavoriteButton ? 'Fjern biblioteket som favoritbibliotek' : 'Tilføj bibliotek til favoritter!'}
      </a>
    ) : ''; // Show favorite button if user is logged in, otherwise don't show.

    return (
      <div className='library'>
        <a className='button tiny' onClick={this.goBack}>Tilbage!</a>
        <p>{this.state.library.data.agencyName}</p>
        <p>{this.state.library.data.agencyId}</p>
        <p>{this.state.library.data.branchEmail}</p>
        <p>{this.state.library.data.branchId}</p>
        <p>{this.state.library.data.branchNameDan}</p>
        <p>{this.state.library.data.branchPhone}</p>
        <p>{this.state.library.data.branchWebsiteUrl}</p>
        <p>{this.state.library.data.city}</p>
        <p>{this.state.library.data.openingHoursDan}</p>
        <p>{this.state.library.data.postalAddress}</p>
        <p>{this.state.library.data.postalCode}</p>
        {favoriteButton}<commit_msg>us31: fix favorite button on library<commit_after>'use strict';
/**
 * @file
 * Currently the main entrypoint served on /work.
 * Provides the work view for the enduser.
 */
import React, {PropTypes} from 'react';

import {find} from 'lodash';

// Stores
import LibraryStore from '../../stores/Library.store.js';
import ProfileStore from '../../stores/Profile.store.js';

// Actions
import LibraryActions from '../../actions/Library.action.js';
import ProfileActions from '../../actions/Profile.action.js';

class Library extends React.Component {
  static displayName() {
    return 'Library.component';
  }

  static propTypes() {
    return {
      id: PropTypes.string.isRequired,
      libData: PropTypes.object
    };
  }

  constructor() {
    super();

    this.state = {
      library: LibraryStore.store,
      profile: ProfileStore.getProfile()
    };

    this.unsubscribe = [
      LibraryStore.listen(
        () => this.setState({
          library: LibraryStore.store
        })
      ),
      ProfileStore.listen(
        () => this.setState({
          profile: ProfileStore.getProfile()
        })
      )
    ];
  }

  componentWillMount() {
    this.state.library.data = this.props.libData ? this.props.libData : this.state.library.data;
  }

  componentDidMount() {
    LibraryActions.libraryIdUpdated.trigger(this.props.id);
  }

  componentWillUnmount() {
    this.unsubscribe.forEach((unsubscriber) => {
      unsubscriber();
    });
  }

  shouldDisableFavoriteButton() {
    return (find(this.state.profile.favoriteLibraries, 'agencyID', this.state.library.data.branchId));
  }

  addOrRemoveFromFavorites(self) {
    return () => {
      if (self.shouldDisableFavoriteButton()) {
        ProfileActions.removeLibraryFromFavorites(this.state.library.data.branchId);
      }
      else {
        ProfileActions.addLibraryToFavorites(this.state.library.data.branchId, this.state.library.data.agencyId);
      }
    };
  }

  goBack() {
    window.history.back();
  }

  render() {
    const shouldDisableFavoriteButton = this.shouldDisableFavoriteButton();
    const favoriteButton = this.state.profile.userIsLoggedIn ? (
      <a className={shouldDisableFavoriteButton ? 'button alert' : 'button'} onClick={this.addOrRemoveFromFavorites(this)}>
        {shouldDisableFavoriteButton ? 'Fjern biblioteket som favoritbibliotek' : 'Tilføj bibliotek til favoritter!'}
      </a>
    ) : ''; // Show favorite button if user is logged in, otherwise don't show.

    return (
      <div className='library'>
        <a className='button tiny' onClick={this.goBack}>Tilbage!</a>
        <p>{this.state.library.data.agencyName}</p>
        <p>{this.state.library.data.agencyId}</p>
        <p>{this.state.library.data.branchEmail}</p>
        <p>{this.state.library.data.branchId}</p>
        <p>{this.state.library.data.branchNameDan}</p>
        <p>{this.state.library.data.branchPhone}</p>
        <p>{this.state.library.data.branchWebsiteUrl}</p>
        <p>{this.state.library.data.city}</p>
        <p>{this.state.library.data.openingHoursDan}</p>
        <p>{this.state.library.data.postalAddress}</p>
        <p>{this.state.library.data.postalCode}</p>
        {favoriteButton}
{'fraction_non_alphanumeric': 0.14752803664507977, 'fraction_numerical': 0.000315905860053704, 'mean_word_length': 2.9949526813880127, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '973278', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1934, 'n_words': 309}