Skip to content

Commit

Permalink
feat: implement file filtering logic WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Bianchi <vbianchi@protonmail.com>
  • Loading branch information
v-bianchi committed Sep 26, 2024
1 parent efd7736 commit a7567ab
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 164 deletions.
344 changes: 181 additions & 163 deletions src/store/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { defineStore } from 'pinia'

Check failure on line 6 in src/store/files.js

View workflow job for this annotation

GitHub Actions / NPM lint

There should be at least one empty line between import groups
import { subscribe } from '@nextcloud/event-bus'

Check failure on line 7 in src/store/files.js

View workflow job for this annotation

GitHub Actions / NPM lint

There should be at least one empty line between import groups
import { set } from 'vue'

Check failure on line 8 in src/store/files.js

View workflow job for this annotation

GitHub Actions / NPM lint

`vue` import should occur before import of `@nextcloud/event-bus`

import axios from '@nextcloud/axios'

Check failure on line 10 in src/store/files.js

View workflow job for this annotation

GitHub Actions / NPM lint

`@nextcloud/axios` import should occur before import of `@nextcloud/event-bus`
Expand All @@ -12,174 +13,191 @@ import { generateOcsUrl } from '@nextcloud/router'

import { useSidebarStore } from './sidebar.js'
import { useSignStore } from './sign.js'
import { useFiltersStore } from './filters.js'

Check failure on line 16 in src/store/files.js

View workflow job for this annotation

GitHub Actions / NPM lint

`./filters.js` import should occur before import of `./sidebar.js`
import { useFilesSortingStore } from './filesSorting.js'

Check failure on line 17 in src/store/files.js

View workflow job for this annotation

GitHub Actions / NPM lint

`./filesSorting.js` import should occur before import of `./sidebar.js`

export const useFilesStore = defineStore('files', {
state: () => {
return {
files: {},
selectedNodeId: 0,
identifyingSigner: false,
loading: false,
filterActive: 'all',
}
},

actions: {
addFile(file) {
set(this.files, file.nodeId, file)
this.hydrateFile(file.nodeId)
},
selectFile(nodeId) {
this.selectedNodeId = nodeId ?? 0
if (this.selectedNodeId === 0) {
const signStore = useSignStore()
signStore.reset()
return
}
const sidebarStore = useSidebarStore()
sidebarStore.activeRequestSignatureTab()
},
getFile() {
return this.files[this.selectedNodeId] ?? {}
},
async flushSelectedFile() {
const files = await this.getAllFiles({
nodeId: this.selectedNodeId,
})
this.addFile(files[this.selectedNodeId])
},
enableIdentifySigner() {
this.identifyingSigner = true
},
disableIdentifySigner() {
this.identifyingSigner = false
},
hasSigners() {
if (this.selectedNodeId === 0) {
return false
}
if (!Object.hasOwn(this.getFile(), 'signers')) {
return false
}
return this.files[this.selectedNodeId].signers.length > 0
},
isPartialSigned() {
if (this.selectedNodeId === 0) {
return false
}
if (!Object.hasOwn(this.getFile(), 'signers')) {
return false
}
return this.files[this.selectedNodeId].signers
.filter(signer => signer.signed?.length > 0).length > 0
},
isFullSigned() {
if (this.selectedNodeId === 0) {
return false
export const useFilesStore = function(...args) {
const store = defineStore('files', {
state: () => {
return {
files: {},
selectedNodeId: 0,
identifyingSigner: false,
loading: false,
filterActive: 'all',
}
if (!Object.hasOwn(this.getFile(), 'signers')) {
return false
}
return this.files[this.selectedNodeId].signers.length > 0
&& this.files[this.selectedNodeId].signers
.filter(signer => signer.signed?.length > 0).length === this.files[this.selectedNodeId].signers.length
},
getSubtitle() {
if (this.selectedNodeId === 0) {
return ''
}
const file = this.files[this.selectedNodeId]
if ((file?.requested_by?.userId ?? '').length === 0 || file?.request_date.length === 0) {
return ''
}
return t('libresign', 'Requested by {name}, at {date}', {
name: file.requested_by.userId,
date: Moment(Date.parse(file.request_date)).format('LL LTS'),
})
},
async hydrateFile(nodeId) {
if (Object.hasOwn(this.files[nodeId], 'uuid')) {
return
}
await axios.get(generateOcsUrl('/apps/libresign/api/v1/file/validate/file_id/{fileId}', {
fileId: nodeId,
}))
.then((response) => {
set(this.files, nodeId, response.data.ocs.data)
this.addUniqueIdentifierToAllSigners(this.files[nodeId].signers)

actions: {
addFile(file) {
set(this.files, file.nodeId, file)
this.hydrateFile(file.nodeId)
},
selectFile(nodeId) {
this.selectedNodeId = nodeId ?? 0
if (this.selectedNodeId === 0) {
const signStore = useSignStore()
signStore.reset()
return
}
const sidebarStore = useSidebarStore()
sidebarStore.activeRequestSignatureTab()
},
getFile() {
return this.files[this.selectedNodeId] ?? {}
},
async flushSelectedFile() {
const files = await this.getAllFiles({
nodeId: this.selectedNodeId,
})
.catch(() => {
set(this.files[nodeId], 'signers', [])
this.addFile(files[this.selectedNodeId])
},
enableIdentifySigner() {
this.identifyingSigner = true
},
disableIdentifySigner() {
this.identifyingSigner = false
},
hasSigners() {
if (this.selectedNodeId === 0) {
return false
}
if (!Object.hasOwn(this.getFile(), 'signers')) {
return false
}
return this.files[this.selectedNodeId].signers.length > 0
},
isPartialSigned() {
if (this.selectedNodeId === 0) {
return false
}
if (!Object.hasOwn(this.getFile(), 'signers')) {
return false
}
return this.files[this.selectedNodeId].signers
.filter(signer => signer.signed?.length > 0).length > 0
},
isFullSigned() {
if (this.selectedNodeId === 0) {
return false
}
if (!Object.hasOwn(this.getFile(), 'signers')) {
return false
}
return this.files[this.selectedNodeId].signers.length > 0
&& this.files[this.selectedNodeId].signers
.filter(signer => signer.signed?.length > 0).length === this.files[this.selectedNodeId].signers.length
},
getSubtitle() {
if (this.selectedNodeId === 0) {
return ''
}
const file = this.files[this.selectedNodeId]
if ((file?.requested_by?.userId ?? '').length === 0 || file?.request_date.length === 0) {
return ''
}
return t('libresign', 'Requested by {name}, at {date}', {
name: file.requested_by.userId,
date: Moment(Date.parse(file.request_date)).format('LL LTS'),
})
},
addUniqueIdentifierToAllSigners(signers) {
if (signers === undefined) {
return
}
signers.map(signer => this.addIdentifierToSigner(signer))
},
addIdentifierToSigner(signer) {
// generate unique code to new signer to be possible delete or edit
if ((signer.identify === undefined || signer.identify === '') && signer.signRequestId === undefined) {
signer.identify = btoa(JSON.stringify(signer))
}
if (signer.signRequestId) {
signer.identify = signer.signRequestId
}
},
signerUpdate(signer) {
this.addIdentifierToSigner(signer)
// Remove if already exists
for (let i = this.files[this.selectedNodeId].signers.length - 1; i >= 0; i--) {
if (this.files[this.selectedNodeId].signers[i].identify === signer.identify) {
this.files[this.selectedNodeId].signers.splice(i, 1)
break
}
if (this.files[this.selectedNodeId].signers[i].signRequestId === signer.identify) {
this.files[this.selectedNodeId].signers.splice(i, 1)
break
},
async hydrateFile(nodeId) {
if (Object.hasOwn(this.files[nodeId], 'uuid')) {
return
}
}
this.files[this.selectedNodeId].signers.push(signer)
},
async deleteSigner(signer) {
if (!isNaN(signer.signRequestId)) {
await axios.delete(generateOcsUrl('/apps/libresign/api/{apiVersion}/sign/file_id/{fileId}/{signRequestId}', {
apiVersion: 'v1',
fileId: this.selectedNodeId,
signRequestId: signer.signRequestId,
await axios.get(generateOcsUrl('/apps/libresign/api/v1/file/validate/file_id/{fileId}', {
fileId: nodeId,
}))
}
set(
this.files[this.selectedNodeId],
'signers',
this.files[this.selectedNodeId].signers.filter((i) => i.identify !== signer.identify),
)
},
async getAllFiles(filter) {
const response = await axios.get(generateOcsUrl('/apps/libresign/api/v1/file/list'), { params: filter })
this.files = {}
response.data.ocs.data.data.forEach(file => {
this.addFile(file)
})
return this.files
},
filter(type) {
this.filterActive = type
if (type === 'pending') {
return Object.values(this.files).filter(
(a) => (a.status === 1 || a.status === 2)).sort(
(a, b) => (a.request_date < b.request_date) ? 1 : -1)
}
if (type === 'signed') {
return Object.values(this.files).filter(
(a) => (a.status === 3)).sort(
(a, b) => (a.request_date < b.request_date) ? 1 : -1)
}
if (type === 'all') {
this.filterActive = 'all'
return Object.values(this.files).sort((a, b) => (a.request_date < b.request_date) ? 1 : -1)
}
.then((response) => {
set(this.files, nodeId, response.data.ocs.data)
this.addUniqueIdentifierToAllSigners(this.files[nodeId].signers)
})
.catch(() => {
set(this.files[nodeId], 'signers', [])
})
},
addUniqueIdentifierToAllSigners(signers) {
if (signers === undefined) {
return
}
signers.map(signer => this.addIdentifierToSigner(signer))
},
addIdentifierToSigner(signer) {
// generate unique code to new signer to be possible delete or edit
if ((signer.identify === undefined || signer.identify === '') && signer.signRequestId === undefined) {
signer.identify = btoa(JSON.stringify(signer))
}
if (signer.signRequestId) {
signer.identify = signer.signRequestId
}
},
signerUpdate(signer) {
this.addIdentifierToSigner(signer)
// Remove if already exists
for (let i = this.files[this.selectedNodeId].signers.length - 1; i >= 0; i--) {
if (this.files[this.selectedNodeId].signers[i].identify === signer.identify) {
this.files[this.selectedNodeId].signers.splice(i, 1)
break
}
if (this.files[this.selectedNodeId].signers[i].signRequestId === signer.identify) {
this.files[this.selectedNodeId].signers.splice(i, 1)
break
}
}
this.files[this.selectedNodeId].signers.push(signer)
},
async deleteSigner(signer) {
if (!isNaN(signer.signRequestId)) {
await axios.delete(generateOcsUrl('/apps/libresign/api/{apiVersion}/sign/file_id/{fileId}/{signRequestId}', {
apiVersion: 'v1',
fileId: this.selectedNodeId,
signRequestId: signer.signRequestId,
}))
}
set(
this.files[this.selectedNodeId],
'signers',
this.files[this.selectedNodeId].signers.filter((i) => i.identify !== signer.identify),
)
},
async getAllFiles(filter) {
const { chips } = useFiltersStore()

Check failure on line 164 in src/store/files.js

View workflow job for this annotation

GitHub Actions / NPM lint

'chips' is assigned a value but never used
const { sortingMode, sortingDirection } = useFilesSortingStore()

Check failure on line 165 in src/store/files.js

View workflow job for this annotation

GitHub Actions / NPM lint

'sortingMode' is assigned a value but never used

Check failure on line 165 in src/store/files.js

View workflow job for this annotation

GitHub Actions / NPM lint

'sortingDirection' is assigned a value but never used
const response = await axios.get(generateOcsUrl('/apps/libresign/api/v1/file/list'), { params: filter })
this.files = {}
response.data.ocs.data.data.forEach(file => {
this.addFile(file)
})
return this.files
},
filter(type) {
this.filterActive = type
if (type === 'pending') {
return Object.values(this.files).filter(
(a) => (a.status === 1 || a.status === 2)).sort(
(a, b) => (a.request_date < b.request_date) ? 1 : -1)
}
if (type === 'signed') {
return Object.values(this.files).filter(
(a) => (a.status === 3)).sort(
(a, b) => (a.request_date < b.request_date) ? 1 : -1)
}
if (type === 'all') {
this.filterActive = 'all'
return Object.values(this.files).sort((a, b) => (a.request_date < b.request_date) ? 1 : -1)
}
},
},
},
})
})

const filesStore = store(...args)

// Make sure we only register the listeners once
if (!filesStore._initialized) {
subscribe('libresign:filters:update', filesStore.getAllFiles)
subscribe('libresign:sorting:update', filesStore.getAllFiles)
filesStore._initialized = true
}

return filesStore
}
3 changes: 3 additions & 0 deletions src/store/filesSorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { emit } from '@nextcloud/event-bus'

Check failure on line 6 in src/store/filesSorting.js

View workflow job for this annotation

GitHub Actions / NPM lint

There should be at least one empty line between import groups
import { defineStore } from 'pinia'

const DEFAULT_SORTING_DIRECTION = 'asc'
Expand All @@ -16,6 +17,7 @@ export const useFilesSortingStore = defineStore('filesSorting', {
actions: {
toggleSortingDirection() {
this.sortingDirection = this.sortingDirection === 'asc' ? 'desc' : 'asc'
emit('libresign:sorting:update')
},

toggleSortBy(key) {
Expand All @@ -27,6 +29,7 @@ export const useFilesSortingStore = defineStore('filesSorting', {
// else sort ASC by this new key
this.sortingMode = key
this.sortingDirection = DEFAULT_SORTING_DIRECTION
emit('libresign:sorting:update')
},
},
})
Loading

0 comments on commit a7567ab

Please sign in to comment.