Skip to content

Commit

Permalink
Merge pull request #20 from Andres-Fernandez-Caballero/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Andres-Fernandez-Caballero authored Apr 7, 2023
2 parents e582207 + de44db3 commit 271b6de
Show file tree
Hide file tree
Showing 21 changed files with 718 additions and 641 deletions.
132 changes: 0 additions & 132 deletions src/components/DialogTicket/DialogTocket.tsx

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/DialogTicket/index.ts

This file was deleted.

22 changes: 11 additions & 11 deletions src/constants/programingLanguages.ts
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' },
];
120 changes: 120 additions & 0 deletions src/pages/class/ClassCreate/ClassCreate.hooks.ts
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,
};
};
66 changes: 66 additions & 0 deletions src/pages/class/ClassCreate/ClassCreate.interface.ts
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;
}
Loading

0 comments on commit 271b6de

Please sign in to comment.