Skip to content

Commit 75dc8c0

Browse files
committed
refactor frontend part
1 parent fd2d37b commit 75dc8c0

File tree

10 files changed

+244
-294
lines changed

10 files changed

+244
-294
lines changed

HwProj.APIGateway/HwProj.APIGateway.API/TableGenerators/ExcelGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace HwProj.APIGateway.API.TableGenerators
1111
/// <summary>
1212
/// Implements course report generation.
1313
/// </summary>
14-
public class ExcelGenerator
14+
public static class ExcelGenerator
1515
{
1616
/// <summary>
1717
/// Font used in the reports.

hwproj.front/src/App.tsx

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -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,53 +71,6 @@ 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}&client_id=49a5c4e9d2744ff5b0161a017d1ecd05&client_secret=184c6646ff62417fa2e942a18023881a`;
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-
}
120-
12174
render() {
12275
return (
12376
<>
@@ -134,15 +87,7 @@ class App extends Component<AppProps, AppState> {
13487
<Route exact path="/courses" component={Courses}/>
13588
<Route exact path="/profile/:id" component={Workspace}/>
13689
<Route exact path="/create_course" component={CreateCourse}/>
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-
/>}/>
90+
<Route exact path="/courses/:id" component={Course}/>
14691
<Route exact path="/courses/:courseId/edit" component={EditCourse}/>
14792
<Route exact path="/homework/:homeworkId/edit" component={EditHomework}/>
14893
<Route exact path="/task/:taskId/edit" component={EditTask}/>
@@ -158,17 +103,7 @@ class App extends Component<AppProps, AppState> {
158103
path="/register"
159104
render={(props) => <Register {...props} onLogin={this.login}/>}
160105
/>
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-
/>
106+
<Route exact path="/yandex" component={Course}/>
172107
<Route exact path={"*"} component={WrongPath}/>
173108
</Switch>
174109
</>

hwproj.front/src/components/Courses/Course.tsx

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ interface ICourseState {
3232

3333
interface ICourseProps {
3434
id: string;
35-
onSet: (courseId: string) => void;
36-
isFromRedirect: boolean;
37-
redirectHandler: () => void;
3835
}
3936

4037
const styles = makeStyles(() => ({
@@ -44,8 +41,21 @@ const styles = makeStyles(() => ({
4441
},
4542
}))
4643

47-
const Course: React.FC<ICourseProps> = (props) => {
48-
const courseId = props.id
44+
const Course: React.FC<RouteComponentProps<ICourseProps>> = (props) => {
45+
const getLastViewedCourseId = () =>
46+
{
47+
const sessionStorageCourseId = sessionStorage.getItem("courseId")
48+
return sessionStorageCourseId === null ? "-1" : sessionStorageCourseId
49+
}
50+
51+
const updatedLastViewedCourseId = (courseId : string) =>
52+
{
53+
sessionStorage.setItem("courseId", courseId)
54+
}
55+
56+
const courseIdFromProps = props.match.params.id
57+
const isFromYandex = courseIdFromProps === undefined
58+
const courseId = isFromYandex ? getLastViewedCourseId() : courseIdFromProps
4959
const classes = styles()
5060

5161
const [courseState, setCourseState] = useState<ICourseState>({
@@ -62,6 +72,7 @@ const Course: React.FC<ICourseProps> = (props) => {
6272
studentSolutions: []
6373
})
6474
const setCurrentState = async () => {
75+
updatedLastViewedCourseId(courseId)
6576
const course = await ApiSingleton.coursesApi.apiCoursesByCourseIdGet(+courseId)
6677
const solutions = await ApiSingleton.statisticsApi.apiStatisticsByCourseIdGet(+courseId)
6778

@@ -74,19 +85,27 @@ const Course: React.FC<ICourseProps> = (props) => {
7485
mentors: course.mentors!,
7586
acceptedStudents: course.acceptedStudents!,
7687
newStudents: course.newStudents!,
77-
studentSolutions: solutions
88+
studentSolutions: solutions,
89+
tabValue: isFromYandex ? 1 : 0
7890
}))
91+
if (isFromYandex)
92+
{
93+
window.history.replaceState(null, "", `/courses/${courseId}`)
94+
}
7995
}
8096

8197
useEffect(() => {
82-
setCurrentState();
83-
props.onSet(courseId);
84-
if (props.isFromRedirect)
85-
{
86-
props.redirectHandler();
87-
}
98+
setCurrentState()
8899
}, [])
89100

101+
const getUserYandexCode = (url: string) =>
102+
{
103+
const queryParameters = new URLSearchParams(window.location.search)
104+
return queryParameters.get("code")
105+
}
106+
107+
const idd = getUserYandexCode(props.location.search)
108+
90109
const joinCourse = async () => {
91110
await ApiSingleton.coursesApi
92111
.apiCoursesSignInCourseByCourseIdPost(+courseId)
@@ -308,6 +327,7 @@ const Course: React.FC<ICourseProps> = (props) => {
308327
isMentor={isMentor}
309328
course={courseState.course}
310329
solutions={studentSolutions}
330+
yandexCode={idd}
311331
/>
312332
</Grid>
313333
</Grid>}

hwproj.front/src/components/Courses/StudentStats.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface IStudentStatsProps {
1010
homeworks: HomeworkViewModel[];
1111
isMentor: boolean;
1212
userId: string;
13+
yandexCode: string | null;
1314
solutions: StatisticsCourseMatesModel[];
1415
}
1516

@@ -100,6 +101,7 @@ class StudentStats extends React.Component<IStudentStatsProps, IStudentStatsStat
100101
<SaveStats
101102
courseId={this.props.course.id}
102103
userId={this.props.userId}
104+
yandexCode={this.props.yandexCode}
103105
/>
104106
</div>
105107
</div>

hwproj.front/src/components/Solutions/DownloadStats.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const DownloadStats: FC<DownloadStatsProps> = (props: DownloadStatsProps) => {
3535

3636
return <Grid container spacing={1} style={{ marginTop: 15 }}>
3737
<Grid container item spacing={1} alignItems={"center"}>
38-
<Grid item xs={5}>
38+
<Grid item>
3939
<TextField size={"small"} fullWidth label={"Название файла"} value={fileName}
4040
onChange={event => {
4141
event.persist();

hwproj.front/src/components/Solutions/ExportStats.tsx

Lines changed: 0 additions & 99 deletions
This file was deleted.

hwproj.front/src/components/Solutions/ExportToGoogle.tsx

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,23 @@ import apiSingleton from "../../api/ApiSingleton";
88
interface ExportToGoogleProps {
99
courseId: number | undefined
1010
userId: string
11-
url: string
12-
onUrlChange: (url: string) => void
1311
onCancellation: () => void
1412
}
1513

1614
interface ExportToGoogleState {
15+
url: string,
1716
googleSheetTitles: ResultString | undefined,
1817
selectedSheet: number
1918
}
2019

2120
const ExportToGoogle: FC<ExportToGoogleProps> = (props: ExportToGoogleProps) => {
2221
const [state, setState] = useState<ExportToGoogleState>({
22+
url: '',
2323
selectedSheet: 0,
2424
googleSheetTitles: undefined,
2525
})
2626

27-
const {googleSheetTitles, selectedSheet } = state
28-
29-
useEffect(() => {
30-
handleGoogleDocUrlChange(props.url);
31-
}, []);
27+
const {url, googleSheetTitles, selectedSheet } = state
3228

3329
const handleGoogleDocUrlChange = async (value: string) => {
3430
const titles = await apiSingleton.statisticsApi.apiStatisticsGetSheetTitlesGet(value)
@@ -49,18 +45,16 @@ const ExportToGoogle: FC<ExportToGoogleProps> = (props: ExportToGoogleProps) =>
4945
</Alert>)
5046
||
5147
<Alert severity="info" variant={"standard"}>
52-
Для загрузки таблицы необходимо разрешить доступ на редактирование по ссылке для Google Docs
53-
страницы
48+
Для загрузки таблицы необходимо разрешить доступ на редактирование по ссылке для Google Sheets
5449
</Alert>}
5550
</Grid>
5651
<Grid container item spacing={1} alignItems={"center"}>
5752
<Grid item xs={5}>
58-
<TextField size={"small"} fullWidth label={"Ссылка на Google Docs"} value={props.url}
59-
onChange={event => {
60-
const newUrl = event.target.value
61-
props.onUrlChange(newUrl)
62-
handleGoogleDocUrlChange(newUrl)
63-
}} />
53+
<TextField size={"small"} fullWidth label={"Ссылка на Google Docs"} value={url}
54+
onChange={event =>
55+
handleGoogleDocUrlChange(event.target.value)
56+
}
57+
/>
6458
</Grid>
6559
{googleSheetTitles && googleSheetTitles.value && googleSheetTitles.value.length > 0 && <Grid item>
6660
<Select
@@ -80,7 +74,7 @@ const ExportToGoogle: FC<ExportToGoogleProps> = (props: ExportToGoogleProps) =>
8074
apiSingleton.statisticsApi.apiStatisticsExportToSheetGet(
8175
props.courseId,
8276
props.userId,
83-
props.url,
77+
url,
8478
getGoogleSheetName(),
8579
)}>
8680
Сохранить

0 commit comments

Comments
 (0)