@@ -37,7 +37,6 @@ function checkCardsArr(cardsArr) {
3737 if ( ! Array . isArray ( cardsArr ) ) return false
3838 else if ( cardsArr . length === 0 ) return true
3939 else {
40- //console.log('check array')
4140 cardsArr . forEach ( ( card ) => {
4241 if ( typeof card !== "object" ) return false
4342 else if ( ! checkCardFields ( card ) ) return false
@@ -52,41 +51,51 @@ function checkCardFields(card) {
5251 typeof card . completed === "boolean" &&
5352 typeof card . text === "string"
5453 )
55- //console.log('check fields',!isNaN(card.id),typeof card.completed === "boolean", typeof card.text === "string",res)
5654 return res
5755}
5856
5957function App ( ) {
6058 const [ cardsArr , setCards ] = useCardsArr ( [ ] )
6159 const [ editCardId , setEditCardId ] = React . useState ( null )
6260 const [ loading , setLoading ] = React . useState ( true )
61+ const [ logged , setLogged ] = React . useState ( false )
6362 React . useEffect ( tryLogin , [ ] ) // eslint-disable-line react-hooks/exhaustive-deps
64- React . useEffect ( loadDataFromServer , [ ] ) // eslint-disable-line react-hooks/exhaustive-deps
63+ React . useEffect ( loadDataFromServer , [ logged ] ) // eslint-disable-line react-hooks/exhaustive-deps
6564 React . useEffect ( loadDataToServer , [ cardsArr ] ) // eslint-disable-line react-hooks/exhaustive-deps
6665
66+
6767 ///////////
6868 function tryLogin ( ) {
6969 try {
70- login ( ) . then ( loadDataFromServer , alert )
70+ login ( ) . then ( setLogged , alert )
7171 } catch ( e ) {
7272 console . error ( e )
7373 }
7474 }
75+ ///////////
7576
77+ ///////////
7678 function loadDataToServer ( ) {
77- //console.log("***\n[HOOK] - loadDataToServer (onCardsArr)\n***")
7879 try {
79- postData ( cardsArr ) . then ( console . log , dataError )
80+ if ( logged ) postData ( cardsArr ) . then ( ( res ) => {
81+ console . log ( '[onPostData]' , res )
82+ } , dataError )
8083 }
8184 catch ( e ) {
8285 console . error ( e )
8386 }
8487 }
8588
8689 function loadDataFromServer ( ) {
87- //console.log("[HOOK] - loadDataFromServer")
8890 try {
89- loadData ( ) . then ( setLoadedCards , dataError )
91+ if ( logged ) {
92+ setLoading ( true )
93+ loadData ( ) . then ( data => {
94+ console . log ( '[onLoadData]' , 'Данные с сервера загружены' )
95+ setLoadedCards ( data )
96+ setLoading ( false )
97+ } , dataError )
98+ }
9099 }
91100 catch ( e ) {
92101 console . error ( e )
@@ -96,15 +105,14 @@ function App() {
96105 function dataError ( msg ) {
97106 console . log ( `Data request error. Response: ${ msg } ` )
98107 }
108+ ///////////
99109
110+ ///////////
100111 function setLoadedCards ( cards ) {
101112 setCards ( [ ...cards ] )
102113 cardCount = calcCount ( cards )
103- setLoading ( false )
104114 }
105- ///////////
106115
107- ///////////
108116 function removeCard ( index ) {
109117 cardsArr . splice ( index , 1 )
110118 setCards ( [ ...cardsArr ] )
@@ -124,7 +132,6 @@ function App() {
124132 }
125133 ] )
126134 )
127-
128135 }
129136
130137 function changeCardState ( index ) {
@@ -152,16 +159,17 @@ function App() {
152159
153160
154161 return (
155- < Context . Provider value = { { removeCard, changeCardState, setEditCard, unsetEditCard, editCardContent } } >
162+ < Context . Provider value = { { removeCard, changeCardState, setEditCard, unsetEditCard, editCardContent, editCardId } } >
156163 < div className = "App" >
157164 < header className = "p-1 h2 text-center" >
158165 < img src = { logo } className = "App-logo" alt = "logo" />
159166 < h1 className = "d-inline-block h2" > { testText } </ h1 >
160- < p className = "show_login" > </ p >
167+ < p className = "show_login" onClick = { ( e ) => { e . target . style . opacity = e . target . style . opacity !== '0' ? '0' : '1' } } >
168+ { logged && `Login: ${ logged } ` }
169+ </ p >
161170 </ header >
162171
163172 < main className = "p-1" >
164-
165173 < AddCard onCreate = { addCard } onDeleteAll = { deleteAll } />
166174 < Modal component = { ModalCardEdit } componentProps = { { card : getCardByIndex ( editCardId ) , index : editCardId } } />
167175 { loading && < Loader /> }
@@ -172,7 +180,6 @@ function App() {
172180 < p className = "m-3 p-3 h5 text-muted" > No cards. You can add a new one!</ p >
173181 </ div >
174182 ) }
175-
176183 </ main >
177184 </ div >
178185 </ Context . Provider >
0 commit comments