Skip to content

Commit

Permalink
Merge pull request #6 from agility/apps-sdk-v2-marketplace
Browse files Browse the repository at this point in the history
tweak to modal params
  • Loading branch information
joelvarty authored May 26, 2023
2 parents fe291a5 + b9b4cab commit b3185b4
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agility/app-sdk",
"version": "2.0.0-rc12",
"version": "2.0.0-rc18",
"description": "JavaScript library for building Agility CMS apps.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
7 changes: 7 additions & 0 deletions src/lib/getCloseModalID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const getCloseModalID = (): string | null => {
if (typeof window === 'undefined') return null
const params = window.location.search
const urlParams = new URLSearchParams(params)
const id = urlParams.get('closeModalID') ?? null
return id
}
36 changes: 36 additions & 0 deletions src/methods/closeModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { IAppEventParam, IModalParam } from '../types';
import { getOperationID } from '../lib/getOperationID';
import { Subject } from 'rxjs';
import { addOperation } from '../lib/operationAccess';
import { getAppID } from '../lib/getAppID';
import { invokeAppMethod } from '../lib/invokeAppMethod';
import { getCloseModalID } from '../lib/getCloseModalID';

/**
* Closes the current modal and passes the props to the callback. Only to be used on a modal surface.
*
* @param {*} props
*/
export const closeModal = ( props : any) => {

const appID = getAppID()
const closeModalID = getCloseModalID()
if (!appID || !closeModalID) return

const operationID = getOperationID()

const arg: IAppEventParam<{ closeModalID: string, props: any }> = {
appID,
operationID,
operationType: "closeModal",
arg: {
closeModalID,
props
}
}


//call the method in the parent windpow
invokeAppMethod(arg)

}
1 change: 1 addition & 0 deletions src/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * as assetsMethods from "./assets"
export * as pageMethods from "./pages"
export * from './setHeight'
export * from './openModal'
export * from './closeModal'
export * from "./getAppInstall"
export * from "./refresh"
export * from "./setVisibility"
Expand Down
10 changes: 6 additions & 4 deletions src/methods/openModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ import { invokeAppMethod } from '../lib/invokeAppMethod';
* @template T
* @param {Props<T>} { name, props, callback }
*/
export const openModal = <T>({ title, callback }: IModalParam<T> ) => {
export const openModal = <T>({ name, title, props, callback }: IModalParam<T> ) => {

const appID = getAppID()
if (!appID) return
const operationID = getOperationID()
const closeModalID = getOperationID()

const arg: IAppEventParam<{ closeModalID: string, title: string }> = {
const arg: IAppEventParam<{ closeModalID: string, name: string, title: string | null, props: any }> = {
appID,
operationID,
operationType: "openModal",
arg: {
closeModalID,
title
title: title || null,
name,
props
}
}

Expand All @@ -36,7 +38,7 @@ export const openModal = <T>({ title, callback }: IModalParam<T> ) => {

const closeOperation = new Subject<T>();
closeOperation.subscribe((ret: any) => {
callback(ret)
callback(ret?.props)
closeOperation.unsubscribe()
})

Expand Down
5 changes: 3 additions & 2 deletions src/types/IAppEventParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface IAppEventParam<T> {
| "updateConfigurationValue"
| "getContentItem"
| "openModal"
| "closeModal"
| "selectAssets"
| "persistData"
| "refresh"
Expand All @@ -24,8 +25,8 @@ export interface IAppEventParam<T> {
| "removeSelectedItemListener"
| "setVisibility"
| "getPageItem"
| "getManagementAPIToken"
| "getAPIKey"
| "getManagementAPIToken"
| "getAPIKey"
error?: string
arg?: T
}
2 changes: 2 additions & 0 deletions src/types/IContextParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ export interface IContextParam {
contentItem?: IContentItem
contentModel?: IContentModel
pageItem?: IPageItem
modalProps?: any
closeModalID?: string
}
29 changes: 28 additions & 1 deletion src/types/IModalParam.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
export interface IModalParam<T> {
/**
* The title to show on the model. Include this to have the built-in title bar with close button. Omit it to do your own title bar.
*
* @type {string}
* @memberof IModalParam
*/
title: string

/**
* The name of the modal to show.
*
* @type {string}
* @memberof IModalParam
*/
name: string

/**
* The props to pass to the modal.
*
* @type {*}
* @memberof IModalParam
*/
props?: any
callback: (result: T) => void

/**
* The callback for when the modal is closed.
*
* @memberof IModalParam
*/
callback: (result?: T) => void
}
7 changes: 6 additions & 1 deletion src/useAgilityAppSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface AgilityAddSKReturn {
contentItem: IContentItem | null,
contentModel: IContentModel | null
pageItem: IPageItem | null,
modalProps: any
}

/**
Expand All @@ -35,6 +36,8 @@ export const useAgilityAppSDK = (): AgilityAddSKReturn => {
const [contentItem, setContentItem] = useState<IContentItem | null>(null)
const [pageItem, setPageItem] = useState<IPageItem | null>(null)

const [modalProps, setModalProps] = useState<any>(null)

useEffect(() => {
const appID = getAppID()
if (!appID) return
Expand All @@ -52,6 +55,7 @@ export const useAgilityAppSDK = (): AgilityAddSKReturn => {

setContentItem(context.contentItem || null)
setContentModel(context.contentModel || null)
setModalProps(context.modalProps || null)

setInitializing(false)
operation.unsubscribe()
Expand Down Expand Up @@ -85,6 +89,7 @@ export const useAgilityAppSDK = (): AgilityAddSKReturn => {
field,
contentItem,
contentModel,
pageItem
pageItem,
modalProps
}
}

0 comments on commit b3185b4

Please sign in to comment.