-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Andres-Fernandez-Caballero/dev
Dev
- Loading branch information
Showing
21 changed files
with
718 additions
and
641 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { IProgramingLeanguaje } from '@interfaces/Domain'; | ||
import { IProgramingLeanguajeFirebaseEntity } from '@interfaces/FirebaseEntitys'; | ||
|
||
export const programingLanguages: IProgramingLeanguaje[] = [ | ||
{ name: 'C#' }, | ||
{ name: 'C++' }, | ||
{ name: 'C' }, | ||
{ name: 'Php' }, | ||
{ name: 'Java' }, | ||
{ name: 'JavaScript' }, | ||
{ name: 'TypeScript' }, | ||
{ name: 'Python' }, | ||
{ name: 'Html' }, | ||
export const programingLanguages: IProgramingLeanguajeFirebaseEntity[] = [ | ||
{ id: '1', name: 'C#' }, | ||
{ id: '2', name: 'C++' }, | ||
{ id: '3', name: 'C' }, | ||
{ id: '4', name: 'Php' }, | ||
{ id: '5', name: 'Java' }, | ||
{ id: '6', name: 'JavaScript' }, | ||
{ id: '7', name: 'TypeScript' }, | ||
{ id: '8', name: 'Python' }, | ||
{ id: '9', name: 'Html' }, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { | ||
IClassFirebaseEntity, | ||
IProgramingLeanguajeFirebaseEntity, | ||
IStudentFirebaseEntity, | ||
ITicketFirebaseEntity, | ||
} from '@interfaces/FirebaseEntitys'; | ||
import moment from 'moment/moment'; | ||
import { useEffect, useState } from 'react'; | ||
import { useAppSelector } from '@store/hooks/hook'; | ||
import { selectStudents } from '@slyces/students.slice'; | ||
import { programingLanguages } from '@constants/programingLanguages'; | ||
import { FORM_FORMAT_DATE } from '@constants/date'; | ||
import { CLASS_PRICE } from '@constants/price'; | ||
|
||
export const useClassCreateForm = ( | ||
initDurationClass: number, | ||
classPrice: number = CLASS_PRICE | ||
) => { | ||
const { students } = useAppSelector(selectStudents); | ||
const [studentSelected, setStudentSelected] = | ||
useState<IStudentFirebaseEntity>(); | ||
const [programingLanguageSelected, setProgramingLanguageSelected] = | ||
useState<IProgramingLeanguajeFirebaseEntity>( | ||
programingLanguages.filter( | ||
language => language.name.toLowerCase() === 'javascript' | ||
)[0] | ||
); | ||
const [duration, setDuration] = useState<number>(initDurationClass); | ||
const [datetime, setDatetime] = useState<string>( | ||
moment().format(FORM_FORMAT_DATE) | ||
); | ||
const [price, setPrice] = useState<number>(classPrice); | ||
const [ticket, setTicket] = useState<ITicketFirebaseEntity | undefined>(); | ||
|
||
useEffect(() => { | ||
updateTicket(); | ||
}, [price, studentSelected, programingLanguageSelected, duration, datetime]); | ||
|
||
const selectStudentById = (idUser: string) => { | ||
const studentFind = students.find( | ||
(student: IStudentFirebaseEntity) => student.id === idUser | ||
); | ||
if (studentFind === undefined) throw new Error('Student not found'); | ||
setStudentSelected(studentFind); | ||
}; | ||
|
||
const selectProgramingLanguageById = (idProgramingLanguage: string) => { | ||
const programingLanguageFind = programingLanguages.find( | ||
(programingLanguage: IProgramingLeanguajeFirebaseEntity) => | ||
programingLanguage.id === idProgramingLanguage | ||
); | ||
if (programingLanguageFind === undefined) | ||
throw new Error('Programing language not found'); | ||
setProgramingLanguageSelected(programingLanguageFind); | ||
console.log(programingLanguageSelected); | ||
}; | ||
|
||
const selectDuration = (duration: number) => { | ||
if (duration < 0) throw new Error('Duration is negative'); | ||
setDuration(duration); | ||
}; | ||
|
||
const selectDatetime = (datetime: string) => { | ||
const date = moment(datetime); | ||
if (!date.isValid()) throw new Error('Datetime is not valid'); | ||
// if (date.isAfter(moment())) throw new Error('Datetime is after now'); | ||
setDatetime(datetime); | ||
}; | ||
|
||
const createClass = (): IClassFirebaseEntity => { | ||
if (studentSelected == null) throw new Error('Seleccione un estudiante'); | ||
if (programingLanguageSelected == null) | ||
throw new Error('Programing language not found'); | ||
|
||
return { | ||
dateTime: moment(datetime).format('YYYY-MM-DDTHH:mm'), | ||
duration, | ||
programingLeanguage: programingLanguageSelected, | ||
student: studentSelected, | ||
}; | ||
}; | ||
|
||
const createTicket = (): ITicketFirebaseEntity | undefined => { | ||
if (studentSelected === undefined) return undefined; | ||
return { | ||
amount: getAmount(), | ||
date: moment().format('YYYY-MM-DDTHH:mm'), | ||
isPaid: false, | ||
class: createClass(), | ||
}; | ||
}; | ||
|
||
const updateTicket = (): void => { | ||
setTicket(createTicket()); | ||
}; | ||
|
||
const getAmount = (): number => { | ||
if (studentSelected === undefined) return 0; | ||
return price * duration; | ||
}; | ||
|
||
const updatePrice = (newPrice: number) => { | ||
if (newPrice < 0) throw new Error('El precio no puede ser negativo'); | ||
setPrice(newPrice); | ||
}; | ||
|
||
return { | ||
updateTicket, | ||
ticket, | ||
selectStudentById, | ||
selectProgramingLanguageById, | ||
selectDatetime, | ||
selectDuration, | ||
programingLanguageSelected, | ||
datetime, | ||
getAmount, | ||
price, | ||
updatePrice, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React, { ReactNode } from 'react'; | ||
import { AutocompleteChangeReason, SelectChangeEvent } from '@mui/material'; | ||
import { | ||
IProgramingLeanguajeFirebaseEntity, | ||
IStudentFirebaseEntity, | ||
} from '@interfaces/FirebaseEntitys'; | ||
|
||
export type EventGeneric = (event: React.ChangeEvent<HTMLInputElement>) => void; | ||
export type OnChangeSelectorStudent = ( | ||
event: React.SyntheticEvent, | ||
value: IStudentFirebaseEntity, | ||
reason: AutocompleteChangeReason | ||
) => void; | ||
|
||
export type OnChangeSelectorDuration = ( | ||
event: Event, | ||
newValue: number | number[] | ||
) => void; | ||
|
||
export type OnChangeSelectorDatetime = EventGeneric; | ||
export interface ClassCreateProps { | ||
onSubmit: (e: React.FormEvent<HTMLFormElement>) => void; | ||
|
||
onChangeSelectorProgramingLanguage: ( | ||
event: SelectChangeEvent<string>, | ||
child: ReactNode | ||
) => void; | ||
onChangeSelectorStudent: OnChangeSelectorStudent; | ||
programingLanguageSelected: IProgramingLeanguajeFirebaseEntity; | ||
|
||
onChangeSelectorDuration: OnChangeSelectorDuration; | ||
|
||
onChangeSelectorDatetime: OnChangeSelectorDatetime; | ||
|
||
maxDurationClass: number; | ||
durationStepRange: number; | ||
initDuration: number; | ||
selectedDatetime: string; | ||
} | ||
|
||
export interface SelectStudentProps { | ||
onChangeSelector: ( | ||
event: React.SyntheticEvent, | ||
value: IStudentFirebaseEntity, | ||
reason: string, | ||
details?: string | ||
) => void; | ||
students: IStudentFirebaseEntity[]; | ||
} | ||
|
||
export interface SelectProgramingLanguageProps { | ||
onChangeSelector: OnChangeSelectorStudent; | ||
programingLanguageSelected: IProgramingLeanguajeFirebaseEntity; | ||
} | ||
|
||
export interface SelectDurationProps { | ||
onChange: OnChangeSelectorDuration; | ||
rangeMax: number; | ||
step: number; | ||
initValue: number; | ||
} | ||
|
||
export interface SelectorDatetimeProps { | ||
onChange: OnChangeSelectorDatetime; | ||
selectedDatetime: string; | ||
} |
Oops, something went wrong.