1+ import React , { useState , useEffect } from 'react'
2+ import classes from './App.module.css'
3+ import Modal from './plugins/modal/Modal'
4+ import PropTypes from 'prop-types' ;
5+ // import * as firebase from 'firebase'
6+ import dbAPI from './firebaseAPI'
7+
8+
9+ console . log ( 'run welcomeWindow' ) ;
10+
11+ const GamesList = ( ) => {
12+ return (
13+ < div > List</ div >
14+ )
15+ }
16+
17+
18+ const WelcomeWindow = ( { currentUserName} ) => {
19+ const [ showModal , setShowModal ] = useState ( false ) ;
20+ const [ userName , setUserName ] = useState ( currentUserName ) ;
21+ const [ inputName , setInputName ] = useState ( '' ) ; // для сохранения строки ввода имени в переменную
22+
23+ useEffect ( ( ) => {
24+ console . log ( 'effect userName' , userName ) ;
25+ if ( ! userName ) return ;
26+ localStorage . setItem ( 'userName' , userName )
27+ dbAPI . userInit ( userName )
28+ } , [ userName ] )
29+
30+ useEffect ( ( ) => {
31+ const name = localStorage . getItem ( 'userName' || [ ] )
32+ console . log ( 'reborn userName' , name ) ;
33+ // for clear localStorage activate below strings and run App TWICE:
34+ // localStorage.setItem('userName', '')
35+ setUserName ( name )
36+ } , [ ] )
37+ const buttonClasses = [ ]
38+ // add new class
39+ buttonClasses . push ( 'btn' ) ;
40+ buttonClasses . push ( 'btn-primary' ) ;
41+
42+
43+ const handleNewGame = ( ) => {
44+ if ( ! userName ) setShowModal ( true )
45+ }
46+
47+ const handleSubmit = ( ) => {
48+ console . log ( 'Submit function!' , userName ) ;
49+ setUserName ( inputName )
50+ setShowModal ( false )
51+ }
52+
53+ const handleCancel = ( ) => {
54+ console . log ( 'Cancel function!' ) ;
55+ setShowModal ( false )
56+ }
57+
58+ const handleChange = ( { target : { value } } ) => {
59+ setInputName ( value )
60+ }
61+
62+ return (
63+ < div className = { classes [ 'App-body' ] } style = { { textAlign : 'center' } } >
64+
65+ < div > { userName + ( userName ? '! ' : '' ) + `Приветствую Вас на игре 'Квадраты'` } </ div >
66+ < GamesList games = { '! put list form FB here' } />
67+ < button
68+ className = { buttonClasses . join ( ' ' ) }
69+ onClick = { handleNewGame } >
70+ New Game
71+ </ button >
72+
73+ < button
74+ className = { buttonClasses . join ( ' ' ) }
75+ onClick = { handleNewGame } >
76+ Join Game
77+ </ button >
78+ < Modal
79+ title = "Test Dialog window"
80+ isOpen = { showModal }
81+ onCancel = { handleCancel }
82+ onSubmit = { handleSubmit }
83+ >
84+ < div className = "form-inline" style = { { justifyContent : 'center' } } >
85+ < input type = "text" className = "form-control" placeholder = "Enter your name" onChange = { handleChange } />
86+ </ div >
87+ </ Modal >
88+ </ div >
89+ )
90+ }
91+
92+ WelcomeWindow . propTypes = {
93+ currentUserName : PropTypes . string ,
94+ }
95+
96+ WelcomeWindow . defaultProps = {
97+ currentUserName : '' ,
98+ }
99+
100+
101+ export default WelcomeWindow
0 commit comments