1+ import { Checkbox , ChoiceGroup , CommandBarButton , DefaultButton , Dialog , DialogFooter , DialogType , IChoiceGroupOption , MessageBar , MessageBarType , PrimaryButton } from '@fluentui/react' ;
2+ import { useEffect , useState } from 'react' ;
3+ import { IQuickLinkListProps } from './Actions' ;
4+ import { buttonStyles } from './QuickLinkButton' ;
5+
6+ function updatePageLayout ( siteUrl : string , serverRequestPath : string , layout : any ) {
7+ console . log ( siteUrl , serverRequestPath , layout ) ;
8+
9+ fetch ( siteUrl + '/_api/contextinfo' , {
10+ method : 'POST' ,
11+ headers : {
12+ accept : 'application/json;odata=nometadata' ,
13+ 'content-type' : 'application/json;odata=nometadata' ,
14+ 'X-ClientService-ClientTag' : 'SPEDITOR'
15+ }
16+ } )
17+ . then ( response => response . json ( ) )
18+ . then ( ( r ) => {
19+ console . log ( r ) ;
20+
21+ fetch ( siteUrl + "/_api/web/getFileByServerRelativeUrl('" + serverRequestPath + "')/listItemAllFields" , {
22+ method : 'POST' ,
23+ headers : {
24+ accept : 'application/json;odata=nometadata' ,
25+ 'X-HTTP-Method' : 'MERGE' ,
26+ 'IF-MATCH' : '*' ,
27+ 'X-RequestDigest' : r . FormDigestValue ,
28+ 'content-type' : 'application/json;odata=nometadata' ,
29+ 'X-ClientService-ClientTag' : 'SPEDITOR'
30+ } ,
31+ body : JSON . stringify ( {
32+ PageLayoutType : layout
33+ } )
34+ } ) . then ( ( ) => {
35+ return true
36+ } )
37+ } ) ;
38+ }
39+
40+ const ChangePageLayout = ( { plo, tabId, ctx } : IQuickLinkListProps ) => {
41+
42+ const options : IChoiceGroupOption [ ] = [
43+ { key : 'Home' , text : 'Home' } ,
44+ { key : 'Article' , text : 'Article' } ,
45+ { key : 'SingleWebPartAppPage' , text : 'SingleWebPartAppPage' } ,
46+ { key : 'RepostPage' , text : 'RepostPage' , disabled : true } ,
47+ { key : 'HeaderlessSearchResults' , text : 'HeaderlessSearchResults' , disabled : true } ,
48+ { key : 'Spaces' , text : 'Spaces' , disabled : true } ,
49+ { key : 'Topic' , text : 'Topic' , disabled : true } ,
50+ ] ;
51+ const modelProps = {
52+ isBlocking : false ,
53+ styles : { main : { maxWidth : 450 } } ,
54+ } ;
55+ const dialogContentProps = {
56+ type : DialogType . largeHeader ,
57+ title : 'Change pagelayout' ,
58+ subText : "Change the pagelayout of the current page. Enable `I know what I´m doing` for more pagelayouts." ,
59+ } ;
60+
61+ const [ hideDialog , setHideDialog ] = useState ( true ) ;
62+ const [ showDisabled , setShowDisabled ] = useState ( false ) ;
63+ const [ selected , setSelected ] = useState ( plo . PageLayoutType ) ;
64+ const [ showSuccess , setShowSuccess ] = useState ( false ) ;
65+
66+ useEffect ( ( ) => {
67+ setTimeout ( ( ) => {
68+ setShowSuccess ( false )
69+ } , 3000 )
70+ } , [ showSuccess ] )
71+
72+
73+ return ( < >
74+ < CommandBarButton
75+ text = { 'Change pagelayout' }
76+ iconProps = { { iconName : 'Edit' } }
77+ styles = { buttonStyles }
78+ disabled = { ! plo . PageLayoutType }
79+ onClick = { ( ) => setHideDialog ( false ) }
80+ />
81+ < Dialog
82+ hidden = { hideDialog }
83+ onDismiss = { ( ) => setHideDialog ( false ) }
84+ dialogContentProps = { dialogContentProps }
85+ modalProps = { modelProps } >
86+ < ChoiceGroup
87+ defaultSelectedKey = { plo . PageLayoutType }
88+ options = { options . map ( ( e ) => ! showDisabled ? e : { ...e , disabled : false } ) }
89+ onChange = { ( e , option ) => setSelected ( option ! . key ) }
90+ />
91+ < Checkbox styles = { { root : { marginTop : 10 } } }
92+ label = "Enable more options please, I know what I'm doing!"
93+ onChange = { ( e , checked ) => setShowDisabled ( checked || false ) }
94+ checked = { showDisabled }
95+ />
96+ { showSuccess ? < MessageBar messageBarType = { MessageBarType . success } >
97+ Pagelayout changed, reload the page.
98+ </ MessageBar > : < div style = { { height : 32 } } > </ div > }
99+ < DialogFooter >
100+ < PrimaryButton onClick = { ( ) => chrome . scripting . executeScript ( {
101+ target : { tabId : tabId } ,
102+ world : 'MAIN' ,
103+ args : [ ctx . webAbsoluteUrl , ctx . serverRequestPath , selected ] ,
104+ func : updatePageLayout ,
105+ } ) . then ( injectionResults => {
106+ console . log ( injectionResults ) ;
107+ setShowSuccess ( true ) ;
108+ } ) }
109+ text = "Save"
110+ disabled = { plo . PageLayoutType === selected } />
111+ < DefaultButton onClick = { ( ) => setHideDialog ( true ) } text = "Close" />
112+ </ DialogFooter >
113+ </ Dialog >
114+ </ >
115+ )
116+ }
117+
118+ export default ChangePageLayout ;
0 commit comments