From 5640f43b9df0b39d2768dee6e8a540beebf40caf Mon Sep 17 00:00:00 2001 From: Julien Nahum Date: Tue, 19 Dec 2023 18:57:31 +0100 Subject: [PATCH] Reworked workspaces store --- .../components/global/WorkspaceDropdown.vue | 11 +- .../pages/auth/components/LoginForm.vue | 10 +- .../pages/welcome/TemplatesSlider.vue | 32 ++-- client/composables/stores/useContentStore.js | 8 +- client/middleware/01.check-auth.global.js | 7 +- client/pages/home.vue | 168 +++++++----------- client/pages/index.vue | 2 +- client/pages/templates/index.vue | 3 +- client/stores/templates.js | 15 +- client/stores/workspaces.js | 125 +++++-------- package-lock.json | 6 - resources/js/components/Modal.vue | 4 +- resources/js/components/Navbar.vue | 4 +- .../open/forms/OpenCompleteForm.vue | 10 +- resources/js/middleware/check-auth.js | 4 +- resources/js/pages/home.vue | 81 ++++----- resources/js/stores/forms.js | 2 +- 17 files changed, 210 insertions(+), 282 deletions(-) diff --git a/client/components/global/WorkspaceDropdown.vue b/client/components/global/WorkspaceDropdown.vue index bd940413..f314f14f 100644 --- a/client/components/global/WorkspaceDropdown.vue +++ b/client/components/global/WorkspaceDropdown.vue @@ -42,9 +42,6 @@ diff --git a/client/pages/index.vue b/client/pages/index.vue index d68d0dd3..5337f656 100644 --- a/client/pages/index.vue +++ b/client/pages/index.vue @@ -126,7 +126,7 @@ - +

diff --git a/client/pages/templates/index.vue b/client/pages/templates/index.vue index 022f5cfd..d3b8ff16 100644 --- a/client/pages/templates/index.vue +++ b/client/pages/templates/index.vue @@ -13,7 +13,7 @@

- + @@ -30,5 +30,6 @@ import {loadAllTemplates} from "~/stores/templates.js"; const templatesStore = useTemplatesStore() loadAllTemplates(templatesStore) +const loading = computed(() => templatesStore.loading) const templates = computed(() => templatesStore.getAll) diff --git a/client/stores/templates.js b/client/stores/templates.js index c1b78bad..bba8d551 100644 --- a/client/stores/templates.js +++ b/client/stores/templates.js @@ -36,26 +36,29 @@ export const useTemplatesStore = defineStore('templates', () => { ...contentStore, industries, types, + allLoaded, getTemplateTypes, getTemplateIndustries, initTypesAndIndustries } }) -export const fetchTemplate = (slug) => { - return useOpnApi(templatesEndpoint + '/' + slug) +export const fetchTemplate = (slug, options = {}) => { + return useOpnApi(templatesEndpoint + '/' + slug, options) } -export const fetchAllTemplates = () => { - return useOpnApi(templatesEndpoint) +export const fetchAllTemplates = (options = {}) => { + return useOpnApi(templatesEndpoint, options) } -export const loadAllTemplates = async (store) => { +export const loadAllTemplates = async (store, options={}) => { + console.log('in------',store, store.allLoaded) if (!store.allLoaded) { store.startLoading() store.initTypesAndIndustries() - const {data} = await fetchAllTemplates() + const {data} = await fetchAllTemplates(options) store.set(data.value) + store.stopLoading() store.allLoaded = true } } diff --git a/client/stores/workspaces.js b/client/stores/workspaces.js index 29cc48c1..9c474f80 100644 --- a/client/stores/workspaces.js +++ b/client/stores/workspaces.js @@ -1,93 +1,50 @@ import {defineStore} from 'pinia' import {useStorage} from "@vueuse/core" +import {useContentStore} from "~/composables/stores/useContentStore.js"; export const workspaceEndpoint = 'open/workspaces/' -const storedWorkspaceId = useStorage('currentWorkspace', 0) +export const useWorkspacesStore = defineStore('workspaces', () => { -export const useWorkspacesStore = defineStore('workspaces', { - state: () => ({ - content: new Map, - currentId: null, - loading: false - }), - getters: { - getById: (state) => (id) => { - if (state.content.length === 0) return null - return state.content.find(item => item.id === id) - }, - getCurrent (){ - if (this.content.length === 0 || this.currentId === null) return null - return this.content.find(item => item.id === this.currentId) - } - }, - actions: { - set(items) { - this.content = items - if (this.currentId == null && this.content.length > 0) { - // If one only, set it - if (this.content.length === 1) { - this.setCurrentId(items[0].id) - } else if (storedWorkspaceId && this.content.find(item => item.id === parseInt(storedWorkspaceId.value))) { - // Check local storage for current workspace, or take first - this.setCurrentId(parseInt(storedWorkspaceId.value)) - } else { - // Else, take first - this.setCurrentId(items[0].id) - } - } else { - this.setCurrentId(null) - } - }, - setCurrentId(id) { - this.currentId = id - storedWorkspaceId.value = id - }, - addOrUpdate(item) { - this.content = this.content.filter((val) => val.id !== item.id) - this.content.push(item) - if (this.currentId == null) { - this.currentId = item.id - storedWorkspaceId.value = this.currentId - } - }, - remove(itemId) { - this.content = this.content.filter((val) => val.id !== itemId) - if (this.currentId === itemId) { - this.setCurrentId(this.content.length > 0 ? this.content[0].id : null) - } - }, - startLoading() { - this.loading = true - }, - stopLoading() { - this.loading = false - }, - resetState() { - this.set([]) - this.stopLoading() - }, - load() { - this.set([]) - this.startLoading() - console.log('loaindgworkspaces') - return useOpnApi(workspaceEndpoint).then(({data, error}) => { - this.set(data.value) - this.stopLoading() - }) - }, - loadIfEmpty() { - if (this.content.length === 0) { - return this.load() - } - return Promise.resolve() - }, - delete(id) { - this.startLoading() - return useOpnApi(workspaceEndpoint + id, {method: 'DELETE'}).then(({data}) => { - this.remove(data.value.workspace_id) - this.stopLoading() - }) + const storedWorkspaceId = useCookie('currentWorkspace') + + const contentStore = useContentStore() + const currentId = ref(storedWorkspaceId) + + const getCurrent = computed(() => { + return contentStore.getByKey(currentId.value) + }) + + const setCurrentId = (id) => { + currentId.value = id + storedWorkspaceId.value = id + } + + const save = (items) => { + contentStore.save(items) + console.log('cookie issi', currentId.value, contentStore.length.value) + if (currentId.value == null && contentStore.length.value) { + setCurrentId(items[0].id) } } + + const remove = (itemId) => { + contentStore.remove(itemId) + if (currentId.value === itemId) { + setCurrentId(contentStore.length.value > 0 ? contentStore.getAll[0].id : null) + } + } + + return { + ...contentStore, + currentId, + getCurrent, + setCurrentId, + save, + remove + } }) + +export const fetchAllWorkspaces = (options = {}) => { + return useOpnApi(workspaceEndpoint, options) +} diff --git a/package-lock.json b/package-lock.json index 85eea006..c483ee6c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,7 +20,6 @@ "date-fns": "^2.28.0", "debounce": "^1.2.1", "fuse.js": "^6.4.6", - "js-cookie": "^2.2.1", "js-sha256": "^0.9.0", "libphonenumber-js": "^1.10.44", "pinia": "^2.1.7", @@ -9283,11 +9282,6 @@ "node": ">=6" } }, - "node_modules/js-cookie": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-2.2.1.tgz", - "integrity": "sha512-HvdH2LzI/EAZcUwA8+0nKNtWHqS+ZmijLA30RwZA0bo7ToCckjK5MkGhjED9KoRcXO6BaGI3I9UIzSA1FKFPOQ==" - }, "node_modules/js-sha256": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.9.0.tgz", diff --git a/resources/js/components/Modal.vue b/resources/js/components/Modal.vue index 24f32286..652bf796 100644 --- a/resources/js/components/Modal.vue +++ b/resources/js/components/Modal.vue @@ -20,7 +20,7 @@
-
+
@@ -28,7 +28,7 @@
-

diff --git a/resources/js/components/Navbar.vue b/resources/js/components/Navbar.vue index 526a35e0..8268f2fb 100644 --- a/resources/js/components/Navbar.vue +++ b/resources/js/components/Navbar.vue @@ -71,7 +71,7 @@ My Forms - @@ -80,7 +80,7 @@ My Templates - { authStore.fetchUser().then((user) => { initCrisp(user) initSentry(user) + useWorkspacesStore().fetchWorkspaces() }) } catch (e) { console.error(e) diff --git a/resources/js/pages/home.vue b/resources/js/pages/home.vue index 2779761c..8ce68218 100644 --- a/resources/js/pages/home.vue +++ b/resources/js/pages/home.vue @@ -74,12 +74,14 @@
- + Draft - + Closed import { computed } from 'vue' -import { useAuthStore } from '../stores/auth'; -import { useFormsStore } from '../stores/forms'; -import { useWorkspacesStore } from '../stores/workspaces'; +import { useAuthStore } from '../stores/auth' +import { useFormsStore } from '../stores/forms' +import { useWorkspacesStore } from '../stores/workspaces' import Fuse from 'fuse.js' import Form from 'vform' import TextInput from '../components/forms/TextInput.vue' import OpenFormFooter from '../components/pages/OpenFormFooter.vue' -import ExtraMenu from '../components/pages/forms/show/ExtraMenu.vue' - -const loadForms = function () { - const formsStore = useFormsStore() - const workspacesStore = useWorkspacesStore() - formsStore.startLoading() - workspacesStore.loadIfEmpty().then(() => { - formsStore.loadIfEmpty(workspacesStore.currentId) - }) -} +import ExtraMenu from '../components/pages/forms/show/ExtraMenu.vue export default { components: { OpenFormFooter, TextInput, ExtraMenu }, - beforeRouteEnter (to, from, next) { - loadForms() - next() - }, middleware: 'auth', props: { @@ -141,12 +130,16 @@ export default { const authStore = useAuthStore() const formsStore = useFormsStore() const workspacesStore = useWorkspacesStore() + + formsStore.startLoading() + formsStore.loadIfEmpty(workspacesStore.currentId) + return { formsStore, workspacesStore, - user : computed(() => authStore.user), - forms : computed(() => formsStore.content), - formsLoading : computed(() => formsStore.loading) + user: computed(() => authStore.user), + forms: computed(() => formsStore.content), + formsLoading: computed(() => formsStore.loading) } }, @@ -161,26 +154,6 @@ export default { } }, - mounted () {}, - - methods: { - editForm (form) { - this.selectedForm = form - this.showEditFormModal = true - }, - onTagClick (tag) { - const idx = this.selectedTags.indexOf(tag) - if (idx === -1) { - this.selectedTags.push(tag) - } else { - this.selectedTags.splice(idx, 1) - } - }, - viewForm (form) { - this.$router.push({ name: 'forms.show', params: { slug: form.slug } }) - } - }, - computed: { isFilteringForms () { return (this.searchForm.search !== '' && this.searchForm.search !== null) || this.selectedTags.length > 0 @@ -218,6 +191,26 @@ export default { allTags () { return this.formsStore.getAllTags } + }, + + mounted () {}, + + methods: { + editForm (form) { + this.selectedForm = form + this.showEditFormModal = true + }, + onTagClick (tag) { + const idx = this.selectedTags.indexOf(tag) + if (idx === -1) { + this.selectedTags.push(tag) + } else { + this.selectedTags.splice(idx, 1) + } + }, + viewForm (form) { + this.$router.push({ name: 'forms.show', params: { slug: form.slug } }) + } } } diff --git a/resources/js/stores/forms.js b/resources/js/stores/forms.js index 62ec8918..391e6c70 100644 --- a/resources/js/stores/forms.js +++ b/resources/js/stores/forms.js @@ -80,4 +80,4 @@ export const useFormsStore = defineStore('forms', { return Promise.resolve() } } -}) \ No newline at end of file +})