From d8ca1a2de1f8474d13ffcdcaed12b3825ea9f4ca Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 14 Apr 2024 17:12:16 +0200 Subject: [PATCH] fix(favorites): make favorites work with configurable views --- frontend/src/helpers/projectView.ts | 1 - pkg/models/project.go | 35 ++++++++++++++++++++++++++--- pkg/models/project_view.go | 18 ++++++++++++--- pkg/models/task_collection.go | 4 ++++ 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/frontend/src/helpers/projectView.ts b/frontend/src/helpers/projectView.ts index 2e40f9efe..2c8cfb763 100644 --- a/frontend/src/helpers/projectView.ts +++ b/frontend/src/helpers/projectView.ts @@ -8,7 +8,6 @@ const SETTINGS_KEY_PROJECT_VIEW = 'projectView' * Save the current project view to local storage */ export function saveProjectView(projectId: IProject['id'], viewId: number) { - console.log({projectId, viewId}) if (!projectId || !viewId) { return } diff --git a/pkg/models/project.go b/pkg/models/project.go index 066e9927c..0d3450dce 100644 --- a/pkg/models/project.go +++ b/pkg/models/project.go @@ -112,15 +112,43 @@ type ProjectBackgroundType struct { // ProjectBackgroundUpload represents the project upload background type const ProjectBackgroundUpload string = "upload" +const FavoritesPseudoProjectID = -1 + // FavoritesPseudoProject holds all tasks marked as favorites var FavoritesPseudoProject = Project{ - ID: -1, + ID: FavoritesPseudoProjectID, Title: "Favorites", Description: "This project has all tasks marked as favorites.", IsFavorite: true, Position: -1, - Created: time.Now(), - Updated: time.Now(), + + Views: []*ProjectView{ + { + ID: -1, + ProjectID: FavoritesPseudoProjectID, + Title: "List", + ViewKind: ProjectViewKindList, + Position: 100, + Filter: "done = false", + }, + { + ID: -2, + ProjectID: FavoritesPseudoProjectID, + Title: "Gantt", + ViewKind: ProjectViewKindGantt, + Position: 200, + }, + { + ID: -3, + ProjectID: FavoritesPseudoProjectID, + Title: "Table", + ViewKind: ProjectViewKindTable, + Position: 300, + }, + }, + + Created: time.Now(), + Updated: time.Now(), } // ReadAll gets all projects a user has access to @@ -212,6 +240,7 @@ func (p *Project) ReadAll(s *xorm.Session, a web.Auth, search string, page int, func (p *Project) ReadOne(s *xorm.Session, a web.Auth) (err error) { if p.ID == FavoritesPseudoProject.ID { + p.Views = FavoritesPseudoProject.Views // Already "built" the project in CanRead return nil } diff --git a/pkg/models/project_view.go b/pkg/models/project_view.go index 66cc98f48..b2b3781d9 100644 --- a/pkg/models/project_view.go +++ b/pkg/models/project_view.go @@ -347,10 +347,22 @@ func (p *ProjectView) Update(s *xorm.Session, _ web.Auth) (err error) { return } -func GetProjectViewByIDAndProject(s *xorm.Session, id, projectID int64) (view *ProjectView, err error) { +func GetProjectViewByIDAndProject(s *xorm.Session, viewID, projectID int64) (view *ProjectView, err error) { + if projectID == FavoritesPseudoProjectID && viewID < 0 { + for _, v := range FavoritesPseudoProject.Views { + if v.ID == viewID { + return v, nil + } + } + + return nil, &ErrProjectViewDoesNotExist{ + ProjectViewID: viewID, + } + } + view = &ProjectView{} exists, err := s. - Where("id = ? AND project_id = ?", id, projectID). + Where("id = ? AND project_id = ?", viewID, projectID). NoAutoCondition(). Get(view) if err != nil { @@ -359,7 +371,7 @@ func GetProjectViewByIDAndProject(s *xorm.Session, id, projectID int64) (view *P if !exists { return nil, &ErrProjectViewDoesNotExist{ - ProjectViewID: id, + ProjectViewID: viewID, } } diff --git a/pkg/models/task_collection.go b/pkg/models/task_collection.go index 7f55e4486..e00b5f1bd 100644 --- a/pkg/models/task_collection.go +++ b/pkg/models/task_collection.go @@ -100,6 +100,10 @@ func getTaskFilterOptsFromCollection(tf *TaskCollection, projectView *ProjectVie param.orderBy = getSortOrderFromString(tf.OrderBy[i]) } + if s == taskPropertyPosition && projectView != nil && projectView.ID < 0 { + continue + } + if s == taskPropertyPosition && projectView != nil { param.projectViewID = projectView.ID }