feature/projects-all-the-way-down #3323
|
@ -57,19 +57,28 @@ export const useProjectStore = defineStore('project', () => {
|
|||
isLoading.value = newIsLoading
|
||||
}
|
||||
dpschen marked this conversation as resolved
Outdated
|
||||
|
||||
function setProject(project: IProject) {
|
||||
function setProject(project: IProject, updateChildren: boolean = true) {
|
||||
projects.value[project.id] = project
|
||||
update(project)
|
||||
|
||||
project.childProjects?.forEach(setProject)
|
||||
|
||||
if (baseStore.currentProject?.id === project.id) {
|
||||
baseStore.setCurrentProject(project)
|
||||
}
|
||||
|
||||
if (updateChildren) {
|
||||
project.childProjects?.forEach(p => setProject(p))
|
||||
}
|
||||
|
||||
// if the project is a child project, we need to also set it in the parent
|
||||
if (project.parentProjectId) {
|
||||
dpschen
commented
See comment about having the data as sub projects from earlier. See comment about having the data as sub projects from earlier.
|
||||
const parent = projects.value[project.parentProjectId]
|
||||
parent.childProjects = parent.childProjects?.map(p => p.id === project.id ? project : p)
|
||||
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.
|
||||
}
|
||||
}
|
||||
|
||||
function setProjects(newProjects: IProject[]) {
|
||||
newProjects.forEach(setProject)
|
||||
newProjects.forEach(p => setProject(p))
|
||||
}
|
||||
|
||||
function removeProjectById(project: IProject) {
|
||||
|
|
Remove added indention
Done.