Skip to content

Commit

Permalink
Move shared functions for Portfolio/Aggregating to shared PortalDataS…
Browse files Browse the repository at this point in the history
…ervice [skip-install]
  • Loading branch information
Remi749 committed Jan 18, 2024
1 parent fed58ea commit 91febeb
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export function useColumnFormPanel() {
}
try {
if (isEditing) {
await context.props.dataAdapter
.updateProjectContentColumn(columnItem, persistRenderGlobally)
await context.props.dataAdapter.portalDataService
.updateProjectContentColumn('PROJECT_CONTENT_COLUMNS', columnItem, persistRenderGlobally)
.then(() => {
const editedColumn = new ProjectContentColumn(columnItem)
context.dispatch(ADD_COLUMN(editedColumn))
Expand All @@ -50,8 +50,8 @@ export function useColumnFormPanel() {
const updateItem: SPDataSourceItem = {
GtProjectContentColumnsId: properties.Id
}
context.props.dataAdapter
.updateDataSourceItem(updateItem, context.state.currentView?.title)
context.props.dataAdapter.portalDataService
.updateDataSourceItem('DATA_SOURCES', updateItem, context.state.currentView?.title)
.then(() => {
context.dispatch(ADD_COLUMN(newColumn))
})
Expand All @@ -61,8 +61,8 @@ export function useColumnFormPanel() {
}

const onDeleteColumn = async () => {
await context.props.dataAdapter
.deleteProjectContentColumn(context.state.columnForm.column)
await context.props.dataAdapter.portalDataService
.deleteProjectContentColumn('PROJECT_CONTENT_COLUMNS', context.state.columnForm.column)
.then(() => {
context.dispatch(DELETE_COLUMN())
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ export function useViewFormPanel() {
}

/**
* Saves the changes made to the view by updating the item in the `DATA_SOURCES` list or adding a new item to the list.
* Dismisses the form panel by dispatching the `SET_VIEW_FORM_PANEL` action.
* Saves the column to the list. If the column is new, it will
* also add the column to the current view. If the column is
* being edited, it will update the column in the list.
*
* If the column is being edited, it will update the column in the list
* using `updateItemInList` from the `dataAdapter`. If the column is new,
* it will add the column to the list using `addItemToList` from
* the `dataAdapter`.
*/
const onSave = async () => {
const { currentView } = context.state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export function useEditViewColumnsPanel(
const properties: SPDataSourceItem = {
GtProjectContentColumnsId: columns.map((c) => c.id)
}
await context.props.dataAdapter
.updateDataSourceItem(properties, context.state.currentView?.title, true)
await context.props.dataAdapter.portalDataService
.updateDataSourceItem('DATA_SOURCES', properties, context.state.currentView?.title, true)
.then(() => {
context.dispatch(SET_COLUMNS({ columns }))
onDismiss()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const ColumnFormPanel: FC = () => {
findMatchingSearchProperty,
columnMessages
} = useColumnFormPanel()

return (
<Panel
isOpen={context.state.columnForm.isOpen}
Expand Down
96 changes: 6 additions & 90 deletions SharePointFramework/PortfolioWebParts/src/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { format } from '@fluentui/react/lib/Utilities'
import { dateAdd, PnPClientStorage, stringIsNullOrEmpty } from '@pnp/core'
import { dateAdd, PnPClientStorage } from '@pnp/core'
import {
IItemUpdateResult,
ISearchResult,
PermissionKind,
QueryPropertyValueType,
Expand All @@ -22,10 +21,8 @@ import {
ProjectContentColumn,
ProjectListModel,
SPContentType,
SPDataSourceItem,
SPFxContext,
SPProjectColumnItem,
SPProjectContentColumnItem,
SPProjectItem,
SPTimelineConfigurationItem,
TimelineConfigurationModel,
Expand Down Expand Up @@ -164,7 +161,11 @@ export class DataAdapter implements IPortfolioWebPartsDataAdapter {
calculatedLevel = strings.DataSourceLevelProject
}
level = level ?? calculatedLevel
const columns = await this.fetchProjectContentColumns(category, level)
const columns = await this.portalDataService.fetchProjectContentColumns(
'PROJECT_CONTENT_COLUMNS',
category,
level
)
const [views, viewsUrls, columnUrls, levels] = await Promise.all([
this.fetchDataSources(category, level, columns),
this.portalDataService.getListFormUrls('DATA_SOURCES'),
Expand Down Expand Up @@ -762,65 +763,6 @@ export class DataAdapter implements IPortfolioWebPartsDataAdapter {
}
}

public async fetchProjectContentColumns(dataSourceCategory: string, level?: string) {
try {
if (stringIsNullOrEmpty(dataSourceCategory)) return []
const projectContentColumnsList = this.portalDataService.web.lists.getByTitle(
strings.ProjectContentColumnsListName
)
const columnItems = await projectContentColumnsList.items.select(
...Object.keys(new SPProjectContentColumnItem())
)()
const filteredColumnItems = columnItems.filter(
(col) =>
col.GtDataSourceCategory === dataSourceCategory ||
(!col.GtDataSourceCategory && !col.GtDataSourceLevel) ||
(!col.GtDataSourceCategory && _.contains(col.GtDataSourceLevel, level))
)
return filteredColumnItems.map((item) => new ProjectContentColumn(item))
} catch (error) {
throw new Error(format(strings.DataSourceCategoryError, dataSourceCategory))
}
}

public async updateProjectContentColumn(
columnItem: SPProjectContentColumnItem,
persistRenderAs = false
): Promise<IItemUpdateResult> {
try {
const list = this._sp.web.lists.getByTitle(strings.ProjectContentColumnsListName)
const properties: SPProjectContentColumnItem = _.pick(
columnItem,
[
'GtColMinWidth',
'GtColMaxWidth',
persistRenderAs && 'GtFieldDataTypeProperties',
persistRenderAs && 'GtFieldDataType'
].filter(Boolean)
)
return await list.items.getById(columnItem.Id).update(properties)
} catch (error) {
throw new Error(error)
}
}

public async deleteProjectContentColumn(column: Record<string, any>): Promise<any> {
try {
const list = this._sp.web.lists.getByTitle(strings.ProjectContentColumnsListName)
const items = await list.items()
const item = items.find((i) => i.GtManagedProperty === column.fieldName)

if (!item) {
throw new Error(format(strings.ProjectContentColumnItemNotFound, column.fieldName))
}

const itemDeleteResult = list.items.getById(item.Id).delete()
return itemDeleteResult
} catch (error) {
throw new Error(error)
}
}

public async deleteItemFromList(listName: string, itemId: number): Promise<boolean> {
try {
const list = this._sp.web.lists.getByTitle(listName)
Expand All @@ -847,32 +789,6 @@ export class DataAdapter implements IPortfolioWebPartsDataAdapter {
return false
}
}

public async updateDataSourceItem(
properties: SPDataSourceItem,
dataSourceTitle: string,
shouldReplace: boolean = false
): Promise<IItemUpdateResult> {
try {
const list = this._sp.web.lists.getByTitle(strings.DataSourceListName)
const [item] = await list.items.filter(`Title eq '${dataSourceTitle}'`)()
if (!item) {
throw new Error(format(strings.DataSourceItemNotFound, dataSourceTitle))
}
if (item.GtProjectContentColumnsId && !shouldReplace) {
properties.GtProjectContentColumnsId = [
...item.GtProjectContentColumnsId,
properties.GtProjectContentColumnsId
]
return await list.items.getById(item.Id).update(properties)
} else {
properties.GtProjectContentColumnsId = properties.GtProjectContentColumnsId as number[]
return await list.items.getById(item.Id).update(properties)
}
} catch (error) {
throw new Error(error)
}
}
}

export * from './types'
51 changes: 0 additions & 51 deletions SharePointFramework/PortfolioWebParts/src/data/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { WebPartContext } from '@microsoft/sp-webpart-base'
import { IItemUpdateResult } from '@pnp/sp/items'
import { ISiteUserInfo } from '@pnp/sp/presets/all'
import { ISearchResult, SortDirection } from '@pnp/sp/search'
import {
Expand All @@ -10,9 +9,7 @@ import {
PortfolioOverviewView,
ProjectContentColumn,
ProjectListModel,
SPDataSourceItem,
SPProjectColumnItem,
SPProjectContentColumnItem,
SPProjectItem,
TimelineConfigurationModel,
TimelineContentModel
Expand Down Expand Up @@ -281,41 +278,6 @@ export interface IPortfolioWebPartsDataAdapter {
dataSourceCategory?: string
): Promise<any[]>

/**
* Fetch project content columns from the project content columns SharePoint list on the hub site
* with the specified `dataSourceCategory` or without a category. The result is transformed into
* `ProjectColumn` objects. The `renderAs` property is set to the `dataType` property in lower case
* and with spaces replaced with underscores.
*
* If the `dataSourceCategory` is null or empty, an empty array is returned.
*
* @param category Category for data source
* @param level Level for data source
*/
fetchProjectContentColumns?(
dataSourceCategory: string,
level?: string
): Promise<ProjectContentColumn[]>

/**
* Update project content column with new values for properties `GtColMinWidth` and `GtColMaxWidth`,
* as well as the `GtFieldDataType` property if parameter `persistRenderAs` is true.
*
* @param column Project content column
* @param persistRenderAs Persist render as property
*/
updateProjectContentColumn?(
columnItem: SPProjectContentColumnItem,
persistRenderAs?: boolean
): Promise<any>

/**
* Delete project content column
*
* @param column Column to delete
*/
deleteProjectContentColumn?(property: Record<string, any>): Promise<any>

/**
* Adds a new column to the project columns list and adds the column to the specified view.
*
Expand All @@ -326,17 +288,4 @@ export interface IPortfolioWebPartsDataAdapter {
properties: SPProjectColumnItem,
view: PortfolioOverviewView
): Promise<boolean>

/**
* Update the data source item with title `dataSourceTitle` with the properties in `properties`.
*
* @param properties Properties
* @param dataSourceTitle Data source title
* @param shouldReplace Should replace the existing columns
*/
updateDataSourceItem?(
properties: SPDataSourceItem,
dataSourceTitle: string,
shouldReplace?: boolean
): Promise<IItemUpdateResult>
}
Loading

0 comments on commit 91febeb

Please sign in to comment.