From ad4f5ee0f909533b67b232b31dd1e678990c101b Mon Sep 17 00:00:00 2001 From: Caroline Desprat Date: Tue, 26 Mar 2024 11:38:09 +0000 Subject: [PATCH] refactor: remove inline template + lint --- .../src/views/TaskRecordReviewList.vue | 371 +++++++++--------- 1 file changed, 195 insertions(+), 176 deletions(-) diff --git a/prophecies/apps/frontend/src/views/TaskRecordReviewList.vue b/prophecies/apps/frontend/src/views/TaskRecordReviewList.vue index 614f4ed0..e3ead555 100644 --- a/prophecies/apps/frontend/src/views/TaskRecordReviewList.vue +++ b/prophecies/apps/frontend/src/views/TaskRecordReviewList.vue @@ -22,7 +22,6 @@ import User from '@/models/User' export default { name: 'TaskRecordReviewList', - mixins: [taskRecordReviewFiltersMixin], components: { AppBreadcrumb, AppHeader, @@ -38,12 +37,13 @@ export default { TaskRecordReviewPageParams, TaskRecordReviewTutorial }, + mixins: [taskRecordReviewFiltersMixin], props: { taskId: { type: [Number, String] } }, - data () { + data() { return { countBy: [], pagination: null, @@ -53,100 +53,66 @@ export default { taskRecordReviewIds: [] } }, - watch: { - pageNumber (value) { - if (this.pagination && this.pagination.page !== value) { - return this.waitFor(this.fetchTaskRecordReviewsLoader, this.fetchTaskRecordReviews) - } - }, - pageSize () { - return this.waitFor(this.fetchTaskRecordReviewsLoader, this.fetchTaskRecordReviews) - }, - sort () { - return this.waitFor(this.fetchTaskRecordReviewsLoader, this.fetchTaskRecordReviews) - }, - routeFilters (value, previousValue) { - if (!isEqual(value, previousValue)) { - return this.waitFor(this.fetchTaskRecordReviewsLoader, this.fetchTaskRecordReviews) - } - }, - taskRecordReviewIds (value) { - // Maintain a clean list of selected ids - Object.entries(this.selectedIds).forEach(([id]) => { - // The task record review is not present on the page anymore... - // It needs to be deleted from the list of selected id! - if (!value.includes(id)) { - this.$delete(this.selectedIds, id) - } - }) - } - }, - created () { - this.$root.$on('prophecies::toggleCinematicView', () => { - this.showCinematicView = !this.showCinematicView - }) - return this.setup() - }, computed: { selectAll: { - set (checked) { + set(checked) { this.taskRecordReviews.forEach(({ id }) => { this.$set(this.selectedIds, id, checked) }) }, - get () { + get() { const selectedIds = values(this.selectedIds) return this.taskRecordReviews.length > 0 && compact(selectedIds).length === this.taskRecordReviews.length } }, - pageNumber () { + pageNumber() { return Number(this.$route.query['page[number]']) || 1 }, pageSize: { - get () { + get() { return String(this.$route.query['page[size]'] || '10') }, - set (pageSize) { + set(pageSize) { const query = { ...this.$route.query, 'page[size]': pageSize } this.$router.push({ path: this.$route.path, query }, noop) } }, sort: { - get () { + get() { return this.$route.query.sort || 'task_record__id' }, - set (sort) { + set(sort) { if (isEqual({ ...this.$route.query, sort }, this.$route.query)) { const query = { 'page[size]': this.pageSize, ...this.routeFiltersQueryParams } this.$router.push({ path: this.$route.path, query }, noop) } } }, - hasSorting () { + hasSorting() { return this.sort !== 'task_record__id' }, - hasFilters () { + hasFilters() { return !!keys(this.routeFilters).length }, - hasSelectedAndLockedRecords () { + hasSelectedAndLockedRecords() { return this.selectedAndLockedIdsCount > 0 }, - hasSelectedRecords () { + hasSelectedRecords() { return this.selectedIdsCount > 0 }, - showAppliedFilters () { + showAppliedFilters() { return !this.showFilters && (this.hasFilters || this.hasSorting) }, - isBulkSelectChoiceFormDisabled () { + isBulkSelectChoiceFormDisabled() { return !this.isTaskOpen || !this.hasSelectedRecords || this.$wait.is(this.bulkSelectChoiceLoader) }, - selectedIdsCount () { + selectedIdsCount() { return this.selectedIdsList.length }, - selectedAndLockedIdsCount () { + selectedAndLockedIdsCount() { return this.selectedAndLockedIdsList.length }, - selectedIdsList () { + selectedIdsList() { return Object.entries(this.selectedIds).reduce((list, [id, selected]) => { if (selected) { list.push(id) @@ -154,21 +120,21 @@ export default { return list }, []) }, - selectedAndLockedIdsList () { - return filter(this.selectedIdsList, id => { + selectedAndLockedIdsList() { + return filter(this.selectedIdsList, (id) => { return this.isTaskRecordReviewLocked(id) }) }, - selectedAndUnlockedIdsList () { - return filter(this.selectedIdsList, id => { + selectedAndUnlockedIdsList() { + return filter(this.selectedIdsList, (id) => { return !this.isTaskRecordReviewLocked(id) }) }, - filters () { + filters() { // Method from the mixins return this.getTaskFilters(this.task) }, - otherQueryParams () { + otherQueryParams() { return Object.entries(this.$route.query).reduce((all, [key, value]) => { if (!this.isFiltersParam(this.filters, { key })) { all[key] = value @@ -176,7 +142,7 @@ export default { return all }, {}) }, - routeFiltersQueryParams () { + routeFiltersQueryParams() { return Object.entries(this.$route.query).reduce((all, [key, value]) => { if (this.isFiltersParam(this.filters, { key })) { all[key] = value @@ -185,7 +151,7 @@ export default { }, {}) }, routeFilters: { - get () { + get() { return Object.entries(this.$route.query).reduce((all, [key, value]) => { if (this.isFiltersParam(this.filters, { key })) { all[key] = value @@ -193,7 +159,7 @@ export default { return all }, {}) }, - set (routeFiltersQueryParams) { + set(routeFiltersQueryParams) { if (!isEqual(routeFiltersQueryParams, this.routeFiltersQueryParams)) { const otherQueryParams = this.otherQueryParams const query = { ...otherQueryParams, ...routeFiltersQueryParams, 'page[number]': 1 } @@ -201,29 +167,24 @@ export default { } } }, - appliedRouteFiltersQueryParams () { + appliedRouteFiltersQueryParams() { return this.mapRouteFiltersToAppliedQueryParams(this.routeFilters, this.task) }, - task () { - return Task - .query() + task() { + return Task.query() .with('checkers') .with('choiceGroup') .with('choiceGroup.alternativeValues') .with('choiceGroup.choices') .find(this.taskId) }, - taskIsNotOpened () { + taskIsNotOpened() { return this.task.status !== TaskStatusEnum.OPEN }, - taskRecordReviews () { - return TaskRecordReview - .query() - .whereIdIn(this.taskRecordReviewIds) - .with('taskRecord') - .get() + taskRecordReviews() { + return TaskRecordReview.query().whereIdIn(this.taskRecordReviewIds).with('taskRecord').get() }, - taskRecordReviewsParams () { + taskRecordReviewsParams() { return { ...this.appliedRouteFiltersQueryParams, 'filter[task_record__task]': this.taskId, @@ -234,40 +195,86 @@ export default { sort: this.sort } }, - isTaskOpen () { + isTaskOpen() { return this.task && this.task.status === TaskStatusEnum.OPEN }, - trailingTaskRecordReview () { + trailingTaskRecordReview() { return this.taskRecordReviews[this.taskRecordReviews.length - 1] }, - fetchAllLoader () { + fetchAllLoader() { return uniqueId('load-all-task-record-review-') }, - fetchTaskRecordReviewsLoader () { + fetchTaskRecordReviewsLoader() { return uniqueId('load-task-record-review-') }, - bulkSelectChoiceLoader () { + bulkSelectChoiceLoader() { return uniqueId('bulk-select-choice-') }, - firstPendingTaskRecordReview () { + firstPendingTaskRecordReview() { return TaskRecordReview.query() .whereIdIn(this.taskRecordReviewIds) .where('status', 'PENDING') .where('locked', false) .first() }, - firstPendingTaskRecordReviewId () { + firstPendingTaskRecordReviewId() { return get(this, 'firstPendingTaskRecordReview.id') }, - filtersTogglerVariant () { + filtersTogglerVariant() { return this.showFilters ? 'primary' : 'outline-primary' }, - tipQueryParams () { + tipQueryParams() { return { 'filter[task]': this.task?.id, 'filter[project]': this.task?.projectId } + }, + selectedResults() { + return this.$tc('taskRecordReviewList.selectedResults', this.selectedIdsCount) + }, + lockedResults() { + let lockedResults = '' + if (this.taskIsNotOpened) { + lockedResults = `(${this.$tc('taskRecordReviewList.lockedResults', this.selectedIdsCount)})` + } else if (this.hasSelectedAndLockedRecords) { + lockedResults = `(${this.$tc('taskRecordReviewList.lockedResults', this.selectedAndLockedIdsCount)})` + } + return lockedResults } }, + watch: { + pageNumber(value) { + if (this.pagination && this.pagination.page !== value) { + return this.waitFor(this.fetchTaskRecordReviewsLoader, this.fetchTaskRecordReviews) + } + }, + pageSize() { + return this.waitFor(this.fetchTaskRecordReviewsLoader, this.fetchTaskRecordReviews) + }, + sort() { + return this.waitFor(this.fetchTaskRecordReviewsLoader, this.fetchTaskRecordReviews) + }, + routeFilters(value, previousValue) { + if (!isEqual(value, previousValue)) { + return this.waitFor(this.fetchTaskRecordReviewsLoader, this.fetchTaskRecordReviews) + } + }, + taskRecordReviewIds(value) { + // Maintain a clean list of selected ids + Object.entries(this.selectedIds).forEach(([id]) => { + // The task record review is not present on the page anymore... + // It needs to be deleted from the list of selected id! + if (!value.includes(id)) { + this.$delete(this.selectedIds, id) + } + }) + } + }, + created() { + this.$root.$on('prophecies::toggleCinematicView', () => { + this.showCinematicView = !this.showCinematicView + }) + return this.setup() + }, methods: { - async setup () { + async setup() { try { await this.waitFor(this.fetchAllLoader, this.fetchAll) this.$core.setPageTitle(this.task.name) @@ -276,54 +283,54 @@ export default { this.$router.replace({ name: 'error', params: { title, error } }) } }, - applyFilters (query) { + applyFilters(query) { return this.$router.push({ path: this.$route.path, query }, noop) }, - applyPageParams ({ pageSize, sort }) { + applyPageParams({ pageSize, sort }) { this.$bvModal.hide('modal-page-params') const query = { ...this.$route.query, 'page[size]': pageSize, sort } return this.$router.push({ path: this.$route.path, query }, noop) }, - clearFilters () { + clearFilters() { this.$set(this, 'routeFilters', {}) }, - async fetchTask () { + async fetchTask() { const params = { include: 'project,checkers,templateSetting' } return Task.api().find(this.taskId, { params }) }, - async fetchChoiceGroup () { + async fetchChoiceGroup() { const params = { include: 'alternative_values,choices' } return ChoiceGroup.api().find(this.task.choiceGroupId, { params }) }, - async fetchTaskRecordReviews () { + async fetchTaskRecordReviews() { const params = this.taskRecordReviewsParams const { response } = await TaskRecordReview.api().get('', { params }) const countBy = get(response, 'data.meta.countBy', null) const pagination = get(response, 'data.meta.pagination', null) - const taskRecordReviewIds = get(response, 'data.data', []).map(t => t.id) + const taskRecordReviewIds = get(response, 'data.data', []).map((t) => t.id) this.$set(this, 'countBy', countBy) this.$set(this, 'pagination', pagination) this.$set(this, 'taskRecordReviewIds', taskRecordReviewIds) }, - async goToPage (pageNumber) { + async goToPage(pageNumber) { const query = { ...this.$route.query, 'page[number]': pageNumber } await this.$router.push({ path: this.$route.path, query }, noop) }, - async goToNextPage () { + async goToNextPage() { const currentPage = parseInt(this.pageNumber) // Don't go to a page if it doesn't exist if (currentPage < parseInt(this.pagination.pages)) { return this.goToPage(currentPage + 1) } }, - async goToPreviousPage () { + async goToPreviousPage() { const currentPage = parseInt(this.pageNumber) // Don't go to a page if it doesn't exist if (currentPage > 1) { return this.goToPage(currentPage - 1) } }, - async scrollToActiveTaskRecordReviewCard ({ id }) { + async scrollToActiveTaskRecordReviewCard({ id }) { if (!this.firstPendingTaskRecordReview && this.isTrailingTaskRecordReview(id)) { return this.goToNextPage() } @@ -334,34 +341,34 @@ export default { element.scrollIntoView(options) } }, - isTaskRecordReviewActive (id) { + isTaskRecordReviewActive(id) { const isSelected = this.isTaskRecordReviewSelected(id) return !this.showCinematicView && !isSelected && this.firstPendingTaskRecordReviewId === id }, - isTrailingTaskRecordReview (id) { + isTrailingTaskRecordReview(id) { return this.trailingTaskRecordReview.id === id }, - isTaskRecordReviewSelected (id) { + isTaskRecordReviewSelected(id) { return !!this.selectedIds[id] }, - isTaskRecordReviewLocked (id) { + isTaskRecordReviewLocked(id) { const review = TaskRecordReview.query().with('taskRecord').find(id) // When task record is not found, we assume the review is not locked return get(review, 'taskRecord.locked', false) }, - isTaskRecordReviewFrozen (id) { + isTaskRecordReviewFrozen(id) { const isLoading = this.$wait.is(this.bulkSelectChoiceLoader) return isLoading && this.isTaskRecordReviewSelected(id) }, - selectTaskRecordReview (id, toggler) { + selectTaskRecordReview(id, toggler) { this.$set(this.selectedIds, id, toggler) }, - async bulkSelectChoiceWithLoader (data) { + async bulkSelectChoiceWithLoader(data) { this.$wait.start(this.bulkSelectChoiceLoader) await this.bulkSelectChoice(data) this.$wait.end(this.bulkSelectChoiceLoader) }, - async bulkSelectChoice (data) { + async bulkSelectChoice(data) { // Avoid trying to update locked reviews const ids = this.selectedAndUnlockedIdsList // We use a dedicated method that will format the data for the JSONAPI spec @@ -374,20 +381,20 @@ export default { // so we need to fetch them ourselves await this.fetchTaskRecordReviews() }, - async fetchAll () { + async fetchAll() { await this.fetchTask() await this.fetchChoiceGroup() await this.fetchTaskRecordReviews() }, - async waitFor (loader, fn) { + async waitFor(loader, fn) { this.$wait.start(loader) await fn() this.$wait.end(loader) }, - toggleFilters () { + toggleFilters() { this.showFilters = !this.showFilters }, - toggleSelectAll () { + toggleSelectAll() { this.selectAll = !this.selectAll } } @@ -396,68 +403,81 @@ export default {