Skip to content
Snippets Groups Projects
Commit f6c97648 authored by Elisabethein's avatar Elisabethein
Browse files

Merge remote-tracking branch 'emergency-alert-application/main' into Elisabet

parents 27e2b3c4 97f256e6
No related branches found
No related tags found
No related merge requests found
......@@ -173,11 +173,17 @@ export default {
editGeneralMaterial: false,
editselectedSpeciesGroup: '',
alertShown: false,
user: {
id: null,
species: []
},
};
},
created() {
this.fetchMaterials();
this.fetchSpeciesGroups();
this.fetchUser().then(() => {
this.fetchMaterials();
this.fetchSpeciesGroups();
});
},
computed: {
hasJuhtkondRole() {
......@@ -215,7 +221,7 @@ export default {
const token = this.getAuthToken();
try {
const response = await fetch('http://localhost:8080/api/materials/add', {
//const response = await fetch('api/materials/add', {
//const response = await fetch('api/materials/add', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
......@@ -252,7 +258,7 @@ export default {
const token = this.getAuthToken();
try {
const response = await fetch('http://localhost:8080/api/materials/all', {
//const response = await fetch('api/materials/all', {
//const response = await fetch('api/materials/all', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
......@@ -284,8 +290,7 @@ export default {
this.upperSpeciesList = data.map(group => group.name);
} else if (response.status === 403) {
this.handleUnauthorized();
}
else {
} else {
console.error('Failed to fetch species groups:', response.status);
}
} catch (error) {
......@@ -300,7 +305,7 @@ export default {
const token = this.getAuthToken();
try {
const response = await fetch(`http://localhost:8080/api/materials/delete/${material.id}`, {
//const response = await fetch(`api/materials/delete/${material.id}`, {
//const response = await fetch(`api/materials/delete/${material.id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
......@@ -337,7 +342,7 @@ export default {
const token = this.getAuthToken();
try {
const response = await fetch(`http://localhost:8080/api/materials/update/${this.editMaterialId}`, {
//const response = await fetch(`api/materials/update/${this.editMaterialId}`, {
//const response = await fetch(`api/materials/update/${this.editMaterialId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
......@@ -364,23 +369,79 @@ export default {
},
filterMaterials() {
this.filteredMaterials = this.materials.filter(material => {
// Kontrollime, kas materjal vastab valitud liigigruppidele ja otsingusõnale
filterMaterialsForSearch() {
// Filtreerime ainult nende materjalide põhjal, mis on juba kasutaja grupi järgi filtreeritud
this.filteredMaterials = this.filteredMaterials.filter(material => {
const matchesSpeciesGroup = this.selectedSpecies.length === 0 || this.selectedSpecies.some(group => material.selectedSpeciesGroup === group);
const matchesSearchTerm = material.title.toLowerCase().includes(this.searchTerm.toLowerCase()) ||
material.description.toLowerCase().includes(this.searchTerm.toLowerCase());
// Kontrollime, kas materjal vastab valitud materjalitüübile (üldine või kõik)
const matchesMaterialType =
this.selectedMaterialType === 'all' ||
(this.selectedMaterialType === 'general' && material.generalMaterial === true);
// Tagastame, kui kõik filtrid on täidetud
return matchesSpeciesGroup && matchesSearchTerm && matchesMaterialType;
});
},
filterMaterialsForUser() { // Leiab kasutajale ainult üldised või tema liigigrupi materjalid
this.filteredMaterials = this.materials.filter(material => {
const matchesUserSpeciesGroup = material.generalMaterial === true ||
this.user.species.some(userGroup => {
return String(material.selectedSpeciesGroup) === String(userGroup.name);
});
return matchesUserSpeciesGroup;
});
},
filterMaterials() {
// Kui kasutaja ei ole juhtkonnas, siis rakendame kasutaja liigigruppide filtri
if (!this.hasJuhtkondRole) {
this.filterMaterialsForUser();
}
// Seejärel rakendame otsingutermini ja materjalitüübi filtri
this.filterMaterialsForSearch();
},
async fetchUser() {
const currentUser = this.$store.state.user; // Assuming user data is stored in the store
// Fetch species for the current user
const [species] = await Promise.all([
this.fetchData(`users/${currentUser.id}/species`)
]);
// Merge fetched data into user object
this.user = {
...currentUser,
species: species || []
};
},
async fetchData(endpoint) {
const token = this.getAuthToken();
const response = await fetch(`http://localhost:8080/api/${endpoint}`, {
//const response = await fetch(`api/${endpoint}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
});
if (!response.ok) {
if (response.status === 403) {
this.handleUnauthorized();
} else {
console.error(`Failed to fetch data`);
}
return null;
}
return response.json();
},
},
};
......@@ -596,6 +657,7 @@ input[type="text"] {
width: 400px;
max-width: 90%;
}
.custom-dropdown,
.filters-section {
display: flex;
......@@ -604,6 +666,7 @@ input[type="text"] {
width: 80%;
margin-bottom: 20px;
}
.custom-dropdown,
.filters {
display: flex;
......@@ -612,6 +675,7 @@ input[type="text"] {
width: 40%;
margin-bottom: 20px;
}
.custom-dropdown {
width: 100%;
padding: 8px;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment