Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/attendants object CRUD #46

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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: 14 additions & 3 deletions declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@ let TStudent = {
// JavaScript equivalent for TAttendance interface
let TAttendance = {
id: '',
Date:new Date(),
argoument: '',
students: []//array<{nome, arrivo, uscita}>
//"yyyy-MM-ddTh:m:s" This is standardized and will work reliably
Date:new Date('yyyy-mm-dd'),
argument: '',
attendants: [
{
//id studente? potrebbe bastare e poi semplicemente prendere
//nome e cognome dell'id inserito? o sarebbe troppo complicato?
// name: '',
// lastName: '',
studentId: '',
arrivo: 'hh:mm',
uscita: 'hh:mm'
}
]
};
75 changes: 67 additions & 8 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,43 @@ const updateRegister = ({ id, name, students, votes, attendances }) => {
// Controllare se l'arrivo non deve superare un certo valore
// che sarebbe l'orario di uscita?
// poi si dovrebbe anche controllare che non ci siano due lezioni con lo stesso timestamp
const createAttendance = ({ registerId, date, argument, attendants }) => {
const createAttendance = (
{ registerId,
year,
month,
day,
argument
}) => {

//questo deve prendere l'id dal localstorage
lastAttId = parseInt(lastAttId);
lastAttId++;

//attendance dovrebbe avere anche un id, cosi possiamo avere anche presenze sdoppiate
const Attendance = {
date: new Date(date), //"yyyy-MM-ddTh:m:s" This is standardized and will work reliably
//proposta: eseguire la validazione del mese su HTML

//console.table([{'year': year, 'month': month, 'day': day}]);
const date = `${year}-${month}-${day}`;
let oldAttendances = getRegister(registerId).attendances;

let newAttendance = {
id: '' + lastAttId,
date: new Date(date),
argument: argument,
attendants: [] //array<{nome, arrivo, uscita}>
};
attendants: [],
}

Attendance.attendants.push(...attendants);
getRegister(registerId).attendances.push(Attendance);
const newAttendances = [...oldAttendances, newAttendance];

//le nuove attendances devono essere inserite nel localStorage
getRegister(registerId).attendances = newAttendances;
}

const getAttendance = (registerId, attendanceId) => {
const register = getRegister(registerId);
return register.attendances
.find(attendance => attendance.id === attendanceId)
};

const deleteAttendance = ({ registerId, attendanceId }) => {
const lessons = getRegister(registerId).attendances;
for (let index = 0; index < lessons.length; index++) {
Expand All @@ -117,6 +138,44 @@ const deleteAttendance = ({ registerId, attendanceId }) => {
console.log(`lesson with id: ${attendanceId} not found.`);
}

//spicy zone: codice molto spicy ahead
const addAttendee = ({registerId, AttendanceId, studentId, entry, departure}) => {
let oldAttendants = getAttendance(registerId, AttendanceId).attendants;

getAttendance(registerId, AttendanceId).attendants = [...oldAttendants, {
//mettiamo uno studente o solo il suo id?
studentId,
//mettiamo una Date() o una semplice stringa?
//se stringa allora occorre validation sempre in HTML
arrivo: entry || null,
uscita: departure || null,
}];
}

const deleteAttendee = ({registerId, AttendanceId, studentId}) => {
let oldAttendants = getAttendance(registerId, AttendanceId).attendants;

getAttendance(registerId, AttendanceId).attendants = oldAttendants
.filter(attendant => attendant.studentId !== studentId);
}

const updateAttendee = ({registerId, AttendanceId, studentId, entry, departure}) => {
let oldAttendants = getAttendance(registerId, AttendanceId).attendants;

getAttendance(registerId, AttendanceId).attendants = oldAttendants
.map(attendant => {
if (attendant.studentId === studentId){
return {
studentId,
entry: entry || null,
departure: departure || null
}
} else {
return attendant
}
});
}

// Funzione per creare uno studente
const createStudent = ({ name, lastName, email, lectures }) => {
lastStudId = parseInt(lastStudId);
Expand Down