Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/common-lib/src/utils/customhooks/useAverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Lib
import * as React from 'react'

export interface IUseAverage {
calendarView: string
}
// This hook is used to set how the average is to be computed
// i.e over week, month or something else
export const useAverage = ({ calendarView }: IUseAverage) => {
const [isAverage, setIsAverage] = React.useState(false)
React.useEffect(() => {
setIsAverage(
['week', 'weeks', 'month', 'months', 'monthInDays'].includes(calendarView)
)
}, [calendarView])
return { isAverage }
}
76 changes: 76 additions & 0 deletions packages/common-lib/src/utils/customhooks/useDesign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Lib
import * as React from 'react'
import { calendar } from '@shiksha/common-lib'

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

export interface IUseDesign {
attendance: Array<any>
page: number
colorTheme: any
calendarView: string
t: any
}

// Creates a design hook that helps with maintaining the
// design state
// Currently used in attendance
export const useDesign = ({
attendance,
page,
colorTheme,
calendarView,
t
}: IUseDesign) => {
const [design, setDesign] = React.useState<any>({})
const holidays: Array<any> = []
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 }
}
21 changes: 21 additions & 0 deletions packages/common-lib/src/utils/customhooks/useGenderList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Lib
import * as React from 'react'

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

export interface IUseGenderList {
students: Array<any>
t: any
}
// Maintains the genderList state
// Currently used in attendance
export const useGenderList = ({ students, t }: IUseGenderList) => {
const [genderList, setGenderList] = React.useState<Array<any>>([])
React.useEffect(() => {
const genderList = HandleGenderList(students, t)
setGenderList([...genderList, t('TOTAL')])
}, [students])

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

// Services
import { attendanceRegistryService, studentRegistryService } from '../..'

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

export interface IUsePAStudents {
students: Array<any>
attendance: Array<any>
type: string
}

// A flexible hook used for maintaining list of present/absent students
// passing parameters allows us to configure what kind we need
export const usePAStudents = ({
students,
attendance,
type
}: IUsePAStudents) => {
const holidays: Array<any> = []
const [paStudents, setPaStudents] = useState([])
useEffect(() => {
const getPresentStudents = async ({
students
}: {
students: Array<any>
}) => {
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 attendanceRegistryService.getAll(params)
let data: any
if (type.toLowerCase() === 'present')
data = getStudentsPresentAbsent(
attendanceData,
students,
workingDaysCount
)
else
data = getStudentsPresentAbsent(
attendanceData,
students,
workingDaysCount,
'Absent'
)

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

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

export interface IUseStudentsIds {
students: Array<any>
}
// Used for maintaing list of student ids
// used in attendance
export const useStudentIds = ({ students }: IUseStudentsIds) => {
const [studentIds, setStudentIds] = React.useState<any>([])
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 }
}
49 changes: 49 additions & 0 deletions packages/common-lib/src/utils/customhooks/useWithoutHolidays.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Lib
import * as React from 'react'
import { calendar } from '@shiksha/common-lib'

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

export interface IUseWithoutHolidays {
page: any
calendarView: string
}
// This hook maintains the list of days that are not a holiday
// currently used in attendance
export const useWithoutHolidays = ({
page,
calendarView
}: IUseWithoutHolidays) => {
const [withoutHolidays, setWithoutHolidays] = React.useState<Array<any>>([])
const holidays: Array<any> = []
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 }
}
7 changes: 7 additions & 0 deletions packages/common-lib/src/utils/functions/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Stores some constants for various,
// store all the constants here
export const PRESENT = 'Present'
export const ABSENT = 'Absent'
export const UNMARKED = 'Unmarked'
export const MALE = 'Male'
export const FEMALE = 'Female'
80 changes: 80 additions & 0 deletions packages/common-lib/src/utils/functions/CountReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Constants
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
}

// Returns the data corresponding to a report depending on the
// parameters passed
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: any) =>
e.gender ===
(gender === t('BOYS') ? MALE : gender === t('GIRLS') ? FEMALE : '')
)
.map((e: any) => 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)
}
}
}
35 changes: 35 additions & 0 deletions packages/common-lib/src/utils/functions/FormatDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import moment, { Moment } from 'moment'

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

// TODO: Remove TSignore
// Formats the date given the data(moment object) and type
// Returns a formatted string
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')
}
}
Loading