Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.73 KB | None | 0 0
  1. import React from 'react'
  2. import { Mutation } from 'react-apollo'
  3. import gql from 'graphql-tag'
  4. import jwt from 'jsonwebtoken'
  5. import { withStyles } from 'material-ui'
  6. import Checkbox from 'material-ui/Checkbox'
  7. import FormControlLabel from 'material-ui/Form/FormControlLabel'
  8. import Dialog, { DialogActions, DialogContent, DialogTitle, withMobileDialog } from 'material-ui/Dialog'
  9. import Button from 'material-ui/Button'
  10. import Paper from 'material-ui/Paper'
  11. import TextField from 'material-ui/TextField'
  12. import Grid from 'material-ui/Grid'
  13. import { FormControl } from 'material-ui/Form'
  14. import Select from 'material-ui/Select'
  15. import InputLabel from '@material-ui/core/InputLabel'
  16. import MenuItem from '@material-ui/core/MenuItem'
  17.  
  18. const styles = theme => ({
  19. logoWidth: {
  20. [theme.breakpoints.up('md')]: {
  21. maxWidth: '60%'
  22. },
  23. [theme.breakpoints.down('sm')]: {
  24. maxWidth: '80%'
  25. }
  26. },
  27. paperLogin: {
  28. [theme.breakpoints.up('md')]: {
  29. maxWidth: '30%'
  30. },
  31. [theme.breakpoints.down('sm')]: {
  32. maxWidth: '90%'
  33. }
  34. }
  35. })
  36.  
  37. class Login extends React.Component {
  38. constructor (props) {
  39. super(props)
  40. this.state = {
  41. email: null,
  42. pass: null,
  43. open: false,
  44. error: false
  45. }
  46. }
  47.  
  48. render () {
  49. const {classes} = this.props
  50. return (
  51. <Mutation
  52. mutation={gql`mutation localLogin($token: String!, $expiration: String!) { login(token: $token, expiration: $expiration) @client { token }}`}
  53. ssr={false}
  54. >
  55. {login =>
  56. <div style={{
  57. display: 'flex',
  58. alignItems: 'center',
  59. justifyContent: 'center',
  60. height: '100vh',
  61. backgroundImage: 'url("/images/sfondo4.jpg")',
  62. backgroundSize: 'cover'
  63. }}>
  64. {/* <Paper style={{maxWidth: '60%', textAlign: 'center', flex: 1, position: 'absolute', top: '10%', padding: 30, justifyContent: 'center', alignItems: 'center'}}> */}
  65. <Mutation
  66. mutation={gql`mutation loginRemote($email: String!, $password: String!) { signinUser(email: { email: $email, password: $password }) {
  67. token
  68. }
  69. }`}
  70. ssr={false}
  71. >
  72. {(serverLogin, {data, loading, error}) => {
  73. if (data) {
  74. const expiration = new Date(jwt.decode(data.signinUser.token).exp * 1000).toISOString()
  75. login({variables: {token: data.signinUser.token, expiration: expiration}})
  76. }
  77. return (
  78. <Paper className={classes.paperLogin} style={{textAlign: 'center', flex: 1, padding: 30, justifyContent: 'center', alignItems: 'center'}}>
  79. <Grid container spacing={24}>
  80. <Grid item xs={12}>
  81. <img alt='fruitone logo' src='https://i.imgur.com/sEnQFzJ.png' style={{maxWidth: '60%'}} />
  82. </Grid>
  83. <Grid item xs={12} style={{paddingBottom: 0}}>
  84. <TextField
  85. required
  86. id='mail'
  87. label='Email'
  88. margin='normal'
  89. onChange={(e) => this.setState({email: e.target.value})}
  90. onKeyPress={(ev) => {
  91. console.log(ev.key)
  92. if (ev.key === 'Enter') {
  93. try {
  94. serverLogin({variables: {email: this.state.email, password: this.state.pass}})
  95. } catch (e) {
  96. this.setState({error: true})
  97. }
  98. }
  99. }}
  100. style={{width: '100%'}}
  101. />
  102. </Grid>
  103. <Grid item xs={12} style={{paddingTop: 0}}>
  104. <TextField
  105. required
  106. id='pass'
  107. label='Password'
  108. margin='normal'
  109. onChange={(e) => this.setState({pass: e.target.value})}
  110. onKeyPress={(ev) => {
  111. console.log(ev.key)
  112. if (ev.key === 'Enter') {
  113. try {
  114. serverLogin({variables: {email: this.state.email, password: this.state.pass}})
  115. } catch (e) {
  116. this.setState({error: true})
  117. }
  118. }
  119. }}
  120. style={{width: '100%'}}
  121. type='password'
  122. />
  123. </Grid>
  124. {error
  125. ? <Grid item xs={12}>
  126. <div style={{color: 'red'}}>Utente e password non corrispondono</div>
  127. </Grid>
  128. : null}
  129. <Grid item xs={6} sm={4} md={3} style={{marginTop: 20}}>
  130. <Button onClick={() => {
  131. try {
  132. serverLogin({variables: {email: this.state.email, password: this.state.pass}})
  133. } catch (e) {
  134. this.setState({error: true})
  135. }
  136. }} color='primary' variant='raised'>
  137. Entra
  138. </Button>
  139. </Grid>
  140. <Grid item xs={6} sm={4} md={3} style={{marginTop: 20}}>
  141. <Button onClick={() => this.setState({open: true})} color='secondary' variant='raised'>
  142. Registrati
  143. </Button>
  144. </Grid>
  145. </Grid>
  146. </Paper>
  147. )
  148. }}
  149. </Mutation>
  150. <Registrazione
  151. open={this.state.open}
  152. onClose={() => this.setState({open: false})}
  153. fullScreen={this.props.fullScreen} />
  154. </div>
  155. }
  156. </Mutation>
  157. )
  158. }
  159. }
  160.  
  161. export default withMobileDialog()(withStyles(styles)(Login))
  162.  
  163. class Registrazione extends React.Component {
  164. constructor (props) {
  165. super(props)
  166. this.state = {
  167. ragioneSociale: '',
  168. nome: '',
  169. cognome: '',
  170. email: '',
  171. telefono: '',
  172. sede: '',
  173. indirizzo: '',
  174. codFiscale: '',
  175. citta: '',
  176. cap: '',
  177. pIva: '',
  178. password: '',
  179. cPassword: '',
  180. coordBancarie: '',
  181. sameIndirizzo: true,
  182. open: false,
  183. tipoCliente: ''
  184. }
  185. }
  186. render () {
  187. const props = this.props
  188. const isPwEq = this.state.password === this.state.cPassword
  189. const isCapInt = Number.isInteger(this.state.cap)
  190. const disabled =
  191. this.state.nome === '' ||
  192. this.state.cognome === '' ||
  193. this.state.ragioneSociale === '' ||
  194. this.state.email === '' ||
  195. this.state.telefono === '' ||
  196. this.state.sede === '' ||
  197. this.state.cap === '' ||
  198. this.state.citta === '' ||
  199. this.state.password === '' ||
  200. this.state.cPassword === '' ||
  201. this.state.indirizzo === '' ||
  202. this.state.codFiscale === '' ||
  203. this.state.pIva === '' ||
  204. this.state.coordBancarie === ''
  205.  
  206. return (
  207. <Dialog
  208. fullScreen={props.fullScreen}
  209. open={props.open}
  210. onClose={() => {
  211. props.onClose()
  212. this.setState({udm: {id: null, name: null}, amount: null})
  213. }}
  214. aria-labelledby='responsive-dialog-title'
  215. >
  216. <DialogTitle id='responsive-dialog-title'>
  217. <div style={{color: '#eee'}}>
  218. Registrati
  219. </div>
  220. </DialogTitle>
  221. <div align='center'>
  222. <DialogContent>
  223. <FormControl aria-describedby='unit-helper-text'>
  224. <InputLabel>Tipo di cliente</InputLabel>
  225. <Select
  226. value={this.state.tipoCliente}
  227. onChange={(event) => this.setState({tipoCliente: event.target.value})}
  228. >
  229. <MenuItem value='Privato'>Privato</MenuItem>
  230. <MenuItem value='Business'>Business</MenuItem>
  231. </Select>
  232. </FormControl>
  233. <Grid item xs={12}>
  234. <TextField
  235. id='ragioneSociale'
  236. label='Ragione Sociale'
  237. defaultValue={this.state.ragioneSociale}
  238. onChange={(n) => this.setState({ragioneSociale: n.target.value})}
  239. margin='normal'
  240. />
  241. </Grid>
  242. <Grid item xs={12}>
  243. <TextField
  244. id='email'
  245. label='Email'
  246. defaultValue={this.state.email}
  247. onChange={(n) => this.setState({email: n.target.value})}
  248. margin='normal'
  249. />
  250. </Grid>
  251. <Grid item xs={12}>
  252. <TextField
  253. id='password'
  254. label='Password'
  255. type='password'
  256. defaultValue={this.state.password}
  257. onChange={(n) => this.setState({password: n.target.value})}
  258. margin='normal'
  259. />
  260. </Grid>
  261. <Grid item xs={12}>
  262. <TextField
  263. id='confermaPassword'
  264. label='Conferma Password'
  265. type='password'
  266. defaultValue={this.state.cPassword}
  267. onChange={(n) => this.setState({cPassword: n.target.value})}
  268. margin='normal'
  269. />
  270. </Grid>
  271. {isPwEq
  272. ? null
  273. : <span style={{color: 'red'}}> Le password non corrispondono! </span> }
  274. <Grid item xs={12}>
  275. <TextField
  276. id='telefono'
  277. label='Telefono'
  278. defaultValue={this.state.telefono}
  279. onChange={(n) => this.setState({telefono: n.target.value})}
  280. margin='normal'
  281. />
  282. </Grid>
  283. <Grid item xs={12}>
  284. <TextField
  285. id='indirizzoSede'
  286. label='Indirizzo sede amministr.'
  287. defaultValue={this.state.sede}
  288. onChange={(n) => {
  289. if (this.state.sameIndirizzo) {
  290. this.setState({sede: n.target.value, indirizzo: n.target.value})
  291. } else {
  292. this.setState({sede: n.target.value})
  293. }
  294. }}
  295. margin='normal'
  296. />
  297. </Grid>
  298. <Grid item xs={12}>
  299. <FormControlLabel
  300. style={{marginRight: 0, marginTop: 10}}
  301. control={
  302. <Checkbox
  303. checked={this.state.sameIndirizzo}
  304. onClick={() => this.setState({sameIndirizzo: !this.state.sameIndirizzo})}
  305. color='secondary' />
  306. } label='Indirizzo di consegna coincide con la sede' />
  307. </Grid>
  308. {this.state.sameIndirizzo
  309. ? null
  310. : <Grid item xs={12}>
  311. <TextField
  312. id='indirizzo'
  313. label='Indirizzo di consegna'
  314. defaultValue={this.state.indirizzo}
  315. onChange={(n) => this.setState({indirizzo: n.target.value})}
  316. margin='normal'
  317. />
  318. </Grid>}
  319. <Grid item xs={12}>
  320. <TextField
  321. id='citta'
  322. label='Città'
  323. defaultValue={this.state.citta}
  324. onChange={(n) => this.setState({citta: n.target.value})}
  325. margin='normal'
  326. />
  327. </Grid>
  328. <Grid item xs={12}>
  329. <TextField
  330. id='cap'
  331. label='CAP'
  332. defaultValue={this.state.cap}
  333. onChange={(n) => this.setState({cap: parseInt(n.target.value)})}
  334. margin='normal'
  335. />
  336. </Grid>
  337. {isCapInt
  338. ? null
  339. : this.state.cap !== ''
  340. ? <span style={{color: 'red'}}>Il CAP deve essere un numero</span>
  341. : null
  342. }
  343. <Grid item xs={12}>
  344. <TextField
  345. id='codFiscale'
  346. label='Codice Fiscale'
  347. defaultValue={this.state.codFiscale}
  348. onChange={(n) => this.setState({codFiscale: n.target.value})}
  349. margin='normal'
  350. />
  351. </Grid>
  352. <Grid item xs={12}>
  353. <TextField
  354. id='pIva'
  355. label='Partita Iva'
  356. defaultValue={this.state.pIva}
  357. onChange={(n) => this.setState({pIva: n.target.value})}
  358. margin='normal'
  359. />
  360. </Grid>
  361. <Grid item xs={12}>
  362. <TextField
  363. id='coordBancarie'
  364. label='Coordinate bancarie'
  365. defaultValue={this.state.coordBancarie}
  366. onChange={(n) => this.setState({coordBancarie: n.target.value})}
  367. margin='normal'
  368. />
  369. </Grid>
  370. </DialogContent>
  371. </div>
  372. <DialogActions>
  373. <Button onClick={() => {
  374. props.onClose()
  375. }} color='primary'>
  376. Indietro
  377. </Button>
  378. <Mutation
  379. mutation={gql`mutation createUser(
  380. $nome: String,
  381. $cognome: String,
  382. $ragioneSociale: String,
  383. $indirizzo: String,
  384. $sede: String!,
  385. $pIva: String,
  386. $codFiscale: String,
  387. $cap: Int,
  388. $citta: String,
  389. $telefono: String,
  390. $coordBancarie: String!
  391. $email: String!,
  392. $password: String!,
  393. ) { createUser(
  394. nome: $nome,
  395. cognome: $cognome,
  396. ragioneSociale: $ragioneSociale
  397. indirizzo: $indirizzo
  398. sede: $sede
  399. pIva: $pIva
  400. codFiscale: $codFiscale
  401. cap: $cap
  402. citta: $citta
  403. telefono: $telefono
  404. coordBancarie: $coordBancarie
  405. authProvider: {email:{email:$email, password:$password}}
  406. ) { id } }`}
  407. ssr={false}>
  408. {createUser =>
  409. <Button
  410. onClick={() => {
  411. createUser({variables: {
  412. nome: this.state.nome,
  413. cognome: this.state.cognome,
  414. ragioneSociale: this.state.ragioneSociale,
  415. indirizzo: this.state.indirizzo,
  416. sede: this.state.sede,
  417. pIva: this.state.pIva,
  418. codFiscale: this.state.codFiscale,
  419. cap: parseInt(this.state.cap),
  420. citta: this.state.citta,
  421. telefono: this.state.telefono,
  422. coordBancarie: this.state.coordBancarie,
  423. email: this.state.email,
  424. password: this.state.password
  425. }})
  426. props.onClose()
  427. }}
  428. color='primary'
  429. variant='raised'
  430. style={{marginTop: 15}}
  431. disabled={!isPwEq || disabled || !isCapInt
  432. }
  433. >
  434. Conferma
  435. </Button>
  436. }
  437. </Mutation>
  438. </DialogActions>
  439. </Dialog>
  440. )
  441. }
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement