Fakultas Ilmu Komputer UI

ProfilePage.jsx 8.53 KB
Newer Older
1
import React from 'react';
2
import { Segment, Image, Header, Icon, Checkbox, Container, Button, Form, Grid } from 'semantic-ui-react';
3
import Server from './lib/Server';
4
import Storage from './lib/Storage';
5
import ModalAlert from './components/ModalAlert';
6
import Dumper from './lib/Dumper';
7
8
9

export default class ProfilePage extends React.Component {

10
  static propTypes = {
11
12
    route: React.PropTypes.object.isRequired,
    params: React.PropTypes.object.isRequired,
13
    user: React.PropTypes.object.isRequired,
14
15
16
17
18
19
  };

  constructor(props) {
    super(props);
    /* istanbul ignore next */
    this.state = {
20
21
      id: '',
      npm: '',
22
23
24
25
26
27
28
      name: '',
      major: '',
      batch: '',
      email: '',
      cityOfBirth: '',
      dateOfBirth: '',
      resume: '',
29
30
31
      phone_number: '',
      show_transcript: '',
      photo: '',
32
33
      form: {
        picture: '',
34
        email: '',
35
        phone_number: '',
36
        resume: '',
37
        show_transcript: '',
38
      },
39
      bagikanTranskrip: '',
40
      acceptedNo: 0,
41
      refresh: 1,
Zamil Majdy's avatar
Zamil Majdy committed
42
      loading: false,
43
44
    };
    this.getProfile = this.getProfile.bind(this);
45
    this.handleChange = this.handleChange.bind(this);
46
    this.handleCheckbox = this.handleCheckbox.bind(this);
47
48
    this.handleSubmit = this.handleSubmit.bind(this);
    this.handleFile = this.handleFile.bind(this);
49
    this.gotoLink = this.gotoLink.bind(this);
M. Reza Qorib's avatar
M. Reza Qorib committed
50
    this.gotoStudentResume = this.gotoStudentResume.bind(this);
51
    this.getProfile();
52
53
54
  }

  getProfile() {
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    const id = this.props.route.own ? this.props.user.data.student.id : this.props.params.id;
    return Server.get(`/students/${id}/`).then((data) => {
      this.setState({
        id: data.id,
        name: data.name,
        npm: data.npm,
        resume: data.resume,
        major: data.major,
        batch: data.batch,
        email: data.user.email,
        cityOfBirth: data.birth_place,
        dateOfBirth: data.birth_date,
        phone_number: data.phone_number,
        photo: data.photo,
        show_transcript: data.show_transcript,
70
        acceptedNo: data.accepted_no,
71
72
        bagikanTranskrip: data.show_transcript,
        refresh: this.state.refresh + 1,
73
      });
74
75
76
77
78
79
      if (this.props.route.own) {
        const newSession = this.props.user.data;
        newSession.student = data;
        Storage.set('user-data', newSession);
        window.scrollTo(0, 0);
      }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    }, error => error.then(() => {
      this.state.name = 'Gagal mendapatkan informasi';
    }));
  }

  handleSubmit = (e) => {
    e.preventDefault();

    const submitForm = {};
    Object.keys(this.state.form).forEach((key) => {
      if (this.state.form[key] !== '') {
        submitForm[key] = this.state.form[key];
      }
    });
Zamil Majdy's avatar
Zamil Majdy committed
94
    this.setState({ loading: true });
95
    Server.submit(`/students/${this.state.id}/profile/`, submitForm, 'PATCH').then(() => {
Zamil Majdy's avatar
Zamil Majdy committed
96
      this.setState({ loading: false });
97
      this.modalAlert.open('Profil berhasil diperbaharui', 'Silakan periksa kembali profil anda', this.getProfile);
98
    }, error => error.then((r) => {
Zamil Majdy's avatar
Zamil Majdy committed
99
      this.setState({ loading: false });
100
      this.modalAlert.open('Pembaharuan profil gagal', Dumper.dump(r));
101
102
103
104
105
106
107
108
    }));
  };


  handleFile = (e) => {
    const form = this.state.form;
    form[e.target.name] = e.target.files[0];
    this.setState({ form });
109
  };
110

111
  handleChange = (e) => {
112
113
114
115
116
117
118
119
    const form = this.state.form;
    form[e.target.name] = e.target.value;
    this.setState({ form });
  };

  handleCheckbox = (e, d) => {
    const form = this.state.form;
    form[d.name] = d.checked;
120
    this.setState({ form, show_transcript: d.checked });
121
122
  };

123
124
125
126
127
  gotoLink = (url) => {
    const win = window.open(url);
    win.focus();
  };

M. Reza Qorib's avatar
M. Reza Qorib committed
128
129
  gotoStudentResume = () => this.gotoLink(this.state.resume || '#');

130
131
132
  updateForm(show) {
    if (show) {
      return (
133
        <Segment className="profile-form">
134
135
136
137
138
139
          <Header as="h3" textAlign="center">
            <Icon name="edit" />
            <Header.Content>
              Edit Profile Page
            </Header.Content>
          </Header>
140
          <ModalAlert ref={(modal) => { this.modalAlert = modal; }} />
141
          <Form ref={(input) => { this.form = input; }} key={this.state.refresh} size="small" onSubmit={this.handleSubmit}>
142
143
            <Form.Field>
              <label htmlFor="photo">Profile Picture</label>
144
              <input onChange={this.handleFile} placeholder="Profile Photo.jpg" name="photo" type="File" />
145
            </Form.Field>
146
147
            <Form.Field>
              <label htmlFor="email">Email</label>
148
              <input onChange={this.handleChange} placeholder="jojon@email.com" name="email" />
149
150
            </Form.Field>
            <Form.Field>
151
152
              <label htmlFor="phone_number">No. Hp</label>
              <input onChange={this.handleChange} placeholder="08123456789" name="phone_number" />
153
154
155
            </Form.Field>
            <Form.Field>
              <label htmlFor="resume">Resume</label>
156
              <input onChange={this.handleFile} placeholder="Resume" name="resume" type="File" />
157
            </Form.Field>
158
159
160
            <Form.Field>
              <Checkbox
                onChange={this.handleCheckbox}
sirinbaisa's avatar
sirinbaisa committed
161
                checked={!!this.state.show_transcript}
162
                label="Ijinkan perusahaan tempat saya mendaftar untuk melihat transkrip akademik saya"
163
                name="show_transcript"
164
165
              />
            </Form.Field>
Zamil Majdy's avatar
Zamil Majdy committed
166
            <Button type="submit" size="small" loading={this.state.loading} primary floated="right">Submit</Button>
167
          </Form>
168
        </Segment>
169
170
171
172
173
      );
    }

    return (<div />);
  }
174

175
  render() {
Zamil Majdy's avatar
Zamil Majdy committed
176
    const defaultPicture = 'https://semantic-ui.com/images/wireframe/square-image.png';
177
178
    return (
      <div className="profilePage">
sirinbaisa's avatar
sirinbaisa committed
179
        <Segment className="biodata-section" >
180
181
          <Grid stackable columns={2}>
            <Grid.Column>
sirinbaisa's avatar
sirinbaisa committed
182
              <Header as="h2" icon textAlign="center">
sirinbaisa's avatar
sirinbaisa committed
183
                <br />
184
                <Image src={this.state.photo || defaultPicture} size="medium" />
sirinbaisa's avatar
sirinbaisa committed
185
186
187
              </Header>
            </Grid.Column>

188
            <Grid.Column>
sirinbaisa's avatar
sirinbaisa committed
189
190
              <Container textAlign="left" className="profile-biodata">
                <div className="biodata">
M. Reza Qorib's avatar
M. Reza Qorib committed
191
192
193
194
195
196
197
198
199
200
                  <Segment basic textAlign="center">
                    <h1> { this.state.name } </h1>
                  </Segment>

                  <Segment basic vertical>
                    <Grid>
                      <Grid.Column width={2}>
                        <Icon name="university" size="big" />
                      </Grid.Column>
                      <Grid.Column width={13}>
201
                        <p> { this.state.major || 'N/A' }, { this.state.batch || 'N/A' } </p>
M. Reza Qorib's avatar
M. Reza Qorib committed
202
203
204
205
206
207
208
209
210
211
212
                      </Grid.Column>
                    </Grid>
                  </Segment>

                  <Segment basic vertical>

                    <Grid>
                      <Grid.Column width={2}>
                        <Icon name="mail" size="big" />
                      </Grid.Column>
                      <Grid.Column width={13}>
213
                        <p> { this.state.email || 'N/A' } </p>
M. Reza Qorib's avatar
M. Reza Qorib committed
214
215
216
217
218
219
220
221
222
223
                      </Grid.Column>
                    </Grid>
                  </Segment>

                  <Segment basic vertical>
                    <Grid>
                      <Grid.Column width={2}>
                        <Icon name="phone" size="big" />
                      </Grid.Column>
                      <Grid.Column width={13}>
224
                        <p> { this.state.phone_number || 'N/A' }</p>
M. Reza Qorib's avatar
M. Reza Qorib committed
225
226
227
228
229
230
231
232
233
234
                      </Grid.Column>
                    </Grid>
                  </Segment>

                  <Segment basic vertical>
                    <Grid>
                      <Grid.Column width={2}>
                        <Icon name="gift" size="big" />
                      </Grid.Column>
                      <Grid.Column width={13}>
235
                        <p> { this.state.cityOfBirth || 'N/A' }, { this.state.dateOfBirth || 'N/A' } </p>
M. Reza Qorib's avatar
M. Reza Qorib committed
236
237
238
                      </Grid.Column>
                    </Grid>
                  </Segment>
sirinbaisa's avatar
sirinbaisa committed
239
240
                </div>

sirinbaisa's avatar
sirinbaisa committed
241
                <Container textAlign="center">
Zamil Majdy's avatar
Zamil Majdy committed
242
                  <div className="buttonProfile">
Zamil Majdy's avatar
Zamil Majdy committed
243
                    <Button onClick={this.gotoStudentResume} disabled={!this.state.resume} primary size="small">Resume</Button>
sirinbaisa's avatar
sirinbaisa committed
244
                  </div>
245
                  <br />
sirinbaisa's avatar
sirinbaisa committed
246
                  <div>
M. Reza Qorib's avatar
M. Reza Qorib committed
247
                    <h4> Bagikan Transkrip : { this.state.bagikanTranskrip ? 'Ya' : 'Tidak' }</h4>
sirinbaisa's avatar
sirinbaisa committed
248
249
250
                  </div>
                </Container>
              </Container>
sirinbaisa's avatar
sirinbaisa committed
251
252
            </Grid.Column >
          </Grid>
253
254
        </Segment >
        { this.updateForm(this.props.route.own) }
255
256
257
258
      </div>
    );
  }
}