Skip to content

Commit

Permalink
Added the utils folder to common-lib:
Browse files Browse the repository at this point in the history
* Many utilities are shared across modules, having them all in common lib may help
* For now only the utils folder has been added there, config must be modified to produce appropriate typings
  • Loading branch information
sidtohan committed Jul 16, 2022
1 parent 91028a2 commit 02ff2b3
Show file tree
Hide file tree
Showing 22 changed files with 649 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/common-lib/src/utils/customhooks/useAverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as React from 'react'

export const useAverage = ({ calendarView }) => {
const [isAverage, setIsAverage] = React.useState(false)
React.useEffect(() => {
setIsAverage(
['week', 'weeks', 'month', 'months', 'monthInDays'].includes(calendarView)
)
}, [calendarView])
return { isAverage }
}
60 changes: 60 additions & 0 deletions packages/common-lib/src/utils/customhooks/useDesignHook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Lib
import * as React from 'react'
import { calendar } from '@shiksha/common-lib'

// Utils
import { isMoment, isMoment2DArray } from 'utils/types/typeGuards'
import { PRESENT } from 'utils/functions/Constants'
import { colorTheme } from 'utils/functions/ColorTheme'
import { GetStudentsAttendance } from 'utils/functions/GetStudentsAttendance'

export const useDesignHook = ({ attendance, page, calendarView, t }) => {
const [design, setDesign] = React.useState<any>({})
const holidays = []
React.useEffect(() => {
let daysWithoutHolidays = []
if (typeof page === 'object') {
// @ts-ignore
daysWithoutHolidays = page.map((e) => {
const dat = calendar(e, calendarView ? calendarView : 'days')
if (isMoment(dat) || isMoment2DArray(dat)) return

return dat.filter(
(e) => !(!e.day() || holidays.includes(e.format('YYYY-MM-DD')))
).length
})
}
if (attendance[0]) {
let percentage = 0
let attendanceAll = GetStudentsAttendance({
attendance: attendance[0],
type: 'id'
})
let presentAttendanceCount = attendanceAll.filter(
(e) => e.attendance && e.attendance !== PRESENT
).length
percentage = (presentAttendanceCount * 100) / daysWithoutHolidays.length
if (percentage && percentage >= 100) {
setDesign({
bg: colorTheme.success,
iconName: 'EmotionHappyLineIcon',
titleHeading:
t('YOU_HAVE_BEEN_PRESENT_ALL_DAYS_THIS') + ' ' + calendarView
})
} else if (percentage && percentage < 100 && percentage >= 50) {
setDesign({
bg: colorTheme.warning,
iconName: 'EmotionNormalLineIcon',
titleHeading: t('AGERAGE_CAN_BE_IMPROVED')
})
} else {
setDesign({
bg: colorTheme.danger,
iconName: 'EmotionSadLineIcon',
titleHeading: t('ABSENT_TODAY_POOR_THAN_LAST') + ' ' + calendarView
})
}
}
}, [])
return { design }
}
15 changes: 15 additions & 0 deletions packages/common-lib/src/utils/customhooks/useGenderList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Lib
import * as React from 'react'

// Utilities
import { HandleGenderList } from 'utils/functions/HandleGenderList'

export const useGenderList = ({ students, t }) => {
const [genderList, setGenderList] = React.useState([])
React.useEffect(() => {
const genderList = HandleGenderList(students, t)
setGenderList([...genderList, t('TOTAL')])
}, [students])

return { genderList }
}
58 changes: 58 additions & 0 deletions packages/common-lib/src/utils/customhooks/usePAStudents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Lib
import { useState, useEffect } from 'react'
import { calendar } from '@shiksha/common-lib'
import { getStudentsPresentAbsent } from '@shiksha/common-lib'

// Services
import { GetAttendance, DefaultStudents } from 'services/calls/registryCalls'

// Utilities
import { MomentUnionType } from 'utils/types/types'
import { isMomentArray } from 'utils/types/typeGuards'

export const usePAStudents = ({ students, attendance, type }) => {
const holidays = []
const [paStudents, setPaStudents] = useState([])
useEffect(() => {
const getPresentStudents = async ({ students }) => {
let weekdays: MomentUnionType = calendar(-1, 'week')
// Check type, also for typescripts
if (isMomentArray(weekdays)) {
let workingDaysCount =
type.toLowerCase() === 'present'
? weekdays.filter(
(e) => !(!e.day() || holidays.includes(e.format('YYYY-MM-DD')))
)?.length
: 3
let params = {
fromDate: weekdays?.[0]?.format('YYYY-MM-DD'),
toDate: weekdays?.[weekdays.length - 1]?.format('YYYY-MM-DD')
}
if (type.toLowerCase() === 'absent') params['fun'] = 'getAbsentStudents'
let attendanceData = await GetAttendance(params)
let data
if (type.toLowerCase() === 'present')
data = getStudentsPresentAbsent(
attendanceData,
students,
workingDaysCount
)
else
data = getStudentsPresentAbsent(
attendanceData,
students,
workingDaysCount,
'Absent'
)

let dataNew = students.filter((e) =>
data.map((e) => e.id).includes(e.id)
)
setPaStudents(await DefaultStudents(dataNew))
}
}
getPresentStudents({ students })
}, [students, attendance])

return [paStudents]
}
20 changes: 20 additions & 0 deletions packages/common-lib/src/utils/customhooks/useStudentIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react'

export const useStudentIds = ({ students }) => {
const [studentIds, setStudentIds] = React.useState([])
React.useEffect(() => {
let ignore = false
const getData = async () => {
if (!ignore) {
const temp = students.map((e) => e.id)
setStudentIds(temp)
}
}
getData()
return () => {
ignore = true
}
}, [students])

return { studentIds }
}
40 changes: 40 additions & 0 deletions packages/common-lib/src/utils/customhooks/useWithoutHolidays.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Lib
import * as React from 'react'
import { calendar } from '@shiksha/common-lib'

// Utilities
import { isMoment, isMoment2DArray } from 'utils/types/typeGuards'

export const useWithoutHolidays = ({ page, calendarView }) => {
const [withoutHolidays, setWithoutHolidays] = React.useState([])
const holidays = []
React.useEffect(() => {
let daysWithoutHolidays = []
if (typeof page === 'object') {
// @ts-ignore
daysWithoutHolidays = page.map((e) => {
const dat = calendar(e, calendarView ? calendarView : 'days')
if (isMoment(dat) || isMoment2DArray(dat)) return

return dat.filter(
(e) => !(!e.day() || holidays.includes(e.format('YYYY-MM-DD')))
).length
})
setWithoutHolidays(daysWithoutHolidays)
} else {
const dat = calendar(
page ? page : 0,
calendarView ? calendarView : 'days'
)
if (isMoment(dat) || isMoment2DArray(dat)) return
daysWithoutHolidays = [
dat.filter(
(e) => !(!e.day() || holidays.includes(e.format('YYYY-MM-DD')))
).length
]
setWithoutHolidays(daysWithoutHolidays)
}
}, [calendarView, page])

return { withoutHolidays }
}
5 changes: 5 additions & 0 deletions packages/common-lib/src/utils/functions/ColorTheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import colorTheme from 'colorTheme'
import { overrideColorTheme } from '@shiksha/common-lib'
const colors = overrideColorTheme(colorTheme)

export { colors, colorTheme }
5 changes: 5 additions & 0 deletions packages/common-lib/src/utils/functions/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const PRESENT = 'Present'
export const ABSENT = 'Absent'
export const UNMARKED = 'Unmarked'
export const MALE = 'Male'
export const FEMALE = 'Female'
78 changes: 78 additions & 0 deletions packages/common-lib/src/utils/functions/CountReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Constants
import { useTranslation } from 'react-i18next'
import { MALE, FEMALE } from './Constants'
import { GetStudentsAttendance } from './GetStudentsAttendance'

export interface ICountReport {
gender?: any
isAverage?: any
attendance?: any
attendanceType?: any
type?: string
studentIds?: any
withoutHolidays?: any
students?: any
t?: any
}
export const CountReport = ({
gender,
isAverage,
attendance,
attendanceType,
type,
studentIds,
withoutHolidays,
students,
t
}: ICountReport) => {
let attendanceAll = GetStudentsAttendance({ attendance, type: 'id' })
if (gender && [t('BOYS'), t('GIRLS')].includes(gender)) {
studentIds = students
.filter(
(e) =>
e.gender ===
(gender === t('BOYS') ? MALE : gender === t('GIRLS') ? FEMALE : '')
)
.map((e) => e.id)
}

if (attendanceType === 'Unmarked' && gender === t('TOTAL')) {
let studentIds1 = attendanceAll.filter(
(e) => studentIds.includes(e.studentId) && e.attendance !== attendanceType
)
let val = studentIds.length * withoutHolidays - studentIds1.length
if (isAverage) {
return Math.round(val ? val / studentIds.length : 0)
} else {
return Math.round(val)
}
} else if (type === 'Unmarked' || attendanceType === 'Unmarked') {
let studentIds1 = attendanceAll.filter((e) =>
studentIds.includes(e.studentId)
)

if (attendanceType === 'Unmarked') {
studentIds1 = attendanceAll.filter(
(e) =>
studentIds.includes(e?.studentId) && e.attendance !== attendanceType
)
}
let val = studentIds.length * withoutHolidays - studentIds1.length
if (isAverage) {
return Math.round(val ? val / studentIds.length : 0)
} else {
return Math.round(val)
}
} else {
let val = attendanceAll.filter(
(e) =>
studentIds.includes(e?.studentId) && e.attendance === attendanceType
).length

if (isAverage) {
return Math.round(val ? val / studentIds.length : 0)
} else {
return Math.round(val)
}
}
}
33 changes: 33 additions & 0 deletions packages/common-lib/src/utils/functions/FormatDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import moment, { Moment } from 'moment'

interface IFormatDate {
date: Moment | Moment[] | Moment[][]
type?: string
}

// TODO: Remove TSignore
export const FormatDate: Function = ({ date, type }: IFormatDate) => {
if (!date) return ''
if (type === 'Month') {
return moment(date[0]).format('MMMM Y')
} else if (type === 'Week') {
return (
moment(date[0]).format('D MMM') +
' - ' +
// @ts-ignore
moment(date[date.length - 1]).format('D MMM')
)
} else if (type === 'Today') {
// @ts-ignore
return moment(date).format('D MMM, ddd, HH:MM')
} else if (type === 'Tomorrow') {
// @ts-ignore
return moment(date).format('D MMM, ddd')
} else if (type === 'Yesterday') {
// @ts-ignore
return moment(date).format('D MMM, ddd')
} else {
// @ts-ignore
return moment(date).format('D MMMM, Y')
}
}
25 changes: 25 additions & 0 deletions packages/common-lib/src/utils/functions/GetAttendanceReport.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Services
import { GetAttendance } from 'services/calls/registryCalls'

// Utils
import { isMoment, isMoment2DArray } from 'utils/types/typeGuards'
export interface IGetAttendanceReport {
(page: any, calendar: Function, calendarView: string, fun?: string): any
}

export const GetAttendanceReport: IGetAttendanceReport = async (
page,
calendar,
calendarView,
fun
) => {
let weekdays = calendar(page, calendarView)
if (isMoment(weekdays) || isMoment2DArray(weekdays)) return
let params = {
fromDate: weekdays?.[0]?.format('Y-MM-DD'),
toDate: weekdays?.[weekdays.length - 1]?.format('Y-MM-DD'),
fun
}
const attendanceData = await GetAttendance(params)
return attendanceData
}
6 changes: 6 additions & 0 deletions packages/common-lib/src/utils/functions/GetLastAttendance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import moment from 'moment'
export const GetLastAttendance = (attendance) => {
let dates = attendance.map((d) => moment(d.updatedAt))
let date = moment.max(dates)
return dates.length ? date.format('hh:mmA') : 'N/A'
}
Loading

0 comments on commit 02ff2b3

Please sign in to comment.