Skip to content

Commit

Permalink
Adding filters to a lot of models
Browse files Browse the repository at this point in the history
  • Loading branch information
c2d13p committed Jul 19, 2024
1 parent 1762095 commit 86e06cb
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 12 deletions.
39 changes: 37 additions & 2 deletions src/models/EventConference.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Route } from 'react-router-dom'
import { useState } from 'react'

import ModelEditPage from '../pages/ModelEditPage'
import ModelsPage from '../pages/ModelsPage'
Expand Down Expand Up @@ -26,12 +27,11 @@ export default class EventConference {
'SSD': 'SSD',
'conferenceRoom': 'Aula',
}

this.schema = null

this.IndexPage = ModelsPage
this.ViewPage = ModelViewPage
this.EditPage = ModelEditPage
this.Filters = ConferencesFilters
}

// absolute url of objects index
Expand Down Expand Up @@ -73,3 +73,38 @@ export default class EventConference {
return [indexRouter, viewRouter, editRouter].filter(Boolean)
}
}

function ConferencesFilters({filter}) {
const setFilterFields = filter.setFilter
const [year, setYear] = useState(0)
const currentYear = new Date().getFullYear()
const startYear = 2016
const years = Array.from({ length: currentYear - startYear + 1 }, (_, i) => currentYear - i)

return <>
<select
className="mx-1 form-control"
style={{ width: '10%' }}
placeholder='seleziona anno'
value={year || ""}
onChange={evt => {
const year = parseInt(evt.target.value)
setYear(year)
if (year) {
setFilterFields(prev => ({
...prev,
startDate__lt_or_null: `${year+1}-01-01`,
endDate__gte_or_null: `${year}-01-01`,
}));
} else {
setFilterFields(prev => {
const { startDate__lt_or_null, endDate__gte_or_null, ...rest } = prev;
return rest;
});
}
}}>
<option value="">Tutti gli anni</option>
{years.map(y => <option key={y} value={y}>{y}</option>)}
</select>
</>
}
39 changes: 37 additions & 2 deletions src/models/EventPhdCourse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Route } from 'react-router-dom'
import { Link } from 'react-router-dom'
import { useState } from 'react'

import ModelsPage from '../pages/ModelsPage'
import PhdCourseEditPage from '../pages/PhdCourseEditPage'
Expand Down Expand Up @@ -34,12 +35,11 @@ export default class EventPhdCourse {
'endDate': 'Data Fine',
'lecturers': 'Docente/i',
}

this.schema = null

this.IndexPage = ModelsPage
this.ViewPage = PhdCourseViewPage
this.EditPage = PhdCourseEditPage
this.Filters = PhdCoursesFilters
}

// absolute url of objects index
Expand Down Expand Up @@ -91,3 +91,38 @@ export default class EventPhdCourse {
return [indexRouter, viewRouter, editRouter].filter(Boolean)
}
}

function PhdCoursesFilters({filter}) {
const setFilterFields = filter.setFilter
const [year, setYear] = useState(0)
const currentYear = new Date().getFullYear()
const startYear = 2023
const years = Array.from({ length: currentYear - startYear + 1 }, (_, i) => currentYear - i)

return <>
<select
className="mx-1 form-control"
style={{ width: '10%' }}
placeholder='seleziona anno'
value={year || ""}
onChange={evt => {
const year = parseInt(evt.target.value)
setYear(year)
if (year) {
setFilterFields(prev => ({
...prev,
startDate__lt_or_null: `${year+1}-01-01`,
endDate__gte_or_null: `${year}-01-01`,
}));
} else {
setFilterFields(prev => {
const { startDate__lt_or_null, endDate__gte_or_null, ...rest } = prev;
return rest;
});
}
}}>
<option value="">Tutti gli anni</option>
{years.map(y => <option key={y} value={y}>{y}</option>)}
</select>
</>
}
16 changes: 9 additions & 7 deletions src/models/EventSeminar.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export default class EventSeminar {
'category': 'Categoria',
'startDatetime': 'Inizio',
}

this.schema = null

this.IndexPage = ModelsPage
this.ViewPage = ModelViewPage
this.EditPage = ModelEditPage
Expand Down Expand Up @@ -80,9 +78,13 @@ function SeminarsFilters({filter}) {
const setFilterFields = filter.setFilter
const [year, setYear] = useState(0)
const currentYear = new Date().getFullYear()
const years = Array.from({length: 10} , (_, i) => currentYear + 1 - i)
const startYear = 2013
const years = Array.from({ length: currentYear - startYear + 1 }, (_, i) => currentYear - i)

return <>
<select
<select
className="mx-1 form-control"
style={{ width: '10%' }}
placeholder='seleziona anno'
value={year || ""}
onChange={evt => {
Expand All @@ -91,12 +93,12 @@ function SeminarsFilters({filter}) {
if (year) {
setFilterFields(prev => ({
...prev,
startDatetime__gte: `${year}-01-01`,
startDatetime__lt: `${year+1}-01-01`,
startDatetime__gte_or_null: `${year}-01-01`,
startDatetime__lt_or_null: `${year+1}-01-01`,
}))
} else {
setFilterFields(prev => {
const {startDatetime__gte, startDatetime__lt, ...rest} = prev
const {startDatetime__gte_or_null, startDatetime__lt_or_null, ...rest} = prev
return rest
})
}
Expand Down
70 changes: 69 additions & 1 deletion src/models/Grant.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Model from './Model'
import { useState } from 'react'

export default class Grant extends Model {
constructor() {
Expand Down Expand Up @@ -26,10 +27,77 @@ export default class Grant extends Model {
'pi': "pi",
'updatedAt': "modificato",
}
this.Filters = GrantsFilters
}

describe(grant) { return grant?.name }
}

function GrantsFilters({ filter }) {
const setFilterFields = filter.setFilter
const [year, setYear] = useState(0)
const [status, setStatus] = useState('all')
const currentYear = new Date().getFullYear()
const startYear = 2007
const years = Array.from({ length: currentYear - startYear + 1 }, (_, i) => currentYear - i)

return (
<>
<select
className="mx-1 form-control"
style={{ width: '10%' }}
placeholder='Seleziona anno'
value={year || ""}
onChange={evt => {
const year = parseInt(evt.target.value);
setYear(year);
if (year) {
setFilterFields(prev => ({
...prev,
startDate__lt_or_null: `${year + 1}-01-01`,
endDate__gte_or_null: `${year}-01-01`,
}));
} else {
setFilterFields(prev => {
const { startDate__lt_or_null, endDate__gte_or_null, ...rest } = prev;
return rest;
});
}
}}
>
<option value="">Tutti gli anni</option>
{years.map(y => <option key={y} value={y}>{y}</option>)}
</select>
<select
className="mx-1 form-control"
style={{ width: '10%' }}
value={status}
onChange={evt => {
const status = evt.target.value;
setStatus(status);
setFilterFields(prev => {
const { endDate__gte_or_null, endDate__lt, ...rest } = prev;


if (status === 'active') {
return {
...rest,
endDate__gte_or_null: 'today',
};
} else if (status === 'inactive') {
return {
...rest,
endDate__lt: 'today',
};
} else {
return rest;
}
});
}}
>
<option value="all">Tutti</option>
<option value="active">Attivi</option>
<option value="inactive">Non attivi</option>
</select>
</>
);
}
70 changes: 70 additions & 0 deletions src/models/Group.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Model from './Model'
import { useState } from 'react'

export default class Group extends Model {
constructor() {
Expand All @@ -21,8 +22,77 @@ export default class Group extends Model {
'members': "membri",
'updatedAt': "modificato",
}
this.Filters = GroupsFilters
}

describe(obj) { return `${obj?.name}` }
}

function GroupsFilters({ filter }) {
const setFilterFields = filter.setFilter
const [year, setYear] = useState(0)
const [status, setStatus] = useState('all')
const currentYear = new Date().getFullYear()
const startYear = 2007
const years = Array.from({ length: currentYear - startYear + 1 }, (_, i) => currentYear - i)

return (
<>
<select
className="mx-1 form-control"
style={{ width: '10%' }}
placeholder='Seleziona anno'
value={year || ""}
onChange={evt => {
const year = parseInt(evt.target.value);
setYear(year);
if (year) {
setFilterFields(prev => ({
...prev,
startDate__lt_or_null: `${year + 1}-01-01`,
endDate__gte_or_null: `${year}-01-01`,
}));
} else {
setFilterFields(prev => {
const { startDate__lt_or_null, endDate__gte_or_null, ...rest } = prev;
return rest;
});
}
}}
>
<option value="">Tutti gli anni</option>
{years.map(y => <option key={y} value={y}>{y}</option>)}
</select>
<select
className="mx-1 form-control"
style={{ width: '10%' }}
value={status}
onChange={evt => {
const status = evt.target.value;
setStatus(status);
setFilterFields(prev => {
const { endDate__gte_or_null, endDate__lt, ...rest } = prev;

if (status === 'active') {
return {
...rest,
endDate__gte_or_null: 'today',
};
} else if (status === 'inactive') {
return {
...rest,
endDate__lt: 'today',
};
} else {
return rest;
}
});
}}
>
<option value="all">Tutti</option>
<option value="active">Attivi</option>
<option value="inactive">Non attivi</option>
</select>
</>
);
}
37 changes: 37 additions & 0 deletions src/models/RoomAssignment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Model from './Model'
import { useState } from 'react'

export default class RoomAssignment extends Model {
constructor() {
Expand All @@ -23,9 +24,45 @@ export default class RoomAssignment extends Model {
'room': "stanza",
'updatedAt': "modificato",
}
this.Filters = RoomAssignmentsFilters
}

describe(obj) {
return `${obj.person?.lastName} ${obj.room?.building}${obj.room?.floor} ${obj.room?.number}`
}
}

function RoomAssignmentsFilters({filter}) {
const setFilterFields = filter.setFilter
const [year, setYear] = useState(0)
const currentYear = new Date().getFullYear()
const startYear = 2016;
const years = Array.from({ length: currentYear - startYear + 1 }, (_, i) => currentYear - i);

return <>
<select
className="mx-1 form-control"
style={{ width: '10%' }}
placeholder='seleziona anno'
value={year || ""}
onChange={evt => {
const year = parseInt(evt.target.value)
setYear(year)
if (year) {
setFilterFields(prev => ({
...prev,
startDate__lt_or_null: `${year+1}-01-01`,
endDate__gte_or_null: `${year}-01-01`,
}));
} else {
setFilterFields(prev => {
const { startDate__lt_or_null, endDate__gte_or_null, ...rest } = prev;
return rest;
});
}
}}>
<option value="">Tutti gli anni</option>
{years.map(y => <option key={y} value={y}>{y}</option>)}
</select>
</>
}
Loading

0 comments on commit 86e06cb

Please sign in to comment.