Skip to content

Commit

Permalink
Made some documentation oriented changes:
Browse files Browse the repository at this point in the history
* Added comments to all functions, custom hooks and types to briefly describe their use
* Removed some redundant utilities that were not needed when the code is in utils folder
  • Loading branch information
sidtohan committed Jul 23, 2022
1 parent 1d81299 commit dd98c8a
Show file tree
Hide file tree
Showing 23 changed files with 185 additions and 74 deletions.
8 changes: 7 additions & 1 deletion packages/common-lib/src/utils/customhooks/useAverage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Lib
import * as React from 'react'

export const useAverage = ({ calendarView }) => {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,30 @@ 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'
import { isMoment, isMoment2DArray } from '../types/typeGuards'
import { PRESENT } from '../functions/Constants'
import { GetStudentsAttendance } from '../functions/GetStudentsAttendance'

export const useDesignHook = ({ attendance, page, calendarView, t }) => {
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 = []
const holidays: Array<any> = []
React.useEffect(() => {
let daysWithoutHolidays = []
if (typeof page === 'object') {
Expand Down
12 changes: 9 additions & 3 deletions packages/common-lib/src/utils/customhooks/useGenderList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
import * as React from 'react'

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

export const useGenderList = ({ students, t }) => {
const [genderList, setGenderList] = React.useState([])
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')])
Expand Down
38 changes: 27 additions & 11 deletions packages/common-lib/src/utils/customhooks/usePAStudents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ import { calendar } from '@shiksha/common-lib'
import { getStudentsPresentAbsent } from '@shiksha/common-lib'

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

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

export const usePAStudents = ({ students, attendance, type }) => {
const holidays = []
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 }) => {
const getPresentStudents = async ({
students
}: {
students: Array<any>
}) => {
let weekdays: MomentUnionType = calendar(-1, 'week')
// Check type, also for typescripts
if (isMomentArray(weekdays)) {
Expand All @@ -29,8 +45,8 @@ export const usePAStudents = ({ students, attendance, type }) => {
toDate: weekdays?.[weekdays.length - 1]?.format('YYYY-MM-DD')
}
if (type.toLowerCase() === 'absent') params['fun'] = 'getAbsentStudents'
let attendanceData = await GetAttendance(params)
let data
let attendanceData = await attendanceRegistryService.getAll(params)
let data: any
if (type.toLowerCase() === 'present')
data = getStudentsPresentAbsent(
attendanceData,
Expand All @@ -45,10 +61,10 @@ export const usePAStudents = ({ students, attendance, type }) => {
'Absent'
)

let dataNew = students.filter((e) =>
data.map((e) => e.id).includes(e.id)
let dataNew = students.filter((e: any) =>
data.map((e: any) => e.id).includes(e.id)
)
setPaStudents(await DefaultStudents(dataNew))
setPaStudents(await studentRegistryService.setDefaultValue(dataNew))
}
}
getPresentStudents({ students })
Expand Down
9 changes: 7 additions & 2 deletions packages/common-lib/src/utils/customhooks/useStudentIds.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as React from 'react'

export const useStudentIds = ({ students }) => {
const [studentIds, setStudentIds] = React.useState([])
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 () => {
Expand Down
17 changes: 13 additions & 4 deletions packages/common-lib/src/utils/customhooks/useWithoutHolidays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ import * as React from 'react'
import { calendar } from '@shiksha/common-lib'

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

export const useWithoutHolidays = ({ page, calendarView }) => {
const [withoutHolidays, setWithoutHolidays] = React.useState([])
const holidays = []
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') {
Expand Down
5 changes: 0 additions & 5 deletions packages/common-lib/src/utils/functions/ColorTheme.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/common-lib/src/utils/functions/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Stores some constants for various,
// store all the constants here
export const PRESENT = 'Present'
export const ABSENT = 'Absent'
export const UNMARKED = 'Unmarked'
Expand Down
8 changes: 5 additions & 3 deletions packages/common-lib/src/utils/functions/CountReport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Constants
import { useTranslation } from 'react-i18next'
import { MALE, FEMALE } from './Constants'
import { GetStudentsAttendance } from './GetStudentsAttendance'

Expand All @@ -14,6 +13,9 @@ export interface ICountReport {
students?: any
t?: any
}

// Returns the data corresponding to a report depending on the
// parameters passed
export const CountReport = ({
gender,
isAverage,
Expand All @@ -29,11 +31,11 @@ export const CountReport = ({
if (gender && [t('BOYS'), t('GIRLS')].includes(gender)) {
studentIds = students
.filter(
(e) =>
(e: any) =>
e.gender ===
(gender === t('BOYS') ? MALE : gender === t('GIRLS') ? FEMALE : '')
)
.map((e) => e.id)
.map((e: any) => e.id)
}

if (attendanceType === 'Unmarked' && gender === t('TOTAL')) {
Expand Down
2 changes: 2 additions & 0 deletions packages/common-lib/src/utils/functions/FormatDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ interface IFormatDate {
}

// 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') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Services
import { GetAttendance } from 'services/calls/registryCalls'
import { attendanceRegistryService } from '../..'

// Utils
import { isMoment, isMoment2DArray } from 'utils/types/typeGuards'
import { isMoment, isMoment2DArray } from '../types/typeGuards'
export interface IGetAttendanceReport {
(page: any, calendar: Function, calendarView: string, fun?: string): any
}
Expand All @@ -20,6 +20,6 @@ export const GetAttendanceReport: IGetAttendanceReport = async (
toDate: weekdays?.[weekdays.length - 1]?.format('Y-MM-DD'),
fun
}
const attendanceData = await GetAttendance(params)
const attendanceData = await attendanceRegistryService.getAll(params)
return attendanceData
}
13 changes: 13 additions & 0 deletions packages/common-lib/src/utils/functions/GetColorTheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Lib
import { overrideColorTheme } from '@shiksha/common-lib'

export interface IGetColors {
colorTheme: any
}
// uses the override color theme function to return a specified color theme
// parameters: ColorThem to be applied
const GetColorTheme = (colorTheme: IGetColors) => {
return { colors: overrideColorTheme, colorTheme }
}

export { GetColorTheme }
8 changes: 7 additions & 1 deletion packages/common-lib/src/utils/functions/GetLastAttendance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import moment from 'moment'
export const GetLastAttendance = (attendance) => {
// Gets the last attendance from a provided array of attendance;

export interface IGetLastAttendance {
(attendance: Array<any>): string
}

export const GetLastAttendance: IGetLastAttendance = (attendance) => {
let dates = attendance.map((d) => moment(d.updatedAt))
let date = moment.max(dates)
return dates.length ? date.format('hh:mmA') : 'N/A'
Expand Down
3 changes: 2 additions & 1 deletion packages/common-lib/src/utils/functions/GetPercentage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ export interface IGetPercentage {
): any
}

// Returns the percentage of students who are absent/present
export const GetPercentage: IGetPercentage = (
attendances,
student,
count,
status = 'Present',
type = 'percentage',
page,
compare
compare = ''
) => {
let weekdays = calendar(
page,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export const GetStatusFromManifest = (manifest) => {
// From a given manifest, returns the status
// currently used for attendance but may be generalised
export interface IGetStatusFromManifest {
(manifest: any): any
}

export const GetStatusFromManifest: IGetStatusFromManifest = (manifest) => {
const status = Array.isArray(
manifest?.['attendance.default_attendance_states']
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ export interface IGetStudentsAttendance {
attendance: Array<any>
type?: string
}
// type tells if format matching or the other one

// Returns attendance following a certain criteria
// type specifies on what criteria
export const GetStudentsAttendance = ({
students,
students = [],
attendance,
type
type = 'format'
}: IGetStudentsAttendance) => {
if (type.toLowerCase() === 'format')
return students
Expand Down
20 changes: 16 additions & 4 deletions packages/common-lib/src/utils/functions/HandleAttendanceData.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
// Lib
import moment from 'moment'
import moment, { Moment } from 'moment'

// Utils
import { PRESENT, ABSENT, UNMARKED } from './Constants'
import { GetStatusFromManifest } from './GetStatusFromManifest'

export interface IHandleAttendanceData {
attendance: Array<any>
day: Moment
sms: any
isIconSizeSmall: Boolean
student: any
manifest: any
}

// Returns various data related to attendance
// such as if day is allowed, the icon props for the attendance icon
export const HandleAttendanceData = ({
attendance,
day,
sms,
isIconSizeSmall,
student,
manifest
}) => {
const holidays = []
}: IHandleAttendanceData) => {
const holidays: Array<any> = []
const status = GetStatusFromManifest(manifest)
let isToday = moment().format('YYYY-MM-DD') === day.format('YYYY-MM-DD')
let isAllowDay = false
Expand All @@ -26,7 +37,8 @@ export const HandleAttendanceData = ({
let isHoliday = day.day() === 0 || holidays.includes(day.format('YYYY-MM-DD'))
let dateValue = day.format('YYYY-MM-DD')
let smsDay = sms?.find(
(e) => e.date === day.format('YYYY-MM-DD') && e.studentId === student.id
(e: any) =>
e.date === day.format('YYYY-MM-DD') && e.studentId === student.id
)
let attendanceItem = attendance
.slice()
Expand Down
13 changes: 10 additions & 3 deletions packages/common-lib/src/utils/functions/HandleGenderList.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { MALE, FEMALE } from './Constants'
export const HandleGenderList = (students, t) => {

export interface IHandleGenderList {
(students: any, t: any): number[]
}

// Returns number of students
// which belong to the specified gender
export const HandleGenderList: IHandleGenderList = (students, t) => {
let genderList = []
genderList = [t('BOYS'), t('GIRLS')].filter((gender) => {
return (
(gender === t('BOYS') &&
students.filter((e) => e.gender === MALE).length) ||
students.filter((e: any) => e.gender === MALE).length) ||
(gender === t('GIRLS') &&
students.filter((e) => e.gender === FEMALE).length)
students.filter((e: any) => e.gender === FEMALE).length)
)
})
return genderList
Expand Down
Loading

0 comments on commit dd98c8a

Please sign in to comment.