Skip to content

Commit

Permalink
Allow user to edit Dataset Name (#3207)
Browse files Browse the repository at this point in the history
* PBENCH-1017

Allow user to change dataset.name
Modify the name of the dataset using PUT /datasets/metadata API
  • Loading branch information
MVarshini authored Feb 8, 2023
1 parent 0a04c49 commit 606a7d5
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 169 deletions.
5 changes: 3 additions & 2 deletions dashboard/src/actions/authActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as TYPES from "./types";

import API from "../utils/axiosInstance";
import Cookies from "js-cookie";
import { SUCCESS } from "assets/constants/overviewConstants";
import { showToast } from "actions/toastActions";
import { uid } from "../utils/helper";

Expand Down Expand Up @@ -44,7 +45,7 @@ export const makeLoginRequest =

navigate(APP_ROUTES.OVERVIEW);

dispatch(showToast("success", "Logged in successfully!"));
dispatch(showToast(SUCCESS, "Logged in successfully!"));
}
dispatch({ type: TYPES.COMPLETED });
} catch (error) {
Expand Down Expand Up @@ -102,7 +103,7 @@ export const registerUser =
...details,
});
if (response.status === 201) {
dispatch(showToast("success", "Account created!", "Login to continue"));
dispatch(showToast(SUCCESS, "Account created!", "Login to continue"));
navigate(APP_ROUTES.AUTH_LOGIN);
}
dispatch({ type: TYPES.COMPLETED });
Expand Down
154 changes: 117 additions & 37 deletions dashboard/src/actions/overviewActions.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import * as CONSTANTS from "assets/constants/overviewConstants";
import * as TYPES from "./types";

import {
DASHBOARD_LOAD_DELAY_MS,
DASHBOARD_SAVED,
DASHBOARD_SEEN,
DATASET_ACCESS,
DATASET_CREATED,
DATASET_OWNER,
EXPIRATION_DAYS_LIMIT,
SERVER_DELETION,
USER_FAVORITE,
} from "assets/constants/overviewConstants";

import API from "../utils/axiosInstance";
import { DANGER } from "assets/constants/toastConstants";
import { findNoOfDays } from "utils/dateFunctions";
import { showToast } from "./toastActions";

Expand All @@ -25,13 +15,13 @@ export const getDatasets = () => async (dispatch, getState) => {
dispatch({ type: TYPES.LOADING });
}
const params = new URLSearchParams();
params.append("metadata", DATASET_CREATED);
params.append("metadata", DATASET_OWNER);
params.append("metadata", DATASET_ACCESS);
params.append("metadata", SERVER_DELETION);
params.append("metadata", DASHBOARD_SAVED);
params.append("metadata", DASHBOARD_SEEN);
params.append("metadata", USER_FAVORITE);
params.append("metadata", CONSTANTS.DATASET_CREATED);
params.append("metadata", CONSTANTS.DATASET_OWNER);
params.append("metadata", CONSTANTS.DATASET_ACCESS);
params.append("metadata", CONSTANTS.SERVER_DELETION);
params.append("metadata", CONSTANTS.DASHBOARD_SAVED);
params.append("metadata", CONSTANTS.DASHBOARD_SEEN);
params.append("metadata", CONSTANTS.USER_FAVORITE);

params.append("owner", username);

Expand All @@ -52,7 +42,7 @@ export const getDatasets = () => async (dispatch, getState) => {
}
}
} catch (error) {
dispatch(showToast("danger", error?.response?.data?.message));
dispatch(showToast(DANGER, error?.response?.data?.message));
dispatch({ type: TYPES.NETWORK_ERROR });
}
if (alreadyRendered) {
Expand All @@ -64,14 +54,28 @@ export const getDatasets = () => async (dispatch, getState) => {

const initializeRuns = () => (dispatch, getState) => {
const data = getState().overview.datasets;
data.forEach((item) => {
item[CONSTANTS.IS_EDIT] = false;
item[CONSTANTS.NAME_COPY] = item.name;
item[CONSTANTS.IS_DIRTY] = false;
item[CONSTANTS.NAME_VALIDATED] = CONSTANTS.DEFAULT;
item[CONSTANTS.IS_ITEM_SEEN] = !!item?.metadata?.[CONSTANTS.DASHBOARD_SEEN];
item[CONSTANTS.IS_ITEM_FAVORITED] =
!!item?.metadata?.[CONSTANTS.USER_FAVORITE];
});
const defaultPerPage = getState().overview.defaultPerPage;

const savedRuns = data.filter((item) => item.metadata[DASHBOARD_SAVED]);
const newRuns = data.filter((item) => !item.metadata[DASHBOARD_SAVED]);
const savedRuns = data.filter(
(item) => item.metadata[CONSTANTS.DASHBOARD_SAVED]
);
const newRuns = data.filter(
(item) => !item.metadata[CONSTANTS.DASHBOARD_SAVED]
);

const expiringRuns = data.filter(
(item) =>
findNoOfDays(item.metadata["server.deletion"]) < EXPIRATION_DAYS_LIMIT
findNoOfDays(item.metadata[CONSTANTS.SERVER_DELETION]) <
CONSTANTS.EXPIRATION_DAYS_LIMIT
);
dispatch({
type: TYPES.EXPIRING_RUNS,
Expand All @@ -91,9 +95,10 @@ const initializeRuns = () => (dispatch, getState) => {
});
};
const metaDataActions = {
save: DASHBOARD_SAVED,
read: DASHBOARD_SEEN,
favorite: USER_FAVORITE,
save: CONSTANTS.DASHBOARD_SAVED,
read: CONSTANTS.DASHBOARD_SEEN,
favorite: CONSTANTS.USER_FAVORITE,
datasetName: CONSTANTS.DATASET_NAME,
};
/**
* Function which return a thunk to be passed to a Redux dispatch() call
Expand Down Expand Up @@ -132,15 +137,20 @@ export const updateDataset =
});
dispatch(initializeRuns());
} else {
dispatch(showToast("danger", response?.data?.message));
dispatch(showToast(DANGER, response?.data?.message));
}
} catch (error) {
dispatch(showToast("danger", error?.response?.data?.message));
dispatch(showToast(DANGER, error?.response?.data?.message));
dispatch({ type: TYPES.NETWORK_ERROR });
}
dispatch({ type: TYPES.COMPLETED });
};

/**
* Function to delete the dataset
* @function
* @param {Object} dataset - Dataset which is being updated *
* @return {Object} - dispatch the action and update the state
*/
export const deleteDataset = (dataset) => async (dispatch, getState) => {
try {
dispatch({ type: TYPES.LOADING });
Expand All @@ -161,10 +171,10 @@ export const deleteDataset = (dataset) => async (dispatch, getState) => {
});

dispatch(initializeRuns());
dispatch(showToast("success", "Deleted!"));
dispatch(showToast(CONSTANTS.SUCCESS, "Deleted!"));
}
} catch (error) {
dispatch(showToast("danger", error?.response?.data?.message));
dispatch(showToast(DANGER, error?.response?.data?.message));
dispatch({ type: TYPES.NETWORK_ERROR });
}
dispatch({ type: TYPES.COMPLETED });
Expand Down Expand Up @@ -200,13 +210,19 @@ export const updateMultipleDataset =
: method === "save"
? "Saved!"
: "Updated!";
dispatch(showToast("success", toastMsg));
dispatch(showToast(CONSTANTS.SUCCESS, toastMsg));
dispatch(setSelectedRuns([]));
} else {
dispatch(showToast("warning", "Select dataset(s) for update"));
}
};

/**
* Function to publish the dataset
* @function
* @param {Object} dataset - Dataset which is being updated
* @param {string} updateValue - Access type value (Public/Private)
* @return {Object} - dispatch the action and update the state
*/
export const publishDataset =
(dataset, updateValue) => async (dispatch, getState) => {
try {
Expand All @@ -221,16 +237,16 @@ export const publishDataset =
const dataIndex = savedRuns.findIndex(
(item) => item.resource_id === dataset.resource_id
);
savedRuns[dataIndex].metadata[DATASET_ACCESS] = updateValue;
savedRuns[dataIndex].metadata[CONSTANTS.DATASET_ACCESS] = updateValue;

dispatch({
type: TYPES.SAVED_RUNS,
payload: savedRuns,
});
dispatch(showToast("success", "Updated!"));
dispatch(showToast(CONSTANTS.SUCCESS, "Updated!"));
}
} catch (error) {
dispatch(showToast("danger", error?.response?.data?.message));
dispatch(showToast(DANGER, error?.response?.data?.message));
dispatch({ type: TYPES.NETWORK_ERROR });
}
dispatch({ type: TYPES.COMPLETED });
Expand All @@ -244,5 +260,69 @@ export const setLoadingDoneFlag = () => async (dispatch, getState) => {
sessionStorage.setItem("loadingDone", true);
dispatch({ type: TYPES.SET_LOADING_FLAG, payload: true });
}
}, DASHBOARD_LOAD_DELAY_MS);
}, CONSTANTS.DASHBOARD_LOAD_DELAY_MS);
};

const filterDatasetType = (type, getState) => {
return type === "newRuns"
? getState().overview.initNewRuns
: getState().overview.savedRuns;
};

const updateDatasetType = (data, type) => {
return {
type: type === "newRuns" ? TYPES.INIT_NEW_RUNS : TYPES.SAVED_RUNS,
payload: data,
};
};
/**
* Function to validate the edited dataset
* @function
* @param {string} value - new value of the metadata that is being edited
* @param {string} metadata - metadata that is being edited *
* @param {string} rId - resource_id of the dataset which is being set to edit
* @param {string} type - Type of the Dataset (Saved/New)
* @return {Object} - dispatch the action and update the state
*/
export const editMetadata =
(value, metadata, rId, type) => async (dispatch, getState) => {
const data = filterDatasetType(type, getState);

const rIndex = data.findIndex((item) => item.resource_id === rId);
data[rIndex][metadata] = value;
data[rIndex][CONSTANTS.IS_DIRTY] = true;
if (value.length > CONSTANTS.DATASET_NAME_LENGTH) {
data[rIndex][CONSTANTS.NAME_VALIDATED] = CONSTANTS.ERROR;
data[rIndex][
CONSTANTS.NAME_ERROR_MSG
] = `Length should be < ${CONSTANTS.DATASET_NAME_LENGTH}`;
} else if (value.length === 0) {
data[rIndex][CONSTANTS.NAME_VALIDATED] = CONSTANTS.ERROR;
data[rIndex][CONSTANTS.NAME_ERROR_MSG] = `Length cannot be 0`;
} else {
data[rIndex][CONSTANTS.NAME_VALIDATED] = CONSTANTS.SUCCESS;
}
dispatch(updateDatasetType(data, type));
};
/**
* Function which toggles the row of New runs or Saved runs Table to edit
* @function
* @param {string} rId - resource_id of the dataset which is being set to edit
* @param {boolean} isEdit - Set/not set to edit
* @param {string} type - Type of the Dataset (Saved/New)
* @return {Object} - dispatch the action and update the state
*/
export const setRowtoEdit =
(rId, isEdit, type) => async (dispatch, getState) => {
const data = filterDatasetType(type, getState);

const rIndex = data.findIndex((item) => item.resource_id === rId);
data[rIndex][CONSTANTS.IS_EDIT] = isEdit;

if (!isEdit) {
data[rIndex].name = data[rIndex][CONSTANTS.NAME_COPY];
data[rIndex][CONSTANTS.IS_DIRTY] = false;
data[rIndex][CONSTANTS.NAME_VALIDATED] = CONSTANTS.SUCCESS;
}
dispatch(updateDatasetType(data, type));
};
16 changes: 14 additions & 2 deletions dashboard/src/assets/constants/overviewConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ export const ROWS_PER_PAGE = 5;

export const DASHBOARD_SAVED = "global.dashboard.saved";
export const DASHBOARD_SEEN = "global.dashboard.seen";
export const DASHBOARD_LOAD_DELAY_MS = 1000;
export const DATASET_ACCESS = "dataset.access";
export const DATASET_CREATED = "dataset.created";
export const DATASET_NAME = "dataset.name";
export const DATASET_NAME_LENGTH = 1024;
export const DATASET_OWNER = "dataset.owner";
export const DEFAULT = "default";
export const ERROR = "error";
export const EXPIRATION_DAYS_LIMIT = 20;
export const IS_DIRTY = "isDirty";
export const IS_EDIT = "isEdit";
export const IS_ITEM_FAVORITED = "isItemFavorited";
export const IS_ITEM_SEEN = "isItemSeen";
export const NAME_COPY = "name_copy";
export const NAME_ERROR_MSG = "name_errorMsg";
export const NAME_VALIDATED = "name_validated";
export const SERVER_DELETION = "server.deletion";
export const SUCCESS = "success";
export const USER_FAVORITE = "user.dashboard.favorite";
export const EXPIRATION_DAYS_LIMIT = 20;
export const DASHBOARD_LOAD_DELAY_MS = 1000;
1 change: 1 addition & 0 deletions dashboard/src/assets/constants/toastConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DANGER = "danger";
Loading

0 comments on commit 606a7d5

Please sign in to comment.