2023-06-28 12:38:10 +00:00
|
|
|
import {ref, shallowReactive, watch, computed, type ComputedGetter} from 'vue'
|
2021-12-10 14:29:28 +00:00
|
|
|
import {useRoute} from 'vue-router'
|
2023-09-06 08:50:52 +00:00
|
|
|
import {useRouteQuery} from '@vueuse/router'
|
2021-10-25 20:17:23 +00:00
|
|
|
|
|
|
|
import TaskCollectionService from '@/services/taskCollection'
|
2022-10-18 14:26:35 +00:00
|
|
|
import type {ITask} from '@/modelTypes/ITask'
|
2022-11-10 15:44:16 +00:00
|
|
|
import {error} from '@/message'
|
2023-06-20 13:24:02 +00:00
|
|
|
import type {IProject} from '@/modelTypes/IProject'
|
2021-10-25 20:17:23 +00:00
|
|
|
|
2023-02-28 10:56:05 +00:00
|
|
|
export type Order = 'asc' | 'desc' | 'none'
|
|
|
|
|
|
|
|
export interface SortBy {
|
|
|
|
id?: Order
|
|
|
|
index?: Order
|
|
|
|
done?: Order
|
|
|
|
title?: Order
|
|
|
|
priority?: Order
|
|
|
|
due_date?: Order
|
|
|
|
start_date?: Order
|
|
|
|
end_date?: Order
|
|
|
|
percent_done?: Order
|
|
|
|
created?: Order
|
|
|
|
updated?: Order
|
|
|
|
}
|
|
|
|
|
2021-10-25 20:17:23 +00:00
|
|
|
// FIXME: merge with DEFAULT_PARAMS in filters.vue
|
|
|
|
export const getDefaultParams = () => ({
|
|
|
|
sort_by: ['position', 'id'],
|
|
|
|
order_by: ['asc', 'desc'],
|
|
|
|
filter_by: ['done'],
|
|
|
|
filter_value: ['false'],
|
|
|
|
filter_comparator: ['equals'],
|
|
|
|
filter_concat: 'and',
|
|
|
|
})
|
|
|
|
|
2023-02-28 10:56:05 +00:00
|
|
|
const SORT_BY_DEFAULT: SortBy = {
|
2021-12-10 14:29:28 +00:00
|
|
|
id: 'desc',
|
|
|
|
}
|
|
|
|
|
|
|
|
// This makes sure an id sort order is always sorted last.
|
|
|
|
// When tasks would be sorted first by id and then by whatever else was specified, the id sort takes
|
|
|
|
// precedence over everything else, making any other sort columns pretty useless.
|
2022-10-27 13:47:03 +00:00
|
|
|
function formatSortOrder(sortBy, params) {
|
2021-12-10 14:29:28 +00:00
|
|
|
let hasIdFilter = false
|
2022-10-27 13:47:03 +00:00
|
|
|
const sortKeys = Object.keys(sortBy)
|
2021-12-10 14:29:28 +00:00
|
|
|
for (const s of sortKeys) {
|
|
|
|
if (s === 'id') {
|
|
|
|
sortKeys.splice(s, 1)
|
|
|
|
hasIdFilter = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasIdFilter) {
|
|
|
|
sortKeys.push('id')
|
2021-10-25 20:17:23 +00:00
|
|
|
}
|
2021-12-10 14:29:28 +00:00
|
|
|
params.sort_by = sortKeys
|
2022-10-27 13:47:03 +00:00
|
|
|
params.order_by = sortKeys.map(s => sortBy[s])
|
2021-10-25 20:17:23 +00:00
|
|
|
|
2021-12-10 14:29:28 +00:00
|
|
|
return params
|
|
|
|
}
|
2021-10-25 20:17:23 +00:00
|
|
|
|
2022-10-27 13:47:03 +00:00
|
|
|
/**
|
2022-11-13 21:04:57 +00:00
|
|
|
* This mixin provides a base set of methods and properties to get tasks.
|
2022-10-27 13:47:03 +00:00
|
|
|
*/
|
2023-06-28 12:38:10 +00:00
|
|
|
export function useTaskList(projectIdGetter: ComputedGetter<IProject['id']>, sortByDefault: SortBy = SORT_BY_DEFAULT) {
|
|
|
|
|
|
|
|
const projectId = computed(() => projectIdGetter())
|
|
|
|
|
2022-10-27 13:47:03 +00:00
|
|
|
const params = ref({...getDefaultParams()})
|
|
|
|
|
|
|
|
const search = ref('')
|
2023-09-06 08:50:52 +00:00
|
|
|
const page = useRouteQuery('page', '1', { transform: Number })
|
2022-10-27 13:47:03 +00:00
|
|
|
|
|
|
|
const sortBy = ref({ ...sortByDefault })
|
2023-09-06 08:50:52 +00:00
|
|
|
|
|
|
|
const allParams = computed(() => {
|
|
|
|
const loadParams = {...params.value}
|
2021-12-10 14:29:28 +00:00
|
|
|
|
|
|
|
if (search.value !== '') {
|
|
|
|
loadParams.s = search.value
|
2021-10-25 20:17:23 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 08:50:52 +00:00
|
|
|
return formatSortOrder(sortBy.value, loadParams)
|
|
|
|
})
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => allParams.value,
|
|
|
|
() => {
|
|
|
|
// When parameters change, the page should always be the first
|
|
|
|
page.value = 1
|
|
|
|
},
|
|
|
|
)
|
2021-12-10 14:29:28 +00:00
|
|
|
|
2023-09-06 08:50:52 +00:00
|
|
|
const getAllTasksParams = computed(() => {
|
2021-12-10 14:29:28 +00:00
|
|
|
return [
|
2023-06-28 12:38:10 +00:00
|
|
|
{projectId: projectId.value},
|
2023-09-06 08:50:52 +00:00
|
|
|
allParams.value,
|
|
|
|
page.value,
|
2021-12-10 14:29:28 +00:00
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
const taskCollectionService = shallowReactive(new TaskCollectionService())
|
|
|
|
const loading = computed(() => taskCollectionService.loading)
|
|
|
|
const totalPages = computed(() => taskCollectionService.totalPages)
|
2021-11-01 17:19:59 +00:00
|
|
|
|
2022-09-28 16:08:23 +00:00
|
|
|
const tasks = ref<ITask[]>([])
|
2021-12-10 14:29:28 +00:00
|
|
|
async function loadTasks() {
|
2022-04-03 12:25:29 +00:00
|
|
|
tasks.value = []
|
2022-11-10 15:44:16 +00:00
|
|
|
try {
|
|
|
|
tasks.value = await taskCollectionService.getAll(...getAllTasksParams.value)
|
|
|
|
} catch (e) {
|
|
|
|
error(e)
|
|
|
|
}
|
2021-11-01 17:19:59 +00:00
|
|
|
return tasks.value
|
2021-10-25 20:17:23 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 14:29:28 +00:00
|
|
|
const route = useRoute()
|
|
|
|
watch(() => route.query, (query) => {
|
2021-12-11 13:51:39 +00:00
|
|
|
const { page: pageQueryValue, search: searchQuery } = query
|
|
|
|
if (searchQuery !== undefined) {
|
2022-09-28 16:08:23 +00:00
|
|
|
search.value = searchQuery as string
|
2021-12-11 13:51:39 +00:00
|
|
|
}
|
|
|
|
if (pageQueryValue !== undefined) {
|
2022-09-28 16:08:23 +00:00
|
|
|
page.value = Number(pageQueryValue)
|
2021-12-11 13:51:39 +00:00
|
|
|
}
|
2021-12-10 14:29:28 +00:00
|
|
|
|
|
|
|
}, { immediate: true })
|
|
|
|
|
|
|
|
|
|
|
|
// Only listen for query path changes
|
|
|
|
watch(() => JSON.stringify(getAllTasksParams.value), (newParams, oldParams) => {
|
|
|
|
if (oldParams === newParams) {
|
|
|
|
return
|
2021-10-25 20:17:23 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 14:29:28 +00:00
|
|
|
loadTasks()
|
|
|
|
}, { immediate: true })
|
2021-10-25 20:17:23 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
tasks,
|
|
|
|
loading,
|
|
|
|
totalPages,
|
2021-12-10 14:29:28 +00:00
|
|
|
currentPage: page,
|
2021-10-25 20:17:23 +00:00
|
|
|
loadTasks,
|
2021-12-10 14:29:28 +00:00
|
|
|
searchTerm: search,
|
2021-10-25 20:17:23 +00:00
|
|
|
params,
|
2022-07-13 14:19:58 +00:00
|
|
|
sortByParam: sortBy,
|
2021-10-25 20:17:23 +00:00
|
|
|
}
|
|
|
|
}
|