Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.70 KB | None | 0 0
  1. import {Col, Button, Form, FormGroup, Label} from 'react-bootstrap';
  2. import {CountyService, UserService, getCounties} from "../../services";
  3. import {Component} from 'react';
  4. import * as React from 'react';
  5. import {Alert} from "../../widgets";
  6. import ReactDOM from 'react-dom';
  7. import {County} from "../../classTypes";
  8. import DropdownButton from "react-bootstrap/es/DropdownButton";
  9. import MenuItem from "react-bootstrap/es/MenuItem";
  10. import Grid from "react-bootstrap/es/Grid";
  11. import {FormControl, PageHeader} from "react-bootstrap";
  12. import Checkbox from "react-bootstrap/es/Checkbox";
  13. import Select from "react-select";
  14.  
  15. let countyService = new CountyService();
  16. let userService = new UserService();
  17.  
  18. interface State{
  19. mail: string;
  20. firstName: string;
  21. lastName: string;
  22. password: string;
  23. password2: string;
  24. typeName: string;
  25. phone: string;
  26. points: number;
  27. countyId: number;
  28. countyName: string;
  29. active: number;
  30. isLoaded: boolean;
  31. }
  32. interface Props{
  33. match: Object,
  34. }
  35.  
  36. /*class BindDropDown extends Component {
  37. constructor(props) {
  38. super(props);
  39. this.state = {
  40. choosen: {name: "Bergen", countyId: 1},
  41. values:[
  42. {name: "Bergen", countyId: 1}
  43. //{ name: this.county.name, countyId: this.county.countyId}
  44. ]
  45. }
  46.  
  47. this.handleChangeCounty = this.handleChangeCounty.bind(this)
  48. }
  49.  
  50. handleChangeCounty(e: Object){
  51. console.log(this.state.choosen.countyId)
  52. this.setState({
  53. choosen: JSON.parse(e.target.value)
  54. })
  55.  
  56.  
  57. };
  58.  
  59. componentWillMount() {
  60. var arr = [];
  61. countyService
  62. .getCounties()
  63. .then(county2 => {
  64. county2.map(e => {
  65. var elem = {
  66. name: e.name,
  67. countyId: e.countyId
  68. };
  69. arr = arr.concat(elem)
  70.  
  71. });
  72. this.setState({
  73. values: arr
  74. })
  75. })
  76.  
  77.  
  78. //this.state.countyName.push(this.state.county.name)})
  79. .catch((error: Error)=>Alert.danger(error.message))
  80.  
  81. }
  82.  
  83.  
  84.  
  85. render(){
  86. let optionTemplate = this.state.values.map(v => {
  87. var data = {name: v.name, countyId: v.countyId}
  88. return(<option key={v.countyId} value={JSON.stringify(data)}> {v.name}</option>)
  89. });
  90. return (
  91. <label>
  92. Velg Kommune:
  93. <select value={this.state.values.countyId} onChange={this.handleChangeCounty}>
  94. {optionTemplate}
  95. </select>
  96. </label>
  97. )
  98. }
  99. }*/
  100.  
  101. export class RegisterUser extends Component<Props, State>{
  102.  
  103. /*state = {
  104. mail: "",
  105. firstName: "",
  106. lastName: "",
  107. postnumber: 0,
  108. password: "",
  109. password2: "",
  110. typeName: "",
  111. phone: 0,
  112. points: 0,
  113. active: 0,
  114. isLoaded: false,
  115. };*/
  116. constructor(props) {
  117. super(props);
  118. this.state = {
  119. mail: "",
  120. firstName: "",
  121. lastName: "",
  122. postnumber: 0,
  123. password: "",
  124. password2: "",
  125. typeName: "",
  126. phone: "",
  127. points: 0,
  128. active: 0,
  129. isLoaded: false,
  130. choosen: {label: "Bergen", countyId: 1},
  131. values:[
  132. {label: "Bergen", countyId: 1}
  133. //{ name: this.county.name, countyId: this.county.countyId}
  134. ]
  135. }
  136.  
  137. this.handleChangeCounty = this.handleChangeCounty.bind(this)
  138. }
  139.  
  140.  
  141. handleChangeCounty(e: Object){
  142. console.log(this.state.choosen.countyId)
  143. this.setState({
  144. choosen: JSON.parse(e.target.value)
  145. })
  146.  
  147.  
  148. };
  149.  
  150. componentWillMount() {
  151. var arr = [];
  152. countyService
  153. .getCounties()
  154. .then(county2 => {
  155. county2.map(e => {
  156. var elem = {
  157. name: e.name,
  158. countyId: e.countyId
  159. };
  160. arr = arr.concat(elem)
  161.  
  162. });
  163. this.setState({
  164. values: arr
  165. })
  166. })
  167.  
  168.  
  169. //this.state.countyName.push(this.state.county.name)})
  170. .catch((error: Error)=>Alert.danger(error.message))
  171.  
  172. }
  173.  
  174. handleStringChange = (name: string) =>(event:SyntheticEvent<HTMLInputElement>)=>{
  175. this.setState({
  176. // $FlowFixMe
  177. [name]:event.target.value,
  178. })
  179. };
  180.  
  181. handleNumberChange = (value: number) =>(event:SyntheticEvent<HTMLInputElement>)=>{
  182. this.setState({
  183. // $FlowFixMe
  184. [value]:event.target.value,
  185. })
  186. };
  187.  
  188. /*;*/
  189. /*;*/
  190.  
  191. /*let optionTemplate = this.state.values.map(v => {
  192. var data = {label: v.name, countyId: v.countyId}
  193. return( <Select
  194. placeholder={"Hjemmekommune"}
  195. name="colors"
  196. options={optiontem}
  197. className="basic-multi-select"
  198. classNamePrefix="select"
  199. />)})*/
  200. render(){
  201. let optionTemplate = this.state.values.map(v => {
  202. var data = {label: v.name, value: v.countyId, countyId: v.countyId}
  203. return(data)
  204. })
  205. return(
  206. <Grid>
  207. <Col md={3}></Col>
  208. <Col md={6}>
  209. <Form horizontal>
  210. <FormGroup controlId="formHorizontalEmail">
  211. <FormGroup>
  212. <FormGroup>
  213. <Col md={3}></Col>
  214. <Col md={6}>
  215. <PageHeader>
  216. Registrer bruker
  217. </PageHeader>
  218. </Col>
  219. <Col md={3}></Col>
  220. </FormGroup>
  221. <Col md={6}>
  222. <FormGroup>
  223. <FormControl type="text" value={this.state.firstName} placeholder="Fornavn"
  224. onChange={this.handleStringChange("firstName")}
  225. />
  226. </FormGroup>
  227. </Col>
  228. <Col md={6}>
  229. <FormGroup>
  230. <FormControl type="text" value={this.state.lastName} placeholder="Etternavn"
  231. onChange={this.handleStringChange("lastName")}/>
  232. </FormGroup>
  233. </Col>
  234. <Col md={6}>
  235. <FormGroup>
  236. <FormControl type="number" value={this.state.phone} placeholder="Telefonnummer"
  237. onChange={this.handleStringChange("phone")}
  238. />
  239. </FormGroup>
  240. </Col>
  241. <Col md={6}>
  242. <FormGroup>
  243. <FormControl type="text" value={this.state.mail} placeholder="Epost"
  244. onChange={this.handleStringChange("mail")}/>
  245. </FormGroup>
  246. </Col>
  247. <Col md={6}>
  248. <FormGroup>
  249. <FormControl type="password" value={this.state.password} placeholder="Passord"
  250. onChange={this.handleStringChange("password")}
  251. />
  252. </FormGroup>
  253. </Col>
  254. <Col md={6}>
  255. <FormGroup>
  256. <FormControl type="password" value={this.state.password2} placeholder="Gjenta passord"
  257. onChange={this.handleStringChange("password2")}/>
  258. </FormGroup>
  259. </Col>
  260. </FormGroup>
  261. <FormGroup>
  262. <Col md={4}>
  263. <FormGroup>
  264. <Label>
  265. Hjemmekommune
  266. </Label>
  267. </FormGroup>
  268. <FormGroup>
  269. <Select
  270. placeholder={"Hjemmekommune"}
  271. name="colors"
  272. options={optionTemplate}
  273. className="basic-multi-select"
  274. classNamePrefix="select"
  275. onChange={console.log(this.state.choosen.countyId)}
  276. />
  277. </FormGroup>
  278. </Col>
  279. <Col md={4}>
  280. <FormGroup>
  281. <Label>Velg kommuner du vil følge</Label>
  282. </FormGroup>
  283. <FormGroup>
  284. <Checkbox inline>Oslo</Checkbox><Checkbox>Trondheim</Checkbox><Checkbox>Bergen</Checkbox>
  285. </FormGroup>
  286. </Col>
  287. <Col md={4}>
  288. <FormGroup>
  289. <Label>Velg arbeidsområder</Label>
  290. </FormGroup>
  291. <FormGroup>
  292. <Checkbox inline>Vann og avløp</Checkbox><Checkbox>Veiarbeid</Checkbox><Checkbox>Strømbrudd</Checkbox>
  293. </FormGroup>
  294. </Col>
  295. </FormGroup>
  296. <FormGroup>
  297. <Col md={4}/>
  298. <Col md ={4}>
  299. <Button type="button" onClick={this.checkMail}>Registrer</Button>
  300. </Col>
  301. <Col md={4}>
  302. </Col>
  303. </FormGroup>
  304. </FormGroup>
  305. </Form>
  306. </Col>
  307. <Col md={3}></Col>
  308. </Grid>
  309. );
  310. }
  311. checkMail = () =>{
  312. var validator = require("email-validator");
  313.  
  314. if(!(validator.validate(this.state.mail))){
  315. Alert.warning("Eposten eksisterer ikke")
  316. }else{
  317. this.register();
  318. }
  319. }
  320.  
  321. checkPass = () => {
  322.  
  323. if (this.state.password !== this.state.password2) {
  324. console.log("To ulike passord");
  325. Alert.warning("Du skrev to ulike passord");
  326. }
  327. else {
  328. let decimal = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
  329. if(this.state.password.match(decimal))
  330. {
  331. this.register();
  332. }
  333. else
  334. {
  335. Alert.warning('Password has to be between 8 to 15 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character')
  336. }
  337. }
  338.  
  339. }
  340.  
  341.  
  342.  
  343.  
  344. register = () => {
  345. console.log("test", this.state);
  346. var mail = this.state.mail
  347. var firstName = this.state.firstName
  348. var lastName = this.state.lastName
  349. var password = this.state.password
  350. var phone = this.state.phone
  351. var countyId = this.state.values.countyId
  352. console.log("county", data.countyId)
  353. userService
  354. .addUser(this.state.mail, this.state.firstName, this.state.lastName, this.state.password, this.state.phone, this.state.values.countyId)
  355. .then(user =>(this.state = user)).then(Alert.success("Bruker registrert"))
  356. .catch((error: Error)=>Alert.danger(error.message))
  357. };
  358. }
  359. /*
  360. * <DropdownButton title="Hjemmekommune">
  361. value={this.state.countyName}
  362. onChange={(e)=>this.handleChangeCounty(e)}>
  363. {this.state.countyName.map((r, i) => {
  364. return <option key={i} value={r}>{r}
  365. </option>
  366. })
  367. }
  368.  
  369. </DropdownButton>
  370.  
  371. checkPass(){
  372. console.log(state);
  373. if(this.state.password==this.state.password2){
  374. this.register();
  375. Alert.success();
  376. }else{
  377. Alert.warning();
  378. }
  379. }
  380.  
  381.  
  382. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement