-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document Component: Column Order DragNDrop #680
- Loading branch information
Showing
6 changed files
with
242 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const COLUMN_NAME_COLLECTION = '_collection'; | ||
export const COLUMN_NAME_DOCUMENT_TYPE = '_documentType'; | ||
export const COLUMN_NAME_ID = '_id'; | ||
export const COLUMN_NAME_JSON = '_json'; | ||
export const COLUMN_NAME_LANGUAGE = '_language'; | ||
export const SELECTED_COLUMNS_DEFAULT = [ | ||
COLUMN_NAME_JSON, | ||
COLUMN_NAME_ID, | ||
COLUMN_NAME_COLLECTION, | ||
COLUMN_NAME_DOCUMENT_TYPE, | ||
COLUMN_NAME_LANGUAGE, | ||
] as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type {JSONResponse} from '../../../services/graphQL/fetchers/index.d'; | ||
|
||
type GetProfileResponse = JSONResponse<{ | ||
getProfile: { | ||
documents: { | ||
columns: string[]|string | ||
} | ||
} | ||
}> | ||
|
||
|
||
import {forceArray} from '@enonic/js-utils'; | ||
import * as gql from 'gql-query-builder'; | ||
import {SELECTED_COLUMNS_DEFAULT} from './constants'; | ||
|
||
|
||
export async function getColumns({ | ||
servicesBaseUrl | ||
}: { | ||
servicesBaseUrl: string | ||
}) { | ||
return fetch(`${servicesBaseUrl}/graphQL`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(gql.query({ | ||
operation: 'getProfile' | ||
})) | ||
}) | ||
.then(res => res.json() as GetProfileResponse) | ||
.then(object => { | ||
// console.log('object', object); | ||
const { | ||
documents: { | ||
columns = [...SELECTED_COLUMNS_DEFAULT] // When there is no profile, use defaults, deref to avoid type issues | ||
} = {} | ||
} = object.data.getProfile || {}; | ||
// console.log('columns', columns); | ||
return forceArray(columns); | ||
}); | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/resources/assets/react/document/persistColumns.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type {JSONResponse} from '../../../services/graphQL/fetchers/index.d'; | ||
|
||
type ModifyProfileResponse = JSONResponse<{ | ||
modifyProfile: { | ||
columns?: string[] | ||
} | ||
}> | ||
|
||
import fastDeepEqual from 'fast-deep-equal/react'; | ||
import * as gql from 'gql-query-builder'; | ||
|
||
|
||
export async function persistColumns({ | ||
columns: columnsParam, | ||
getColumns, | ||
servicesBaseUrl | ||
}: { | ||
columns: string[] | ||
getColumns: ({servicesBaseUrl}: {servicesBaseUrl: string}) => Promise<string[]> | ||
servicesBaseUrl: string | ||
}) { | ||
return getColumns({servicesBaseUrl}).then(prevColumns => { | ||
if (fastDeepEqual(columnsParam, prevColumns)) { | ||
// console.debug('columns unchanged', prevColumns); | ||
return prevColumns; | ||
} else { | ||
// console.debug('columns changed', columnsParam, 'prev', prevColumns); | ||
return fetch(`${servicesBaseUrl}/graphQL`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(gql.mutation({ | ||
operation: 'modifyProfile', | ||
variables: { | ||
object: { | ||
list: false, | ||
required: true, | ||
type: 'JSON', | ||
value: { | ||
columns: columnsParam | ||
} | ||
}, | ||
scope: { | ||
list: false, | ||
required: true, | ||
type: 'String', | ||
value: 'documents' | ||
} | ||
} | ||
})) | ||
}) | ||
.then(res => res.json() as ModifyProfileResponse) | ||
.then((object) => { | ||
// console.log('object', object); | ||
const { | ||
columns// = SELECTED_COLUMNS_DEFAULT // This will reset to default when all columns are removed | ||
} = object.data.modifyProfile; | ||
return columns; | ||
}); | ||
} | ||
}); | ||
|
||
|
||
} |
Oops, something went wrong.