Skip to content

Commit

Permalink
little fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
scffs committed Sep 5, 2023
1 parent c516b23 commit 25a74c0
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 66 deletions.
2 changes: 1 addition & 1 deletion backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const port = process.env.PORT ?? 3000
app.use(preventCrossSiteScripting)
app.use(helmet())
app.use(cors({ origin: '*' }))
app.use(express.json());
app.use(express.json())

const limiter = rateLimit({
windowMs: 60 * 1000,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/routes/login.route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import express, { type Request, type Response } from 'express'
import { AxiosError, type AxiosResponse } from 'axios'
import axiosInstance from '../axiosWrapper'
import { AuthData } from '../../../shared'
import { type AuthData } from '../../../shared'

const router = express.Router()

router.post('/', async (req: Request, res: Response) => {
const { login, password } = req.body;
const { login, password } = req.body
console.log(login)
if (!login || !password || typeof login !== 'string' || typeof password !== 'string') {
return res.status(400).json('Invalid login or password')
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getCookie } from './methods';
import { Pages } from './types';

import Suspense from './components/Suspense';
import ModalRoot from './components/ModalRoot';
import ModalRoot from './modals/ModalRoot';

const Sidebar = lazy(() => import('./components/Sidebar'));
const Epic = lazy(() => import('./components/Epic'));
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/AppWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import NotFound from './components/NotFound';
import Suspense from './components/Suspense';

import { router } from './routes';
import { ModalProvider } from './modals/ModalContext.tsx';

const App = lazy(() => import('./App'));

Expand All @@ -26,7 +27,9 @@ const AppWrapper = () => {
<RouterProvider router={router} notFound={NotFoundCorrect}>
<Suspense id='app' mode='screen'>
<ConfigProvider platform={platform}>
<App />
<ModalProvider>
<App />
</ModalProvider>
</ConfigProvider>
</Suspense>
</RouterProvider>
Expand Down
29 changes: 17 additions & 12 deletions frontend/src/components/LessonCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FC } from 'react';
import {
Card, Group, Header, Placeholder, SimpleCell,
} from '@vkontakte/vkui';
import { useRouteNavigator, useSearchParams } from '@vkontakte/vk-mini-apps-router';
import { useRouteNavigator } from '@vkontakte/vk-mini-apps-router';

import {
Day, Grade, Gradebook, LessonWorkType, Timetable,
Expand All @@ -11,34 +11,39 @@ import setDefaultMark from '../utils/setDefaultMark';
import { formatLessonDate } from '../utils/formatLessonDate';
import truncateText from '../utils/truncateText';

import { MODAL_PAGE_LESSON } from './ModalRoot';
import { MODAL_PAGE_LESSON } from '../modals/ModalRoot';

import SubtitleWithBorder from './SubtitleWithBorder';
import TimeRemaining from './TimeRemaining';
import Mark from './Mark';
import { useModal } from '../modals/ModalContext';

interface ILessonCard {
lesson: Day;
}

const LessonCard: FC<ILessonCard> = ({ lesson }) => {
const routeNavigator = useRouteNavigator();
const [params, setParams] = useSearchParams();

const { openModal } = useModal();

const handleLessonClick = (name: string, endTime: string, startTime: string, timetable: Timetable, gradebook: Gradebook | undefined) => {
routeNavigator.showModal(MODAL_PAGE_LESSON);

const lessonDate = new Date(lesson.date);
const lessonId = lessonDate.toISOString();
params.set('name', name);
params.set('endTime', endTime);
params.set('startTime', startTime);
params.set('timetable', JSON.stringify(timetable));
params.set('gradebook', JSON.stringify(gradebook));
params.set('tasks', JSON.stringify(gradebook?.tasks));
params.set('lessonId', lessonId);

setParams(params);
const modalData = {
name,
endTime,
startTime,
timetable,
gradebook,
tasks: gradebook?.tasks,
lessonId,
};

routeNavigator.showModal(MODAL_PAGE_LESSON);
openModal(modalData);
};

const currentDate = new Date();
Expand Down
39 changes: 39 additions & 0 deletions frontend/src/modals/ModalContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, {
createContext, useContext, useState, ReactNode,
} from 'react';
import { Gradebook, Task, Timetable } from '../../../shared';

interface ModalData {
name: string;
endTime: string;
startTime: string;
timetable: Timetable;
gradebook?: Gradebook | undefined;
tasks?: Task[] | undefined;
lessonId: string;
}

const ModalContext = createContext<{ modalData: ModalData | null; openModal:(data: { name: string; lessonId: string; startTime: string; endTime: string; gradebook: Gradebook | undefined; tasks: Task[] | undefined; timetable: Timetable }) => void } | undefined>(
undefined);

export const ModalProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const [modalData, setModalData] = useState<ModalData | null>(null);

const openModal = (data: ModalData) => {
setModalData(data);
};

return (
<ModalContext.Provider value={{ modalData, openModal }}>
{children}
</ModalContext.Provider>
);
};

export const useModal = () => {
const context = useContext(ModalContext);
if (context === undefined) {
throw new Error('useModal must be used within a ModalProvider');
}
return context;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
Div,
Group, Header, ModalPage, ModalPageHeader, ModalRoot as VKUIModalRoot,
} from '@vkontakte/vkui';
import { useActiveVkuiLocation, useRouteNavigator, useSearchParams } from '@vkontakte/vk-mini-apps-router';
import { useActiveVkuiLocation, useRouteNavigator } from '@vkontakte/vk-mini-apps-router';

import {
Grade, Lesson, LessonType, LessonWorkType, Task, TLesson,
Grade, Lesson, LessonType, LessonWorkType, TLesson,
} from '../../../shared';
import setDefaultMark from '../utils/setDefaultMark';

import Mark from './Mark';
import Mark from "../components/Mark.tsx";
import { useModal } from "./ModalContext.tsx";

export const MODAL_PAGE_LESSON = 'lesson';

Expand Down Expand Up @@ -40,80 +40,62 @@ const cleanData: Lesson = {
};

const ModalRoot = () => {
const { modalData } = useModal();
console.log(modalData);

const routeNavigator = useRouteNavigator();
const { modal: activeModal } = useActiveVkuiLocation();
const [params] = useSearchParams();
const [lessonData, setLessonData] = useState<Lesson>(cleanData);
let tasksArray: Task[] = [];

const lessonId = params.get('lessonId');

const [lessonData, setLessonData] = useState<Lesson>(cleanData);

useEffect(() => {
if (lessonId) {
const timetableParam = params.get('timetable');
const gradebookParam = params.get('gradebook');
const tasksParam = params.get('tasks');

if (tasksParam) {
try {
tasksArray = JSON.parse(tasksParam);
} catch (error) {
console.error('Ошибка при парсинге строки tasksParam:', error);
}
}

let parsedTimetable;
let parsedGradebook;

try {
parsedTimetable = timetableParam ? JSON.parse(timetableParam) : undefined;
parsedGradebook = gradebookParam !== undefined && gradebookParam ? JSON.parse(gradebookParam) : undefined;
} catch (err) {
console.log(err);
}

let lessonName = params.get('name') || '';

if (modalData) {
const {
name, endTime, startTime, timetable, gradebook, tasks: tasksArray,
} = modalData;

let lessonName = name || '';

if (lessonName.includes('/')) {
const parts = lessonName.split('/');

if (parts.length >= 2) {
lessonName = parts[0];
const additionalInfo = parts.slice(1).join('/');

if (additionalInfo.trim()) {
lessonName += ` (${additionalInfo})`;
}
}
}

setLessonData({
name: lessonName,
gradebook: {
id: parsedGradebook?.id || 0,
lessonType: parsedGradebook?.lessonType || '',
id: gradebook?.id || 0,
lessonType: gradebook?.lessonType || '',
tasks: tasksArray,
themes: parsedGradebook?.themes,
themes: gradebook?.themes,
},
timetable: {
classroom: {
id: 0,
building: '',
name: parsedTimetable?.classroom?.name || '',
name: timetable?.classroom?.name || '',
},
teacher: {
id: parsedTimetable.teacher?.id || 0,
lastName: parsedTimetable?.teacher?.lastName || '',
firstName: parsedTimetable?.teacher?.firstName || '',
middleName: parsedTimetable?.teacher?.middleName || '',
id: timetable.teacher?.id || 0,
lastName: timetable?.teacher?.lastName || '',
firstName: timetable?.teacher?.firstName || '',
middleName: timetable?.teacher?.middleName || '',
},
},
startTime: params.get('startTime') || 'Что-то не так с датой',
endTime: params.get('endTime') || 'Что-то не так с датой',
startTime: startTime || 'Что-то не так с датой',
endTime: endTime || 'Что-то не так с датой',
});
}
}, [params, lessonId]);

}, [modalData]);
return (
<VKUIModalRoot activeModal={activeModal} onClose={() => routeNavigator.hideModal()}>
<ModalPage id={MODAL_PAGE_LESSON} size={500} dynamicContentHeight>
Expand Down

0 comments on commit 25a74c0

Please sign in to comment.