Step2.tsx 4.18 KB
Newer Older
Sam Gluck's avatar
Sam Gluck committed
1
2
3
4
5
import * as React from 'react';
import { Col, Row } from '@zendeskgarden/react-grid';

import H6 from '../../components/typography/H6/H6';
import P from '../../components/typography/P/P';
6
7
import TextInput from '../../components/inputs/Text/Text';
import Button from '../../components/elements/Button/Button';
8
import Tag, { TagContainer } from '../../components/elements/Tag/Tag';
9
10
import styled from '../../themes/styled';
import User from '../../types/User';
Sam Gluck's avatar
Sam Gluck committed
11
12

//TODO get tags from the API
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const words = `offer,segment,slave,duck,instant,market,degree,populate,chick,dear,enemy,reply,drink,occur,support,shell,neck`.split(
  ','
);

const InterestsSearchResultsContainer = styled.div`
  margin: 20px 0 0 0;
`;

function InterestsSearchResults({ status, count, result, children }) {
  if (status === SearchStatus.complete) {
    return (
      <InterestsSearchResultsContainer>
        <P style={{ fontWeight: 'bold' }}>{count} Search Results</P>
        {children}
      </InterestsSearchResultsContainer>
    );
  }
  if (status === SearchStatus.in_progress) {
    return (
      <InterestsSearchResultsContainer>
        Searching...
      </InterestsSearchResultsContainer>
    );
Sam Gluck's avatar
Sam Gluck committed
36
  }
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  if (status === SearchStatus.error) {
    return (
      <InterestsSearchResultsContainer>
        Could not search at this time, please try again later. ({result.message}
        )
      </InterestsSearchResultsContainer>
    );
  }
  // status === SearchStatus.idle
  return null;
}

enum SearchStatus {
  idle,
  in_progress,
  complete,
  error
Sam Gluck's avatar
Sam Gluck committed
54
55
}

56
57
58
type Step2Props = {
  user: User;
  toggleInterest: Function;
Sam Gluck's avatar
Sam Gluck committed
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

type Step2State = {
  interestsSearch: {
    status: SearchStatus;
    count: number;
    result: any;
  };
};

export default class extends React.Component<Step2Props, Step2State> {
  _searchTimeout: number = -1;

  state = {
    interestsSearch: {
      status: SearchStatus.idle,
      count: -1,
      result: null
    }
  };

  constructor(props) {
    super(props);
    this.onInterestsSearchSubmit = this.onInterestsSearchSubmit.bind(this);
  }

  // TODO search using API
  onInterestsSearchSubmit(e) {
    if (this._searchTimeout) {
      clearTimeout(this._searchTimeout);
    }

    e.preventDefault();

    this.setState({
      interestsSearch: {
        status: SearchStatus.in_progress,
        count: -1,
        result: null
      }
    });

    this._searchTimeout = window.setTimeout(() => {
      this.setState({
        interestsSearch: {
          status: SearchStatus.complete,
          count: words.length,
          result: words
        }
      });
    }, 2000);
  }

  render() {
    const { user, toggleInterest } = this.props;

    return (
      <>
        <Row>
          <Col>
            <H6 style={{ borderBottom: '1px solid lightgrey' }}>
              <span style={{ color: 'darkgrey', fontSize: '.7em' }}>2.</span>{' '}
              Your Interests
            </H6>
            <P>
              Tell us what you're interested in so we can make your MoodleNet
              experience tailored to you.
            </P>
            <form onSubmit={this.onInterestsSearchSubmit}>
              <TextInput
                placeholder="Search for tags"
                button={<Button type="submit">Search</Button>}
              />
            </form>
            <InterestsSearchResults {...this.state.interestsSearch}>
              <TagContainer>
                {words.map(word => (
                  <Tag
                    focused={user.interests.includes(word)}
                    key={word}
                    onClick={() => toggleInterest(word)}
                  >
                    {word}
                  </Tag>
                ))}
              </TagContainer>
            </InterestsSearchResults>
            <P style={{ fontWeight: 'bold' }}>Popular on MoodleNet</P>
            <P>These tags are popular on MoodleNet.</P>
            <TagContainer>
              {words.map(word => (
                <Tag
                  focused={user.interests.includes(word)}
                  key={word}
                  onClick={() => toggleInterest(word)}
                >
                  {word}
                </Tag>
              ))}
            </TagContainer>
          </Col>
        </Row>
      </>
    );
  }
}