2022-02-17 04:31:59 +00:00
|
|
|
import { put, select, take } from "redux-saga/effects";
|
2021-08-27 09:25:28 +00:00
|
|
|
import { getAppStoreName } from "constants/AppConstants";
|
|
|
|
|
import localStorage from "utils/localStorage";
|
|
|
|
|
import {
|
|
|
|
|
updateAppPersistentStore,
|
|
|
|
|
updateAppTransientStore,
|
|
|
|
|
} from "actions/pageActions";
|
|
|
|
|
import AppsmithConsole from "utils/AppsmithConsole";
|
|
|
|
|
import { getAppStoreData } from "selectors/entitiesSelector";
|
2022-11-01 09:26:13 +00:00
|
|
|
import {
|
|
|
|
|
RemoveValueActionDescription,
|
|
|
|
|
StoreValueActionDescription,
|
2022-12-22 06:34:28 +00:00
|
|
|
} from "@appsmith/entities/DataTree/actionTriggers";
|
2021-10-18 14:03:44 +00:00
|
|
|
import { getCurrentGitBranch } from "selectors/gitSyncSelectors";
|
|
|
|
|
import { getCurrentApplicationId } from "selectors/editorSelectors";
|
2022-06-21 13:57:34 +00:00
|
|
|
import { AppStoreState } from "reducers/entityReducers/appReducer";
|
2022-04-12 10:50:01 +00:00
|
|
|
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
|
2021-08-27 09:25:28 +00:00
|
|
|
|
|
|
|
|
export default function* storeValueLocally(
|
|
|
|
|
action: StoreValueActionDescription["payload"],
|
|
|
|
|
) {
|
|
|
|
|
if (action.persist) {
|
2022-06-21 13:57:34 +00:00
|
|
|
const applicationId: string = yield select(getCurrentApplicationId);
|
|
|
|
|
const branch: string | undefined = yield select(getCurrentGitBranch);
|
2021-10-18 14:03:44 +00:00
|
|
|
const appStoreName = getAppStoreName(applicationId, branch);
|
2021-08-27 09:25:28 +00:00
|
|
|
const existingStore = localStorage.getItem(appStoreName) || "{}";
|
|
|
|
|
const parsedStore = JSON.parse(existingStore);
|
|
|
|
|
parsedStore[action.key] = action.value;
|
|
|
|
|
const storeString = JSON.stringify(parsedStore);
|
|
|
|
|
localStorage.setItem(appStoreName, storeString);
|
2022-06-20 08:05:14 +00:00
|
|
|
yield put(updateAppPersistentStore(parsedStore, action));
|
2021-08-27 09:25:28 +00:00
|
|
|
AppsmithConsole.info({
|
|
|
|
|
text: `store('${action.key}', '${action.value}', true)`,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2022-06-21 13:57:34 +00:00
|
|
|
const existingStore: AppStoreState = yield select(getAppStoreData);
|
2021-08-27 09:25:28 +00:00
|
|
|
const newTransientStore = {
|
|
|
|
|
...existingStore.transient,
|
|
|
|
|
[action.key]: action.value,
|
|
|
|
|
};
|
2022-06-20 08:05:14 +00:00
|
|
|
yield put(updateAppTransientStore(newTransientStore, action));
|
2021-08-27 09:25:28 +00:00
|
|
|
AppsmithConsole.info({
|
|
|
|
|
text: `store('${action.key}', '${action.value}', false)`,
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-06-20 08:05:14 +00:00
|
|
|
/* It is possible that user calls multiple storeValue function together, in such case we need to track completion of each action separately
|
|
|
|
|
We use uniqueActionRequestId to differentiate each storeValueAction here.
|
|
|
|
|
*/
|
|
|
|
|
while (true) {
|
|
|
|
|
const returnedAction: StoreValueActionDescription = yield take(
|
|
|
|
|
ReduxActionTypes.UPDATE_APP_STORE_EVALUATED,
|
|
|
|
|
);
|
|
|
|
|
if (!returnedAction?.payload?.uniqueActionRequestId) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { uniqueActionRequestId } = returnedAction.payload;
|
|
|
|
|
if (uniqueActionRequestId === action.uniqueActionRequestId) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-27 09:25:28 +00:00
|
|
|
}
|
2022-11-01 09:26:13 +00:00
|
|
|
|
|
|
|
|
export function* removeLocalValue(
|
|
|
|
|
action: RemoveValueActionDescription["payload"],
|
|
|
|
|
) {
|
|
|
|
|
const applicationId: string = yield select(getCurrentApplicationId);
|
|
|
|
|
const branch: string | undefined = yield select(getCurrentGitBranch);
|
|
|
|
|
const appStoreName = getAppStoreName(applicationId, branch);
|
|
|
|
|
const existingStore = localStorage.getItem(appStoreName) || "{}";
|
|
|
|
|
const parsedStore = JSON.parse(existingStore);
|
|
|
|
|
delete parsedStore[action.key];
|
|
|
|
|
const storeString = JSON.stringify(parsedStore);
|
|
|
|
|
localStorage.setItem(appStoreName, storeString);
|
|
|
|
|
yield put(updateAppPersistentStore(parsedStore));
|
|
|
|
|
const existingTransientStore: AppStoreState = yield select(getAppStoreData);
|
|
|
|
|
delete existingTransientStore.transient?.[action.key];
|
|
|
|
|
yield put(updateAppTransientStore(existingTransientStore.transient));
|
|
|
|
|
AppsmithConsole.info({
|
|
|
|
|
text: `remove('${action.key}')`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* clearLocalStore() {
|
|
|
|
|
const applicationId: string = yield select(getCurrentApplicationId);
|
|
|
|
|
const branch: string | undefined = yield select(getCurrentGitBranch);
|
|
|
|
|
const appStoreName = getAppStoreName(applicationId, branch);
|
|
|
|
|
localStorage.setItem(appStoreName, "{}");
|
|
|
|
|
yield put(updateAppPersistentStore({}));
|
|
|
|
|
yield put(updateAppTransientStore({}));
|
|
|
|
|
AppsmithConsole.info({
|
|
|
|
|
text: `clear()`,
|
|
|
|
|
});
|
|
|
|
|
}
|