UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

250 lines 12.3 kB
import { check4Gulp } from "../../../../logic/Links/CheckGulping"; import { makeid } from "../../../../logic/Strings/guids"; import { startPerformOp, updatePerformanceEnd } from "../../../molecules/Performance/functions"; import { doSpHttpFetchOrPostAndCheck } from "../../../molecules/SpHttp/Sp/doSpHttpFetch"; import { GetOrPostTenantThemes } from "./GetOrPostTenantThemes"; export async function _reloadThemes(fpsSpService, _performance, displayMode, serverRelativeUrl, setState, forceAnalytics = false) { if (check4Gulp() === true) console.log(`${PalPalconsolePrefix} ~ componentDidMount`); //Start tracking performance _performance.ops.fetch = startPerformOp('fetch Themes', displayMode); _performance.ops.fetch1 = startPerformOp('fetch1 TitleText', displayMode); //Do async code here // for (let i = 0; i < 9000000; i++) { const x = 100 * 4e5 / .12343; } //End tracking performance /** * "https://mysite-admin.sharepoint.com/" * const formDigestValue = this.context.pageContext.web.formDigestValue; * GetTenantThemingOptions("/_api/thememanager/GetTenantThemingOptions", this.); * @param url * @param formDigestValue * @param params * @returns */ const apiFetchThemes = `${serverRelativeUrl}/_api/ThemeManager/GetTenantThemingOptions`; // 9 Works // const apiAddThemes: string = `${serverRelativeUrl}/_api/ThemeManager/AddTenantTheme`;// // const apiDelThemes: string = `${serverRelativeUrl}/_api/ThemeManager/DeleteTenantTheme`;// // const addNew1 = AddTenantThemingOptions( serverRelativeUrl, "/_api/thememanager/AddTenantTheme", this.props._formDigest, {name:"TEST MZ 99", themeJson: JSON.stringify(paletteSample2)}); // const addNew4 = AddTenantThemingOptions( serverRelativeUrl, "/_api/thememanager/DeleteTenantTheme", this.props._formDigest, {name:"Mikey1", }); // Works const fpsThemes1 = await doSpHttpFetchOrPostAndCheck(apiFetchThemes, 'GET', fpsSpService, '', false, true, 'theme', false, null); // eslint-disable-next-line @typescript-eslint/no-explicit-any const resultThemes = fpsThemes1.items; const statePaletteStr = []; const statePaletts = []; const paletteNames = []; if (resultThemes.themePreviews) { const newItems = JSON.parse(JSON.stringify(resultThemes.themePreviews)); newItems.map((item) => { paletteNames.push(item.name); statePaletteStr.push(item.themeJson ? item.themeJson : "Invalid themJson object"); const tempObj = item.themeJson ? JSON.parse(item.themeJson) : { palette: {} }; statePaletts.push({ name: item.name, themeJson: item.themeJson, palette: tempObj.palette, valid: item.themeJson ? true : false, }); }); // eslint-disable-next-line @typescript-eslint/no-explicit-any fpsThemes1.rawSearchResults = resultThemes.hideDefaultThemes; fpsThemes1['itemsY'] = newItems; } console.log(`fpsThemes1`, fpsThemes1); _performance.ops.fetch1 = updatePerformanceEnd(_performance.ops.fetch1, true, 777); _performance.ops.fetch = updatePerformanceEnd(_performance.ops.fetch, true, 999); setState({ paletteState: fpsThemes1, tenantPalettes: fpsThemes1['itemsY'], statePaletteStr: statePaletteStr, statePaletts: statePaletts, paletteNames: paletteNames, selectedPalette: PalPaldefaultPivot, selectedIdx: -1, validUpdate: false, }); return { forceAnalytics: forceAnalytics, _performance: _performance, }; /** * Commenting this part out until I have a vanilla saveAnalytics function in here */ // const analyticsWasExecuted = saveViewAnalytics( 'Palette Pal Banner View', 'didMount' , this.props, 'Views' , forceAnalytics === true ? false : this.state.analyticsWasExecuted, _performance ); // if ( this.state.analyticsWasExecuted !== analyticsWasExecuted ) { // setState({ analyticsWasExecuted: analyticsWasExecuted }); // } } //Use this to add more console.logs for this component export const PalPalconsolePrefix = 'fpsconsole: Palette Pal'; export const PalPaldefaultPivot = `Tenant`; export const PalPalLinkStyles = { fontSize: 'x-large', cursor: 'pointer', fontWeight: 600 }; export function _selectPalette(itemKey, reactState, setState) { console.log('pivotMainClick:', itemKey); //This will force state update first, to show spinner, then will update the info. https://stackoverflow.com/a/38245851 setState({ showSpinner: true, selectedPalette: itemKey, selectedIdx: reactState.paletteNames.indexOf(itemKey), statusMessage: '', messageRefresh: makeid(5), newNameValid: true, newName: itemKey, }); } export function _updatePaletteJSON(text, reactState, setState) { const { selectedIdx } = reactState; let JSONIsValid = text ? true : false; if (JSONIsValid === true) { try { JSON.parse(text); } catch (e) { JSONIsValid = false; } } const statePaletteStr = JSON.parse(JSON.stringify(reactState.statePaletteStr)); statePaletteStr[selectedIdx] = text; setState({ statePaletteStr: statePaletteStr, statusMessage: JSONIsValid === true ? '' : 'Error: JSON seems to be invalid.', messageRefresh: makeid(5), JSONIsValid: JSONIsValid, }); } export function _updateThemeName(text, reactState, setState) { // if ( text && reactState.paletteNames.indexOf( text ) < 0 ) { // } const hasNewName = text ? true : false; const alreadyExists = reactState.paletteNames.some(x => x.toLowerCase() === text.toLowerCase()); const newNameValid = hasNewName === true && alreadyExists === false; setState({ newName: text, newNameValid: newNameValid, statusMessage: !hasNewName ? 'Error: Requires Name' : newNameValid === true ? '' : `Error: Name is Duplicate`, statusResult: !hasNewName || newNameValid === true ? false : true, messageRefresh: makeid(5), }); } export async function _executeCommandErrMess(cmd, _formDigest, userTitle, serverRelativeUrl, reactState, setState) { let errMess = ``; const { selectedIdx, newName, selectedPalette, statePaletteStr, paletteNames } = reactState; // Do nothing if no name - YES YOU CAN CREATE A THEME WITHOUT A NAME!!!! if (cmd === 'Delete' && selectedIdx < 0) errMess = `Error: Can't Delete an invalid selectedIdx`; // else if ( cmd === 'Update' && selectedIdx < 0 ) return; else if (cmd === 'Create' && !newName) errMess = `Error: Can't Create without a valid name`; console.log(`_executeCommand`, cmd); if (cmd === 'Delete') { const test = prompt(`Delete: ${paletteNames[selectedIdx]}\n\nDeleting is permanent! \n\nTo verify, please type in your full name :) `); if (test !== userTitle) { console.log(`Delete Captcha was incorrect:`, test, userTitle); errMess = `Error: Invalid captcha test... you typed ${test}`; } } if (cmd === 'Apply') { const test = prompt(`Apply: ${paletteNames[selectedIdx]}\n\nApplying is permanent! \n\nTo verify, please type in your full name :) `); if (test !== userTitle) { console.log(`Apply Captcha was incorrect:`, test, userTitle); errMess = `Error: Invalid captcha test... you typed ${test}`; } } if (errMess) setState({ statusMessage: errMess, statusResult: false, messageRefresh: makeid(5), }); if (errMess) return errMess; const partialAPI = `/_api/ThemeManager/`; // const apiAddThemes: string = `${serverRelativeUrl}/_api/ThemeManager/AddTenantThemingOptions`;// // const apiDelThemes: string = `${serverRelativeUrl}/_api/ThemeManager/DeleteTenantTheme`;// let fullAPI = ``; // eslint-disable-next-line @typescript-eslint/no-explicit-any let options = null; let statusMessage = ``; const themeJsonStr = statePaletteStr[selectedIdx] ? statePaletteStr[selectedIdx].replace(/\n/g, '').replace(/\\"/g, '"') : ''; // eslint-disable-next-line @typescript-eslint/no-explicit-any let themeJsonObj = null; if (cmd === 'Create' || cmd === 'Update' || cmd === 'Apply') { try { themeJsonObj = JSON.parse(themeJsonStr); // Need to automatically add the palette parent object structure if (themeJsonObj && !themeJsonObj.palette) { themeJsonObj = { palette: JSON.parse(JSON.stringify(themeJsonObj)) }; } options = { name: newName, themeJson: JSON.stringify(themeJsonObj) }; } catch (e) { statusMessage = "Error: Invalid JSON string"; } } if (statusMessage) { setState({ statusMessage: statusMessage, statusResult: false, messageRefresh: makeid(5), }); } if (statusMessage) return statusMessage; if (cmd === 'Delete') { fullAPI = `${partialAPI}DeleteTenantTheme`; options = { name: selectedPalette, }; } else if (cmd === 'Create') { fullAPI = `${partialAPI}AddTenantTheme`; // options = { name: newName, themeJson: JSON.stringify( statePaletteStr[-2] ) }; {name:"TEST MZ 99", themeJson: JSON.stringify(paletteSample2)} // Need to automatically add the palette parent object structure if (themeJsonObj && !themeJsonObj.palette) { themeJsonObj = { palette: JSON.parse(JSON.stringify(themeJsonObj)) }; } options = { name: newName, themeJson: JSON.stringify(themeJsonObj) }; } else if (cmd === 'Update') { fullAPI = `${partialAPI}UpdateTenantTheme`; const themeJsonStr = statePaletteStr[selectedIdx] ? statePaletteStr[selectedIdx].replace(/\n/g, '').replace(/\\"/g, '"') : ''; options = { name: paletteNames[selectedIdx], themeJson: themeJsonStr }; } else if (cmd === 'Apply') { fullAPI = `${partialAPI}ApplyTheme`; const themeJsonStr = statePaletteStr[selectedIdx] ? statePaletteStr[selectedIdx].replace(/\n/g, '').replace(/\\"/g, '"') : ''; options = { name: paletteNames[selectedIdx], themeJson: themeJsonStr }; } else { errMess = `Not sure what happened but was not expecting _executeCommand of: ${cmd}`; alert(errMess); return errMess; } const doUpdate = await GetOrPostTenantThemes(serverRelativeUrl, fullAPI, _formDigest, options, 'POST'); let statusResult = false; if (doUpdate.responseText) { try { const statusObj = JSON.parse(doUpdate.responseText); if (statusObj) { statusMessage = `${cmd} ${options.name} result: ${statusObj.value}`; statusResult = statusObj.value; } if (cmd === 'Delete') { statusMessage = `Refresh Themes to verify ${options.name} was deleted.`; statusResult = null; } else if (cmd === 'Apply') { statusMessage = `Refresh SITE to verify ${options.name} was applied.`; statusResult = null; } } catch (e) { statusMessage = `No idea what happened!`; } } else if (cmd === 'Delete' || cmd === 'Create') { statusMessage = `Refresh Themes to verify ${options.name} was ${cmd}d.`; statusResult = null; } else if (cmd === 'Apply') { statusMessage = `Refresh SITE to verify ${options.name} was applied.`; statusResult = null; } else if (cmd === 'Update') { statusMessage = `Refresh Themes to verify ${options.name} was ${cmd}d.`; statusResult = null; } setState({ statusMessage: statusMessage, statusResult: statusResult, messageRefresh: makeid(5), }); return errMess; } //# sourceMappingURL=ReactComponent.js.map