feature/projects-all-the-way-down #3323
|
@ -66,39 +66,13 @@ export const useProjectStore = defineStore('project', () => {
|
|||
isLoading.value = newIsLoading
|
||||
}
|
||||
|
||||
function setProject(project: IProject, updateChildren: boolean = true) {
|
||||
function setProject(project: IProject) {
|
||||
projects.value[project.id] = project
|
||||
update(project)
|
||||
|
||||
if (baseStore.currentProject?.id === project.id) {
|
||||
|
||||
baseStore.setCurrentProject(project)
|
||||
}
|
||||
|
||||
if (updateChildren) {
|
||||
// When projects are loaded from the api, they will include child projects
|
||||
// in the `childProjects` property. We flatten them out into the project store here.
|
||||
project.childProjects?.forEach(p => setProject(new ProjectModel(p)))
|
||||
delete project.childProjects
|
||||
}
|
||||
|
||||
// if the project is a child project, we need to also set it in the parent
|
||||
if (project.parentProjectId) {
|
||||
const parent = projects.value[project.parentProjectId]
|
||||
let foundProject = false
|
||||
parent.childProjectIds = parent.childProjectIds?.forEach(p => {
|
||||
if (p.id === project.id) {
|
||||
foundProject = true
|
||||
}
|
||||
})
|
||||
// If we hit this, the project now has a new parent project which it did not have before
|
||||
if (!foundProject) {
|
||||
if (!parent.childProjectIds) {
|
||||
parent.childProjectIds = []
|
||||
}
|
||||
parent.childProjectIds.push(project.id)
|
||||
}
|
||||
setProject(parent, false)
|
||||
}
|
||||
}
|
||||
dpschen
commented
The three lines above should be called via a deep watcher on the current project. Not as a side effect. The three lines above should be called via a deep watcher on the current project. Not as a side effect.
konrad
commented
You mean setting the current project in the base store? You mean setting the current project in the base store?
dpschen
commented
I mean:
It might be even better to make the currentProject a computed based on the store instead and only setting it via id (unsure here how to handle projects that are not saved yet) I mean:
1) remove this sideeffect of the `setProject` function:
```
if (baseStore.currentProject?.id === project.id) {
baseStore.setCurrentProject(project)
}
```
2) Instead add a watcher on the project that has the id of the current project.
```ts
watchEffect(() => baseStore.setCurrentProject(projects.value[baseStore.currentProject.id])
)
```
It might be even better to make the currentProject a computed based on the store instead and only setting it via id (unsure here how to handle projects that are not saved yet)
konrad
commented
I've now changed it to use a watcher.
That sounds like a good idea, but let's push that to another PR. I've now changed it to use a watcher.
> It might be even better to make the currentProject a computed based on the store instead and only setting it via id (unsure here how to handle projects that are not saved yet)
That sounds like a good idea, but let's push that to another PR.
konrad
commented
I've now changed it to use a watcher.
That sounds like a good idea, but let's push that to another PR. I've now changed it to use a watcher.
> It might be even better to make the currentProject a computed based on the store instead and only setting it via id (unsure here how to handle projects that are not saved yet)
That sounds like a good idea, but let's push that to another PR.
|
||||
|
||||
dpschen
commented
If I set an id for If I set an id for `parentID` in the passed project: will this update the parents childProjects?
konrad
commented
It did not. While checking this, I discovered how we're not using child ids anywhere any more (including in the api) so I've removed all traces of them. It did not. While checking this, I discovered how we're not using child ids anywhere any more (including in the api) so I've removed all traces of them.
|
||||
function setProjects(newProjects: IProject[]) {
|
||||
|
|
See comment about having the data as sub projects from earlier.