Skip to content

Commit

Permalink
Маленьний рефактор и правка к определению текущего учебного года
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatFix committed Feb 22, 2022
1 parent d0a9ad1 commit c47cbe1
Show file tree
Hide file tree
Showing 15 changed files with 36,233 additions and 8,680 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ npm-debug.log*
eslint-disable-next-line

# Dependency directories
package-lock.json
/node_modules
bundle.zip

Expand Down
44,629 changes: 36,048 additions & 8,581 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"eslint-plugin-prettier": "^3.3.0",
"prettier": "^2.2.1",
"react-hot-loader": "^4.9.0",
"react-scripts": "^3.4.1"
"react-scripts": "^4.0.3"
},
"dependencies": {
"@vkontakte/icons": "^1.89.0",
Expand Down
56 changes: 56 additions & 0 deletions src/api/fetchers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { DonstuAPI } from './instances'

export const getListYears = () =>
DonstuAPI({
url: `/Rasp/ListYears`,
})

export const getTeacherList = (academicYear) => {
if (!academicYear) {
throw new RangeError('invalid academicYear')
}

return DonstuAPI({
url: `/raspTeacherlist?year=${academicYear}`,
})
}

export const getGroupList = (academicYear) => {
if (!academicYear) {
throw new RangeError('invalid academicYear')
}

return DonstuAPI({
url: `/raspGrouplist?year=${academicYear}`,
})
}

export const getTeacherById = (teacherId, date) => {
if (!teacherId) {
throw new RangeError('invalid teacherId')
}

return DonstuAPI({
url: `/Rasp?idTeacher=${teacherId}${date ? `&sdate=${date}` : ''}`,
})
}

export const getGroupById = (groupId, date) => {
if (!groupId) {
throw new RangeError('invalid groupId')
}

return DonstuAPI({
url: `/Rasp?idGroup=${groupId}${date ? `&sdate=${date}` : ''}`,
})
}

// export const fetchListYears = DonstuAPI({
// url: `/Rasp/ListYears`,
// })export const fetchListYears = DonstuAPI({
// url: `/Rasp/ListYears`,
// })export const fetchListYears = DonstuAPI({
// url: `/Rasp/ListYears`,
// })export const fetchListYears = DonstuAPI({
// url: `/Rasp/ListYears`,
// })
2 changes: 2 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './instances'
export * from './fetchers'
7 changes: 7 additions & 0 deletions src/api/instances.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import axios from 'axios'

export const DonstuAPI = axios.create({
crossDomain: true,
timeout: 15000,
baseURL: 'https://edu.donstu.ru/api',
})
1 change: 1 addition & 0 deletions src/store/actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const FETCHING_DISCIPLINES = 'FETCHING_DISCIPLINES'
export const SET_DATE_TOGGLE_WEEK = 'SET_DATE_TOGGLE_WEEK'
export const SET_DATE = 'SET_DATE'
export const SET_CURRENT_DATE = 'SET_CURRENT_DATE'
export const SET_ACADEMIC_YEAR = 'SET_ACADEMIC_YEAR'
export const TOGGLE_OFF = 'TOGGLE_OFF'

export const SET_ALL = 'SET_ALL'
Expand Down
40 changes: 39 additions & 1 deletion src/store/actions/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import {
SET_DATE,
TOGGLE_OFF,
SET_CURRENT_DATE,
SET_ACADEMIC_YEAR,
} from '../actions/actionTypes'
import { fetchSchedule } from './fetchSchedule'
import { DateTime } from 'luxon'
import { getListYears } from '../../api'

const START_MONTH_ACADEMIC_YEAR = [1, 2, 3, 4, 5, 6, 7]

export function setDateToggleWeek(date, toggleWeek) {
return {
Expand All @@ -29,12 +33,47 @@ export function setCurrentDate() {
dayWeekNum: formatDayWeek(DateTime.local().toISODate()),
}
}
export function setAcademicYear(academicYear) {
return {
type: SET_ACADEMIC_YEAR,
academicYear,
}
}
export function toggleOff() {
return {
type: TOGGLE_OFF,
}
}

export function fetchAcademicYear() {
return async (dispatch) => {
let year = ''
try {
const res = await getListYears()

const { data } = res.data
const { years } = data

if (years?.length > 0) {
year = years[years.length - 1]
} else {
throw new Error('academic year undefined')
}
} catch (err) {
const currentYear = new Date().getFullYear()
const currentMonth = DateTime.local().month
const currentAcademicYearStart = START_MONTH_ACADEMIC_YEAR.includes(currentMonth)
? currentYear - 1
: currentYear

year = `${currentAcademicYearStart}-${currentAcademicYearStart + 1}`
} finally {
await dispatch(setAcademicYear(year))
return year
}
}
}

export function nextWeek() {
return (dispatch, getStore) => {
let date = DateTime.fromSQL(getStore().date.date)
Expand Down Expand Up @@ -72,7 +111,6 @@ export function nextWeek() {
export function prevWeek() {
return (dispatch, getStore) => {
let date = DateTime.fromSQL(getStore().date.date)
console.log(date.weekday)
switch (date.weekday) {
case 0:
date = date.minus({ days: 8 })
Expand Down
55 changes: 25 additions & 30 deletions src/store/actions/fetchDisciplines.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ERROR_DISCIPLINES, SUCCESS_DISCIPLINES, FETCHING_DISCIPLINES } from './actionTypes'
import axios from 'axios'
import { getTeacherById, getGroupById } from '../../api'

function error(error) {
return {
Expand All @@ -20,40 +20,35 @@ function fetching() {
}

export function fetchDisciplines() {
return (dispatch, getStore) => {
dispatch(fetching())
const store = getStore()
let url = ''
const post = store.userData.post
if (post === 'Студент') {
const groupId = store.userData.groupId
url = `https://edu.donstu.ru/api/Rasp?idGroup=${groupId}`
} else if (post === 'Преподаватель') {
const teacherId = store.userData.teacherId
url = `https://edu.donstu.ru/api/Rasp?idTeacher=${teacherId}`
} else {
dispatch(error('Error: Ошибка при определении должности.'))
}
return async (dispatch, getStore) => {
try {
await dispatch(fetching())
const { userData } = getStore()
let res = null
const post = userData.post

axios({
url,
crossDomain: true,
timeout: 15000,
}).then(
(res) => {
if (res.data.data.info.group.name) {
const disciplines = getDisciplines(res.data.data)
dispatch(success(disciplines))
}
},
(err) => {
dispatch(error(err))
if (post === 'Студент') {
const groupId = userData.groupId
res = await getGroupById(groupId)
} else if (post === 'Преподаватель') {
const teacherId = userData.teacherId
res = await getTeacherById(teacherId)
} else {
await dispatch(error('Error: Ошибка при определении должности.'))
}
)

if (res?.data.data?.info?.group?.name) {
const disciplines = pullDisciplines(res.data.data)

await dispatch(success(disciplines))
}
} catch (err) {
await dispatch(error(err))
}
}
}

function getDisciplines(data) {
function pullDisciplines(data) {
if (data) {
let disciplines = new Set()
let lessons = Object.keys(data.rasp)
Expand Down
13 changes: 4 additions & 9 deletions src/store/actions/fetchGroupTeachers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
SUCCESS_GROUP_TEACHERS,
FETCHING_GROUP_TEACHERS,
} from '../actions/actionTypes'
import axios from 'axios'
import { getGroupById } from '../../api'

function error(error) {
return {
Expand All @@ -28,16 +28,11 @@ export function fetchGroupTeachers() {
dispatch(fetching())
const store = getStore()
const groupId = store.userData.groupId
const url = `https://edu.donstu.ru/api/Rasp?idGroup=${groupId}`

axios({
url,
crossDomain: true,
timeout: 15000,
}).then(
getGroupById(groupId).then(
(res) => {
if (res.data.data.info.group.name) {
const teachers = getTeachers(res.data.data)
const teachers = pullTeachers(res.data.data)
dispatch(success(teachers))
}
},
Expand All @@ -48,7 +43,7 @@ export function fetchGroupTeachers() {
}
}

function getTeachers(data) {
function pullTeachers(data) {
if (data) {
const teachers = []
const nameListSet = new Set()
Expand Down
39 changes: 19 additions & 20 deletions src/store/actions/fetchGroups.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ERROR_GROUPS, SUCCESS_GROUPS, FETCHING_GROUPS } from './actionTypes'
import axios from 'axios'
import { fetchAcademicYear } from './date'
import { getGroupList } from '../../api'

function error(error) {
return {
Expand All @@ -20,24 +21,22 @@ function fetching() {
}
}
export function fetchGroups() {
return (dispatch) => {
dispatch(fetching())
//const store = getStore()
//const year = store.fetchScheduleGroup.schedule.year
const url = `https://edu.donstu.ru/api/raspGrouplist?year=2020-2021`
axios({
url,
crossDomain: true,
timeout: 15000,
}).then(
(res) => {
const groups = res.data.data
const faculties = Array.from(new Set(groups.map(({ facul }) => facul)))
dispatch(success(groups, faculties))
},
(err) => {
dispatch(error(err))
}
)
return async (dispatch, getStore) => {
try {
await dispatch(fetching())

const { date } = getStore()

const academicYear = date.academicYear || (await dispatch(fetchAcademicYear()))

const res = await getGroupList(academicYear)

const groups = res.data.data
const faculties = Array.from(new Set(groups.map(({ facul }) => facul)))

await dispatch(success(groups, faculties))
} catch (err) {
await dispatch(error(err))
}
}
}
14 changes: 5 additions & 9 deletions src/store/actions/fetchSchedule.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ERROR_SCHEDULE, SUCCESS_SCHEDULE, FETCHING_SCHEDULE, CLEAR_SCHEDULE } from './actionTypes'
import axios from 'axios'
import { toggleOff, setDate } from './date'
import { DateTime } from 'luxon'
import { getTeacherById, getGroupById } from '../../api'

function error(error) {
return {
Expand Down Expand Up @@ -31,31 +31,27 @@ export function fetchSchedule() {
const store = getStore()
const toggleWeek = store.date.toggleWeek
const date = store.date.date
let url = ''
let promise = null
const post = store.userData.post
if (post === 'Студент') {
const groupId = store.userData.groupId
if (!groupId) {
dispatch(error('Error: Группа не выбрана'))
return
}
url = `https://edu.donstu.ru/api/Rasp?idGroup=${groupId}&sdate=${date}`
promise = getGroupById(groupId, date)
} else if (post === 'Преподаватель') {
const teacherId = store.userData.teacherId
if (!teacherId) {
dispatch(error('Error: Преподаватель не выбран'))
return
}
url = `https://edu.donstu.ru/api/Rasp?idTeacher=${teacherId}&sdate=${date}`
promise = getTeacherById(teacherId, date)
} else {
dispatch(error('Error: Ошибка при определении должности. Сообщите разработчику..'))
return
}
axios({
url,
crossDomain: true,
timeout: 20000,
}).then(
promise.then(
(res) => {
if (res.data.data.info.group.name || res.data.data.info.prepod.name) {
let tempData = dataTransformation(res.data.data)
Expand Down
Loading

0 comments on commit c47cbe1

Please sign in to comment.