From c2f5f19f55dc42c8ab8ca1c3610c13c05167bdcf Mon Sep 17 00:00:00 2001 From: lemmy mwaura Date: Sun, 11 Dec 2022 16:59:32 +0300 Subject: [PATCH] refactor: instead of the index and we pass the keypair id --- frontend/pages/dashboard/[id].js | 47 ++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/frontend/pages/dashboard/[id].js b/frontend/pages/dashboard/[id].js index 64befe105..495ff7c04 100644 --- a/frontend/pages/dashboard/[id].js +++ b/frontend/pages/dashboard/[id].js @@ -45,7 +45,7 @@ import getWorkspaces from "../api/workspace/getWorkspaces"; /** * This component represent a single row for an environemnt variable on the dashboard * @param {object} obj - * @param {String[]} obj.keyPair - data related to the environment variable (index, key, value, public/private) + * @param {String[]} obj.keyPair - data related to the environment variable (id, key, value, public/private) * @param {function} obj.deleteRow - a function to delete a certain keyPair * @param {function} obj.modifyKey - modify the key of a certain environment variable * @param {function} obj.modifyValue - modify the value of a certain environment variable @@ -73,7 +73,7 @@ const KeyPair = ({ @@ -84,7 +84,7 @@ const KeyPair = ({ @@ -115,7 +115,7 @@ const KeyPair = ({ onClick={() => modifyVisibility( keyPair.type == "personal" ? "shared" : "personal", - keyPair[1] + keyPair.id ) } className="relative flex justify-start items-center cursor-pointer select-none py-2 px-2 rounded-md text-gray-400 hover:bg-white/10 duration-200 hover:text-gray-200 w-full" @@ -139,7 +139,7 @@ const KeyPair = ({ [...Array(randomStringLength)] .map(() => Math.floor(Math.random() * 16).toString(16)) .join(""), - keyPair[1] + keyPair.id ); } }} @@ -147,7 +147,7 @@ const KeyPair = ({ >

Generate Random Hex

@@ -269,6 +269,7 @@ export default function Dashboard() { setSortMethod( sortMethod == "alphabetical" ? "-alphabetical" : "alphabetical" ); + sortValuesHandler() }; useEffect(() => { @@ -397,8 +398,9 @@ export default function Dashboard() { }); } - // Once "Save changed is clicked", disable that button + // Once "Save changed is clicked", disable that button and sort values setButtonReady(false); + sortValuesHandler() pushKeys({ obj, workspaceId: router.query.id, env }); /** @@ -440,6 +442,21 @@ export default function Dashboard() { setBlurred(!blurred); }; + const sortValuesHandler = () => { + /** + * Since react's SetStateActionHandler optimises renders when values don't change + * we have to map and return a new sorted list to force a render. + * @returns {sorted list} + */ + + let sortedData = data.sort((a, b) => + sortMethod == "alphabetical" + ? a.key.localeCompare(b.key) + : b.key.localeCompare(a.key) + ).map((item) => item) + setData(sortedData) + } + // This function downloads the secrets as a .env file const download = () => { const file = data.map((item) => [item.key, item.value].join("=")).join("\n"); @@ -652,13 +669,7 @@ export default function Dashboard() { .toLowerCase() .includes(searchKeys.toLowerCase()) && keyPair.type == "personal" - ) - .sort((a, b) => - sortMethod == "alphabetical" - ? a.key.localeCompare(b.key) - : b.key.localeCompare(a.key) - ) - ?.map((keyPair, index) => ( + )?.map((keyPair) => ( - sortMethod == "alphabetical" - ? a.key.localeCompare(b.key) - : b.key.localeCompare(a.key) - ) - ?.map((keyPair, index) => ( + )?.map((keyPair) => (