PromucFlow_constructor/app/client/src/utils/storage.ts
Pawan Kumar 960159eef3
Grouping widgets into container (#5704)
* Cut copy paste first cut

* removed different parent groups logic

* mouseup on the outer canvas removes selections.

* bug fix

* remove unwanted dead code.

* Adding tests

* build fix

* min height fixes

* fixing specs.

* fixing specs.

* fix merge conflcits

* fix border positioning

* fix canvas widgets incorrect bouding box

* fix bounding box position issue

* fix bounding box position issue

* fix

* border issue fix

* update test case

* add colors in theme

* use layers + use click capture on actions

* add icon for grouping

* fix overflow issue in contextmenu in containers

* fix context menu display issue

* update position of context menu

* fix container box-shadow issue

* fix container box-shadow issue

* revert container box shadow

* stop opening of property pane on shift clicking a widget

* remove console.log

* fix multiselect box issue

* add container on copy

* add analytics middleware

* refactor paste widget saga

* change flash element to accept array + revert refactor

* add logic to create containers from selected widgets

* update positions of grouped widgets

* fix comments + remove console

* update flashElementbyId to flashElementsById

* remove analytics middleware + remove unecessary imports

* add shorcut for grouping

* fix position issue when pasting

* allow grouping only when multi widgets are selected

* fix ux issues with widget grouping

* fix help text for grouping actions

* filter out the modal widget when calculting next row

* fix delete issue when grouping

* persist positin when grouping if there is no collision

* fix typo for new position

* changes for review comments

* changes for review comments

* fix position issue when pasting

* fix new container position issue

* move utils function to utils

* fix import issue

* fix the composite widget grouping issue

* fix table name bug

* remove repeated code

* move copied groups existence check;

* fix copied group check

Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com>
Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain>
Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-08-25 10:30:31 +05:30

211 lines
5.7 KiB
TypeScript

import log from "loglevel";
import moment from "moment";
import localforage from "localforage";
const STORAGE_KEYS: { [id: string]: string } = {
AUTH_EXPIRATION: "Auth.expiration",
ROUTE_BEFORE_LOGIN: "RedirectPath",
COPIED_WIDGET: "CopiedWidget",
GROUP_COPIED_WIDGETS: "groupCopiedWidgets",
DELETED_WIDGET_PREFIX: "DeletedWidget-",
ONBOARDING_STATE: "OnboardingState",
ONBOARDING_WELCOME_STATE: "OnboardingWelcomeState",
RECENT_ENTITIES: "RecentEntities",
COMMENTS_INTRO_SEEN: "CommentsIntroSeen",
ONBOARDING_FORM_IN_PROGRESS: "ONBOARDING_FORM_IN_PROGRESS",
};
const store = localforage.createInstance({
name: "Appsmith",
});
export const resetAuthExpiration = () => {
const expireBy = moment()
.add(1, "h")
.format();
store.setItem(STORAGE_KEYS.AUTH_EXPIRATION, expireBy).catch((error) => {
log.error("Unable to set expiration time");
log.error(error);
});
};
export const hasAuthExpired = async () => {
const expireBy: string | null = await store.getItem(
STORAGE_KEYS.AUTH_EXPIRATION,
);
if (expireBy && moment().isAfter(moment(expireBy))) {
return true;
}
return false;
};
export const saveCopiedWidgets = async (widgetJSON: string) => {
try {
await store.setItem(STORAGE_KEYS.COPIED_WIDGET, widgetJSON);
return true;
} catch (error) {
log.error("An error occurred when storing copied widget: ", error);
return false;
}
};
export const getCopiedWidgets = async () => {
try {
const widget: string | null = await store.getItem(
STORAGE_KEYS.COPIED_WIDGET,
);
if (widget && widget.length > 0) {
return JSON.parse(widget);
}
} catch (error) {
log.error("An error occurred when fetching copied widget: ", error);
return;
}
};
export const saveDeletedWidgets = async (widgets: any, widgetId: string) => {
try {
await store.setItem(
`${STORAGE_KEYS.DELETED_WIDGET_PREFIX}${widgetId}`,
JSON.stringify(widgets),
);
return true;
} catch (error) {
log.error(
"An error occurred when temporarily storing delete widget: ",
error,
);
return false;
}
};
export const getDeletedWidgets = async (widgetId: string) => {
try {
const widgets: string | null = await store.getItem(
`${STORAGE_KEYS.DELETED_WIDGET_PREFIX}${widgetId}`,
);
if (widgets && widgets.length > 0) {
return JSON.parse(widgets);
}
} catch (error) {
log.error("An error occurred when fetching deleted widget: ", error);
}
};
export const flushDeletedWidgets = async (widgetId: string) => {
try {
await store.removeItem(`${STORAGE_KEYS.DELETED_WIDGET_PREFIX}${widgetId}`);
} catch (error) {
log.error("An error occurred when flushing deleted widgets: ", error);
}
};
export const setOnboardingState = async (onboardingState: boolean) => {
try {
await store.setItem(STORAGE_KEYS.ONBOARDING_STATE, onboardingState);
return true;
} catch (error) {
log.error("An error occurred when setting onboarding state: ", error);
return false;
}
};
export const getOnboardingState = async () => {
try {
const onboardingState = await store.getItem(STORAGE_KEYS.ONBOARDING_STATE);
return onboardingState;
} catch (error) {
log.error("An error occurred when getting onboarding state: ", error);
}
};
export const setOnboardingWelcomeState = async (onboardingState: boolean) => {
try {
await store.setItem(STORAGE_KEYS.ONBOARDING_WELCOME_STATE, onboardingState);
return true;
} catch (error) {
log.error("An error occurred when setting onboarding welcome state: ");
log.error(error);
return false;
}
};
export const getOnboardingWelcomeState = async () => {
try {
const onboardingState = await store.getItem(
STORAGE_KEYS.ONBOARDING_WELCOME_STATE,
);
return onboardingState;
} catch (error) {
log.error("An error occurred when getting onboarding welcome state: ");
log.error(error);
}
};
export const setRecentAppEntities = async (entities: any, appId: string) => {
try {
const recentEntities =
((await store.getItem(STORAGE_KEYS.RECENT_ENTITIES)) as Record<
string,
any
>) || {};
recentEntities[appId] = entities;
await store.setItem(STORAGE_KEYS.RECENT_ENTITIES, recentEntities);
} catch (error) {
log.error("An error occurred while saving recent entities");
log.error(error);
}
};
export const fetchRecentAppEntities = async (appId: string) => {
try {
const recentEntities = (await store.getItem(
STORAGE_KEYS.RECENT_ENTITIES,
)) as Record<string, any>;
return (recentEntities && recentEntities[appId]) || [];
} catch (error) {
log.error("An error occurred while fetching recent entities");
log.error(error);
}
};
export const deleteRecentAppEntities = async (appId: string) => {
try {
const recentEntities =
((await store.getItem(STORAGE_KEYS.RECENT_ENTITIES)) as Record<
string,
any
>) || {};
if (typeof recentEntities === "object") {
delete recentEntities[appId];
}
await store.setItem(STORAGE_KEYS.RECENT_ENTITIES, recentEntities);
} catch (error) {
log.error("An error occurred while saving recent entities");
log.error(error);
}
};
export const setCommentsIntroSeen = async (flag: boolean) => {
try {
await store.setItem(STORAGE_KEYS.COMMENTS_INTRO_SEEN, flag);
return true;
} catch (error) {
log.error("An error occurred when setting COMMENTS_INTRO_SEEN");
log.error(error);
return false;
}
};
export const getCommentsIntroSeen = async () => {
try {
const commentsIntroSeen = (await store.getItem(
STORAGE_KEYS.COMMENTS_INTRO_SEEN,
)) as boolean;
return commentsIntroSeen;
} catch (error) {
log.error("An error occurred while fetching COMMENTS_INTRO_SEEN");
log.error(error);
}
};