summaryrefslogtreecommitdiff
path: root/src/containers/feed/index.js
blob: b7272bf18f46f17058a2c919f739a0ea4b46c103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import React from 'react';
import moment from 'moment';
import { connect } from 'react-redux';

import './styles.css';
import Loader from '../../components/loader';
import TopNav from '../../components/top-nav';
import Filters from '../../components/filters';
import { fetchTrending } from '../../redux/github/actions';
import RepositoryGrid from '../../components/repository-grid';
import RepositoryList from '../../components/repository-list';
import { updateDateJump, updateLanguage, updateViewType } from '../../redux/preference/actions';
import GroupHeading from '../../components/group-heading';

class FeedContainer extends React.Component {
  componentDidMount() {
    const existingRepositories = this.props.github.repositories || [];
    // If there are no loaded repositories before, fetch them
    if (existingRepositories.length === 0) {
      this.fetchNextRepositories();
    }
  }

  fetchNextRepositories() {
    const filters = this.getFilters();
    this.props.fetchTrending(filters);
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    const currPreferences = this.props.preference;
    const prevPreferences = prevProps.preference;

    // If language or dateJump has been updated, reload
    // the repositories
    if (currPreferences.language !== prevPreferences.language ||
      currPreferences.dateJump !== prevPreferences.dateJump) {
      this.fetchNextRepositories();
    }
  }

  getFilters() {
    const filters = {};

    filters.dateRange = this.getNextDateRange();

    if (this.props.preference.language) {
      filters.language = this.props.preference.language;
    }

    if (this.props.preference.options.token) {
      filters.token = this.props.preference.options.token;
    }

    return filters;
  }

  getNextDateRange() {
    const repositories = this.props.github.repositories || [];
    const dateJump = this.props.preference.dateJump;

    const dateRange = {};
    const lastRecords = repositories[repositories.length - 1];

    if (lastRecords) {
      dateRange.start = moment(lastRecords.start).subtract(1, dateJump);
      dateRange.end = lastRecords.start;
    } else {
      dateRange.start = moment().add(1, 'day').subtract(1, dateJump);
      dateRange.end = moment();
    }

    return dateRange;
  }

  render() {
    return (
      <div className="page-wrap">
        <TopNav
          updateDateJump={ this.props.updateDateJump }
          selectedDateJump={ this.props.preference.dateJump }
        />

        <div className="container mt-4 mb-5 pb-4">
          <div className="header-row clearfix">
            <GroupHeading/>
            <div className="group-filters">
              <Filters
                selectedLanguage={ this.props.preference.language }
                selectedViewType={ this.props.preference.viewType }
                updateLanguage={ this.props.updateLanguage }
                updateViewType={ this.props.updateViewType }
              />
            </div>
          </div>
          <div className="body-row">
            {
              this.props.preference.viewType === 'grid'
                ? <RepositoryGrid repositories={ this.props.github.repositories || [] }/>
                : <RepositoryList repositories={ this.props.github.repositories || [] }/>
            }

            { this.props.github.processing && <Loader/> }
          </div>
        </div>
      </div>
    );
  }
}

const mapStateToProps = store => {
  return {
    preference: store.preference,
    github: store.github
  };
};

const mapDispatchToProps = {
  updateLanguage,
  updateViewType,
  updateDateJump,
  fetchTrending
};

export default connect(mapStateToProps, mapDispatchToProps)(FeedContainer);