Skip to content

Commit

Permalink
UpdateToAssemblyCalc
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianlinkflink committed Jan 21, 2025
1 parent 162ac6b commit 2f8283f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 16 deletions.
11 changes: 11 additions & 0 deletions src/components/Misc/Settings/SettingsMaterial.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
<UpdateButton @click="updateMaterial" />
</dd>
</div>
<div class="pt-6 sm:flex">
<dt class="font-medium text-gray-900 sm:w-64 sm:flex-none sm:pr-6">Fetch global assemblies</dt>
<dd class="mt-1 flex justify-between gap-x-6 sm:mt-0 sm:flex-auto">
<input
type="checkbox"
class="h-4 w-4 rounded border-gray-300 text-green-600 focus:ring-green-500"
v-model="settingsStore.materialSettings.globalAssemblies"
/>
<UpdateButton @click="updateMaterial" />
</dd>
</div>
<div class="pt-6 sm:flex">
<dt class="font-medium text-gray-900 sm:w-64 sm:flex-none sm:pr-6">Filter parameters</dt>
<dd class="mt-1 flex justify-between gap-x-6 sm:mt-0 sm:flex-auto">
Expand Down
3 changes: 1 addition & 2 deletions src/components/Modals/AssemblyModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ export default defineComponent({
const materialType = ref('')
const assemblyId = ref(crypto.randomUUID().toString())
const codes = BSAB96
//const filteredProducts = ref<Product[] | Assembly[]>([])
const filterParameters = settingsStore.materialSettings.filterParams
const sortingParameters = settingsStore.materialSettings.sortingParams
Expand All @@ -267,6 +265,7 @@ export default defineComponent({
// This is the assembly we are constructing
const assemblyMaterials = ref<Product[]>([])
// TODO: Make a static list of categories as a type
const categories = ref({
materialTypes: [
{ label: 'Wood', value: 'wood', selected: false },
Expand Down
2 changes: 2 additions & 0 deletions src/models/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface CalculationSettings {
export interface MaterialSettings {
Source: Source
includeCollections: boolean
globalAssemblies: boolean
filterParams: MaterialFilterParam[]
sortingParams: MaterialSortingParam[]
}
Expand Down Expand Up @@ -136,6 +137,7 @@ export const standardCalculationSettings: CalculationSettings = {
export const standardMaterialSettings: MaterialSettings = {
Source: Source.Revalu,
includeCollections: true,
globalAssemblies: false,
filterParams: [
{
paramName: 'metaData.Collection',
Expand Down
37 changes: 27 additions & 10 deletions src/stores/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
orderBy,
limit,
getDocs,
writeBatch
writeBatch,
doc
} from 'firebase/firestore'
import type {
Mapping,
Expand Down Expand Up @@ -333,20 +334,35 @@ export const useFirebaseStore = defineStore('firebase', {

/**
* Fetches the whole assembly list for a project
* @param projectId
* @returns
* @param projectId optional projectId to filter on if omitted gets all assemblies
* @returns list of assemblies
*/
async fetchAssemblyList(projectId: string): Promise<AssemblyList | null> {
async fetchAssemblyList(projectId: string | boolean): Promise<AssemblyList | null> {
this.loading = true
this.error = null
try {
const q = query(
collection(db, 'projectAssemblies'),
where('projectId', '==', projectId)
)
let q
if (projectId === true) {
q = query(
collection(db, 'projectAssemblies')
)
} else {
q = query(
collection(db, 'projectAssemblies'),
where('projectId', '==', projectId)
)
}

const querySnapshot = await getDocs(q)
if (!querySnapshot.empty) {
const assemblyList = querySnapshot.docs[0].data() as AssemblyList
const assemblyList: AssemblyList = {
projectId: projectId as string,
assemblies: []
}
querySnapshot.forEach(doc => {
const data = doc.data() as AssemblyList
assemblyList.assemblies.push(... data.assemblies)
})
return assemblyList
} else {
return null
Expand All @@ -371,7 +387,8 @@ export const useFirebaseStore = defineStore('firebase', {

try {
const querySnapshot = await getDocs(
query(collection(db, 'projectAssemblies'), where('projectId', '==', projectId))
query(collection(db, 'projectAssemblies'),
where('projectId', '==', projectId))
)

const assemblyList: AssemblyList = {
Expand Down
16 changes: 13 additions & 3 deletions src/utils/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useSpeckleStore } from "@/stores/speckle"
import { useFirebaseStore } from "@/stores/firebase"

import { setMappingColorGroup, updateProjectGroups } from "@/utils/projectUtils"
import { useSettingsStore } from "@/stores/settings"

/**
* Updates from a selected mapping to a new one, with all materials and objectIds
Expand Down Expand Up @@ -199,12 +200,21 @@ export async function getAssemblyList() {
const materialStore = useMaterialStore()
const projectStore = useProjectStore()
const firebaseStore = useFirebaseStore()
const settingsStore = useSettingsStore()

try {
const assemblyList: AssemblyList = await firebaseStore.fetchAssemblyList(projectStore.currProject.id)
if (assemblyList) {
materialStore.assemblies = assemblyList.assemblies
if (!settingsStore.materialSettings.globalAssemblies){
const assemblyList: AssemblyList = await firebaseStore.fetchAssemblyList(projectStore.currProject.id)
if (assemblyList) {
materialStore.assemblies = assemblyList.assemblies
}
} else {
const assemblyList: AssemblyList = await firebaseStore.fetchAssemblyList(true)
if (assemblyList) {
materialStore.assemblies = assemblyList.assemblies
}
}

} catch (error) {
console.error('Error fetching assembly list:', error)
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/resultUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export class ResultCalculator {
}
for (const phase in emission[impactCategory]) {
if (!this.totalEmission[impactCategory][phase]) {
this.totalEmission[impactCategory][phase] = { amount: 0 }
this.totalEmission[impactCategory][phase] = 0
}
const emissionAmount = emission[impactCategory][phase] || 0
const currentTotal = this.totalEmission[impactCategory][phase] || 0
Expand Down

0 comments on commit 2f8283f

Please sign in to comment.