3030import { getBuilder } from '@nextcloud/browser-storage'
3131import { getCapabilities } from '@nextcloud/capabilities'
3232import { parseFileSize } from '@nextcloud/files'
33- import { generateOcsUrl } from '@nextcloud/router'
33+ import { showError } from '@nextcloud/dialogs'
34+ import { generateOcsUrl , generateUrl } from '@nextcloud/router'
3435import axios from '@nextcloud/axios'
3536
37+ import { GroupSorting } from '../constants/GroupManagement.ts'
3638import api from './api.js'
3739import logger from '../logger.ts'
3840
3941const localStorage = getBuilder ( 'settings' ) . persist ( true ) . build ( )
4042
41- const orderGroups = function ( groups , orderBy ) {
42- /* const SORT_USERCOUNT = 1;
43- * const SORT_GROUPNAME = 2;
44- * https://github.com/nextcloud/server/blob/208e38e84e1a07a49699aa90dc5b7272d24489f0/lib/private/Group/MetaData.php#L34
45- */
46- if ( orderBy === 1 ) {
47- return groups . sort ( ( a , b ) => a . usercount - a . disabled < b . usercount - b . disabled )
48- } else {
49- return groups . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
50- }
51- }
52-
5343const defaults = {
5444 group : {
5545 id : '' ,
@@ -64,7 +54,7 @@ const defaults = {
6454const state = {
6555 users : [ ] ,
6656 groups : [ ] ,
67- orderBy : 1 ,
57+ orderBy : GroupSorting . UserCount ,
6858 minPasswordLength : 0 ,
6959 usersOffset : 0 ,
7060 usersLimit : 25 ,
@@ -100,8 +90,6 @@ const mutations = {
10090 state . groups = groups . map ( group => Object . assign ( { } , defaults . group , group ) )
10191 state . orderBy = orderBy
10292 state . userCount = userCount
103- state . groups = orderGroups ( state . groups , state . orderBy )
104-
10593 } ,
10694 addGroup ( state , { gid, displayName } ) {
10795 try {
@@ -114,7 +102,6 @@ const mutations = {
114102 name : displayName ,
115103 } )
116104 state . groups . unshift ( group )
117- state . groups = orderGroups ( state . groups , state . orderBy )
118105 } catch ( e ) {
119106 console . error ( 'Can\'t create group' , e )
120107 }
@@ -125,7 +112,6 @@ const mutations = {
125112 const updatedGroup = state . groups [ groupIndex ]
126113 updatedGroup . name = displayName
127114 state . groups . splice ( groupIndex , 1 , updatedGroup )
128- state . groups = orderGroups ( state . groups , state . orderBy )
129115 }
130116 } ,
131117 removeGroup ( state , gid ) {
@@ -143,7 +129,6 @@ const mutations = {
143129 }
144130 const groups = user . groups
145131 groups . push ( gid )
146- state . groups = orderGroups ( state . groups , state . orderBy )
147132 } ,
148133 removeUserGroup ( state , { userid, gid } ) {
149134 const group = state . groups . find ( groupSearch => groupSearch . id === gid )
@@ -154,7 +139,6 @@ const mutations = {
154139 }
155140 const groups = user . groups
156141 groups . splice ( groups . indexOf ( gid ) , 1 )
157- state . groups = orderGroups ( state . groups , state . orderBy )
158142 } ,
159143 addUserSubAdmin ( state , { userid, gid } ) {
160144 const groups = state . users . find ( user => user . id === userid ) . subadmin
@@ -254,6 +238,23 @@ const mutations = {
254238 localStorage . setItem ( `account_settings__${ key } ` , JSON . stringify ( value ) )
255239 state . showConfig [ key ] = value
256240 } ,
241+
242+ setGroupSorting ( state , sorting ) {
243+ const oldValue = state . orderBy
244+ state . orderBy = sorting
245+
246+ // Persist the value on the server
247+ axios . post (
248+ generateUrl ( '/settings/users/preferences/group.sortBy' ) ,
249+ {
250+ value : String ( sorting ) ,
251+ } ,
252+ ) . catch ( ( error ) => {
253+ state . orderBy = oldValue
254+ showError ( t ( 'settings' , 'Could not set group sorting' ) )
255+ logger . error ( error )
256+ } )
257+ } ,
257258}
258259
259260const getters = {
@@ -267,6 +268,21 @@ const getters = {
267268 // Can't be subadmin of admin or disabled
268269 return state . groups . filter ( group => group . id !== 'admin' && group . id !== 'disabled' )
269270 } ,
271+ getSortedGroups ( state ) {
272+ const groups = [ ...state . groups ]
273+ if ( state . orderBy === GroupSorting . UserCount ) {
274+ return groups . sort ( ( a , b ) => {
275+ const numA = a . usercount - a . disabled
276+ const numB = b . usercount - b . disabled
277+ return ( numA < numB ) ? 1 : ( numB < numA ? - 1 : a . name . localeCompare ( b . name ) )
278+ } )
279+ } else {
280+ return groups . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
281+ }
282+ } ,
283+ getGroupSorting ( state ) {
284+ return state . orderBy
285+ } ,
270286 getPasswordPolicyMinLength ( state ) {
271287 return state . minPasswordLength
272288 } ,
0 commit comments