Add pinia dependency for state management

This commit is contained in:
andrea.terzani
2024-10-21 13:41:28 +02:00
parent 2e2262bdd7
commit 78c90e9730
8 changed files with 172 additions and 139 deletions

View File

@@ -0,0 +1,51 @@
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { ScenarioService } from '../service/ScenarioService'
import { UserPrefStore } from './UserPrefStore'
export const ScenarioStore = defineStore('scenario_store', () => {
const projectScenarios = ref([])
const globalScenarios = ref([])
const applicationScenarios = ref([])
const filterString = ref('')
const allScenarios = ref([])
const userPrefStore = UserPrefStore()
async function fetchScenarios() {
await ScenarioService.getScenariosProject(userPrefStore.selectedProject).then(resp => {
projectScenarios.value = resp.data;
allScenarios.value = [...projectScenarios.value]
});
}
async function fetchApplicationScenarios() {
await ScenarioService.getScenariosApplication(userPrefStore.selectedApp).then(resp=>{
console.log("response scenari", resp);
applicationScenarios.value = resp.data
allScenarios.value = [...projectScenarios.value, ...applicationScenarios.value]
})
}
const scenarios = computed(() => {
return allScenarios.value
})
const filteredScenarios = computed(() => {
console.log("scenarios", allScenarios.value);
return allScenarios.value.filter((item) => {
return filterString.value
.toLowerCase()
.split(" ")
.every((v) => item.name.toLowerCase().includes(v));
});
})
return {filteredScenarios, projectScenarios, applicationScenarios, fetchScenarios,fetchApplicationScenarios,scenarios,filterString }
})

View File

@@ -0,0 +1,32 @@
import { useAuth } from '@websanova/vue-auth/src/v3.js';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
export const UserPrefStore = defineStore('userpref_store', () => {
const user = ref(null)
const userLoaded = ref(false)
const selectedApp = ref(null)
async function fetchUserData(){
const auth = useAuth();
await auth.fetch().then((fetchedUser) => {
user.value = fetchedUser.data.data;
userLoaded.value = true;
}).catch((error) => {
reject(error);
});
};
function setSelectedApp(app){
selectedApp.value = app;
}
const selectedProject = computed(() => user.value.selectedProject)
const availableApp = computed(() => user.value.selectedProject.lstApplications)
return { user,fetchUserData,userLoaded,selectedProject,availableApp,setSelectedApp,selectedApp }
})