@@ -41,7 +41,7 @@ class App extends Component<AppProps, AppState> {
4141        this . state  =  { 
4242            loggedIn : ApiSingleton . authService . isLoggedIn ( ) , 
4343            isLecturer : ApiSingleton . authService . isLecturer ( ) , 
44-             newNotificationsCount : 0 
44+             newNotificationsCount : 0 , 
4545        } ; 
4646    } 
4747
@@ -71,6 +71,52 @@ class App extends Component<AppProps, AppState> {
7171        this . props . history . push ( "/login" ) ; 
7272    } 
7373
74+     updatedLastViewedCourseId  =  ( courseId  : string )  => 
75+     { 
76+         sessionStorage . setItem ( "courseId" ,  courseId ) 
77+     } 
78+ 
79+     getLastCourseId  =  ( )  => 
80+     { 
81+         const  sessionStorageCourseId  =  sessionStorage . getItem ( "courseId" ) ; 
82+         return  sessionStorageCourseId  ===  null  ? "-1"  : sessionStorageCourseId ; 
83+     } 
84+ 
85+     getUserYandexToken  =  ( )  => 
86+     { 
87+         const  linkWithConfirmationCode  =  window . location . href ; 
88+         const  regExp  : RegExp  =  new  RegExp ( "yandex\\?code=(.*)" ,  "g" ) ; 
89+         const  confirmationCode  =  regExp . exec ( linkWithConfirmationCode ) ! [ 1 ] ; 
90+         const  fetchBody  =  `grant_type=authorization_code&code=${ confirmationCode }  ; 
91+         interface  ExchangeConfirmationCodeRequest  { 
92+             access_token : string 
93+         } 
94+         fetch ( `https://oauth.yandex.ru/token` ,  { 
95+             method : "post" , 
96+             headers : { 
97+                 'Content-Type' : 'application/x-www-form-urlencoded; Charset=utf-8' , 
98+                 'Host' : 'https://oauth.yandex.ru/' 
99+             } , 
100+             body : fetchBody 
101+         } ) 
102+             . then (  async  ( response )  =>  { 
103+                 if  ( response . status  ===  200 )  { 
104+                     const  jsonResponse  =  await  response . json ( ) ; 
105+                     const  token  =  jsonResponse . access_token ; 
106+                     const  userId  =  await  ApiSingleton . accountApi . apiAccountGetUserDataGet ( ) 
107+                         . then ( ( data )  =>  { 
108+                             const  userData  =  data . userData ; 
109+                             if  ( userData  !==  undefined )  { 
110+                                 return  userData . userId 
111+                             } 
112+                         } ) 
113+                     if  ( token  !==  null  &&  userId  !==  undefined ) 
114+                     { 
115+                         localStorage . setItem ( `yandexAccessToken=${ userId }  ,  token ) ; 
116+                     } 
117+                 } 
118+             } ) ; 
119+     } 
74120
75121    render ( )  { 
76122        return  ( 
@@ -88,7 +134,15 @@ class App extends Component<AppProps, AppState> {
88134                    < Route  exact  path = "/courses"  component = { Courses } /> 
89135                    < Route  exact  path = "/profile/:id"  component = { Workspace } /> 
90136                    < Route  exact  path = "/create_course"  component = { CreateCourse } /> 
91-                     < Route  exact  path = "/courses/:id"  component = { Course } /> 
137+                     < Route  exact  path = "/courses/:id" 
138+                            render = { ( props )  => 
139+                                < Course 
140+                                    { ...props } 
141+                                    id = { props . match . params . id } 
142+                                    onSet = { this . updatedLastViewedCourseId } 
143+                                    isFromRedirect = { false } 
144+                                    redirectHandler = { this . getUserYandexToken } 
145+                                /> } /> 
92146                    < Route  exact  path = "/courses/:courseId/edit"  component = { EditCourse } /> 
93147                    < Route  exact  path = "/homework/:homeworkId/edit"  component = { EditHomework } /> 
94148                    < Route  exact  path = "/task/:taskId/edit"  component = { EditTask } /> 
@@ -104,6 +158,17 @@ class App extends Component<AppProps, AppState> {
104158                        path = "/register" 
105159                        render = { ( props )  =>  < Register  { ...props }  onLogin = { this . login } /> } 
106160                    /> 
161+                     < Route 
162+                         exact 
163+                         path = "/yandex" 
164+                         render = { ( props )  =>  < Course 
165+                             { ...props } 
166+                             id = { this . getLastCourseId ( ) } 
167+                             onSet = { this . updatedLastViewedCourseId } 
168+                             isFromRedirect = { true } 
169+                             redirectHandler = { this . getUserYandexToken } 
170+                         /> } 
171+                     /> 
107172                    < Route  exact  path = { "*" }  component = { WrongPath } /> 
108173                </ Switch > 
109174            </ > 
0 commit comments