From dfc575b66cc581a59b6f4a94f29ebbc24ccdb25b Mon Sep 17 00:00:00 2001
From: Zamil Majdy <z.majdy1996@gmail.com>
Date: Thu, 30 Mar 2017 01:47:29 +0700
Subject: [PATCH] revised company register

---
 assets/js/components/RegisterModal.jsx | 43 ++++++++++++++++++++------
 assets/js/components/TopMenu.jsx       |  2 +-
 core/views/accounts.py                 |  8 ++---
 3 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/assets/js/components/RegisterModal.jsx b/assets/js/components/RegisterModal.jsx
index 225c3181..06d91044 100644
--- a/assets/js/components/RegisterModal.jsx
+++ b/assets/js/components/RegisterModal.jsx
@@ -1,6 +1,8 @@
 import React from 'react';
+import { browserHistory } from 'react-router';
 import { Modal, Button, Form, Input, TextArea, Header, Icon } from 'semantic-ui-react';
 import Server from './../lib/Server';
+import Storage from './../lib/Storage';
 
 export default class RegisterModal extends React.Component {
 
@@ -15,6 +17,19 @@ export default class RegisterModal extends React.Component {
     this.setState({ [e.target.name]: e.target.value });
   };
 
+  handlePassword = (e) => {
+    if (e.target.name === 'password') this.passwordField = e.target; else
+    if (e.target.name === 'password-confirm') this.passwordConfirmField = e.target;
+    const isExist = this.passwordField && this.passwordConfirmField;
+    if (isExist) {
+      if (this.passwordField.value !== this.passwordConfirmField.value) {
+        this.passwordConfirmField.setCustomValidity("Passwords Don't Match");
+      } else {
+        this.passwordConfirmField.setCustomValidity('');
+      }
+    }
+  };
+
   handleSubmit = (e) => {
     e.preventDefault();
     const form = new FormData();
@@ -29,10 +44,15 @@ export default class RegisterModal extends React.Component {
     };
 
     fetch('/api/register/', data)
-      .then(r => r.json())
-      .then((res) => {
-        alert(res);
-      });
+      .then((response) => {
+        if (response.status < 200 || response.status > 399) throw response.json();
+        else return response.json();
+      })
+      .then((response) => {
+        alert('Akun berhasil dibuat :)');
+        Storage.set('user-data', response);
+        browserHistory.push('/home');
+      }, error => error.then(r => alert(JSON.stringify(r))));
   };
 
   render = () => (
@@ -49,22 +69,28 @@ export default class RegisterModal extends React.Component {
           </Header>
           <Form.Field required>
             <label htmlFor="email">Email</label>
-            <Input onChange={this.handleChange} type="text" name="email" icon="user" iconPosition="left" placeholder="email" required />
+            <Input onChange={this.handleChange} type="email" name="email" icon="user" iconPosition="left" placeholder="email" required />
           </Form.Field>
           <Form.Field required>
             <label htmlFor="password">Password</label>
-            <Input onChange={this.handleChange} type="password" name="password" icon="key" iconPosition="left" placeholder="password" required />
+            <Input
+              onChange={(e) => { this.handleChange(e); this.handlePassword(e); }}
+              type="password" id="password" name="password" icon="key" iconPosition="left" placeholder="password" required
+            />
           </Form.Field>
           <Form.Field required>
             <label htmlFor="password-confirm">Konfirmasi Password</label>
-            <Input onChange={this.handleChange} type="password" name="password-confirm" icon="key" iconPosition="left" placeholder="password" required />
+            <Input
+              onChange={(e) => { this.handleChange(e); this.handlePassword(e); }}
+              type="password" id="password-confirm" name="password-confirm" icon="key" iconPosition="left" placeholder="password" required
+            />
           </Form.Field>
 
           <Form.Field required>
             <label htmlFor="name">Nama Perusahaan</label>
             <Input onChange={this.handleChange} placeholder="Nama Perusahaan" name="name" required />
           </Form.Field>
-          <Form.Field required>
+          <Form.Field>
             <label htmlFor="logo">Logo</label>
             <Input
               onChange={this.handleChange}
@@ -72,7 +98,6 @@ export default class RegisterModal extends React.Component {
               icon={{ name: 'attach', circular: true, link: true }}
               placeholder="attach logo"
               type="File"
-              required
             />
           </Form.Field>
           <Form.Field required>
diff --git a/assets/js/components/TopMenu.jsx b/assets/js/components/TopMenu.jsx
index 6e180677..ad5b6bed 100644
--- a/assets/js/components/TopMenu.jsx
+++ b/assets/js/components/TopMenu.jsx
@@ -29,7 +29,7 @@ export default class TopMenu extends React.Component {
       <Menu color="blue" pointing secondary>
         <Image as="a" size="small" src="/assets/img/logo.png" href="/" />
         <Menu.Menu position="right">
-          <Menu.Item as={Link} to="/lowongan" name="home" active={activeItem === 'home'} onClick={this.handleItemClick} />
+          <Menu.Item as={Link} to="/home" name="home" active={activeItem === 'home'} onClick={this.handleItemClick} />
           <Menu.Item as={Link} to="/profile" name="profil" active={activeItem === 'profil'} onClick={this.handleItemClick} />
           <Menu.Item as={Link} onClick={this.logout} name="logout" />
         </Menu.Menu>
diff --git a/core/views/accounts.py b/core/views/accounts.py
index 1aab4a1c..4afa426a 100644
--- a/core/views/accounts.py
+++ b/core/views/accounts.py
@@ -1,8 +1,6 @@
 import requests
 from django.contrib.auth import authenticate, login
 from django.contrib.auth.models import User
-from django.http import HttpResponseBadRequest
-from django.http import HttpResponseNotAllowed
 from rest_framework import viewsets, status
 from rest_framework.decorators import list_route
 from rest_framework.parsers import FormParser,MultiPartParser
@@ -151,11 +149,11 @@ class CompanyRegisterViewSet(viewsets.GenericViewSet):
               required: true
               type: string
             - name: password
-              description: password of the new acoount
+              description: password of the new account
               required: true
               type: string
             - name: email
-              description: email address of the new acoount
+              description: email address of the new account
               required: true
               type: string
             - name: name
@@ -171,7 +169,7 @@ class CompanyRegisterViewSet(viewsets.GenericViewSet):
               required: false
               type: image
             - name: address
-              description: address of the new acoount
+              description: address of the new account
               required: false
               type: string
         """
-- 
GitLab