ProjectsPage.vue
4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<script setup lang="ts">
import { ref, provide } from 'vue'
import { useLocalStorage } from '@vueuse/core'
import { useProjects } from './composables/useProjects'
import ProjectCards from './widgets/ProjectCards.vue'
import ProjectTable from './widgets/ProjectsTable.vue'
import EditProjectForm from './widgets/EditProjectForm.vue'
import { Project } from './types'
import { useModal, useToast } from 'vuestic-ui'
import { useProjectUsers } from './composables/useProjectUsers'
const doShowAsCards = useLocalStorage('projects-view', true)
const { projects, update, add, isLoading, remove, pagination, sorting } = useProjects()
const { users, getTeamOptions, getUserById } = useProjectUsers()
provide('ProjectsPage', {
users,
getTeamOptions,
getUserById,
})
const projectToEdit = ref<Project | null>(null)
const doShowProjectFormModal = ref(false)
const editProject = (project: Project) => {
projectToEdit.value = project
doShowProjectFormModal.value = true
}
const createNewProject = () => {
projectToEdit.value = null
doShowProjectFormModal.value = true
}
const { init: notify } = useToast()
const onProjectSaved = async (project: Project) => {
doShowProjectFormModal.value = false
if ('id' in project) {
await update(project as Project)
notify({
message: 'Project updated',
color: 'success',
})
} else {
await add(project as Project)
notify({
message: 'Project created',
color: 'success',
})
}
}
const { confirm } = useModal()
const onProjectDeleted = async (project: Project) => {
const response = await confirm({
title: 'Delete project',
message: `Are you sure you want to delete project "${project.project_name}"?`,
okText: 'Delete',
size: 'small',
maxWidth: '380px',
})
if (!response) {
return
}
await remove(project)
notify({
message: 'Project deleted',
color: 'success',
})
}
const editFormRef = ref()
const beforeEditFormModalClose = async (hide: () => unknown) => {
if (editFormRef.value.isFormHasUnsavedChanges) {
const agreed = await confirm({
maxWidth: '380px',
message: 'Form has unsaved changes. Are you sure you want to close it?',
size: 'small',
})
if (agreed) {
hide()
}
} else {
hide()
}
}
</script>
<template>
<h1 class="page-title">Projects</h1>
<VaCard>
<VaCardContent>
<div class="flex flex-col md:flex-row gap-2 mb-2 justify-between">
<div class="flex flex-col md:flex-row gap-2 justify-start">
<VaButtonToggle
v-model="doShowAsCards"
color="background-element"
border-color="background-element"
:options="[
{ label: 'Cards', value: true },
{ label: 'Table', value: false },
]"
/>
</div>
<VaButton icon="add" @click="createNewProject">Project</VaButton>
</div>
<ProjectCards
v-if="doShowAsCards"
:projects="projects"
:loading="isLoading"
@edit="editProject"
@delete="onProjectDeleted"
/>
<ProjectTable
v-else
v-model:sort-by="sorting.sortBy"
v-model:sorting-order="sorting.sortingOrder"
v-model:pagination="pagination"
:projects="projects"
:loading="isLoading"
@edit="editProject"
@delete="onProjectDeleted"
/>
</VaCardContent>
<VaModal
v-slot="{ cancel, ok }"
v-model="doShowProjectFormModal"
size="small"
mobile-fullscreen
close-button
stateful
hide-default-actions
:before-cancel="beforeEditFormModalClose"
>
<h1 v-if="projectToEdit === null" class="va-h5 mb-4">Add project</h1>
<h1 v-else class="va-h5 mb-4">Edit project</h1>
<EditProjectForm
ref="editFormRef"
:project="projectToEdit"
:save-button-label="projectToEdit === null ? 'Add' : 'Save'"
@close="cancel"
@save="
(project) => {
onProjectSaved(project)
ok()
}
"
/>
</VaModal>
</VaCard>
</template>