refactor: instead of the index and we pass the keypair id

This commit is contained in:
lemmy mwaura
2022-12-11 16:59:32 +03:00
parent 968af64ee7
commit c2f5f19f55
+26 -21
View File
@@ -45,7 +45,7 @@ import getWorkspaces from "../api/workspace/getWorkspaces";
/** /**
* This component represent a single row for an environemnt variable on the dashboard * This component represent a single row for an environemnt variable on the dashboard
* @param {object} obj * @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.deleteRow - a function to delete a certain keyPair
* @param {function} obj.modifyKey - modify the key of a certain environment variable * @param {function} obj.modifyKey - modify the key of a certain environment variable
* @param {function} obj.modifyValue - modify the value of a certain environment variable * @param {function} obj.modifyValue - modify the value of a certain environment variable
@@ -73,7 +73,7 @@ const KeyPair = ({
<DashboardInputField <DashboardInputField
onChangeHandler={modifyKey} onChangeHandler={modifyKey}
type="varName" type="varName"
index={keyPair[1]} id={keyPair.id}
value={keyPair.key} value={keyPair.key}
duplicates={duplicates} duplicates={duplicates}
/> />
@@ -84,7 +84,7 @@ const KeyPair = ({
<DashboardInputField <DashboardInputField
onChangeHandler={modifyValue} onChangeHandler={modifyValue}
type="value" type="value"
index={keyPair[1]} id={keyPair.id}
value={keyPair.value} value={keyPair.value}
blurred={isBlurred} blurred={isBlurred}
/> />
@@ -115,7 +115,7 @@ const KeyPair = ({
onClick={() => onClick={() =>
modifyVisibility( modifyVisibility(
keyPair.type == "personal" ? "shared" : "personal", 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" 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)] [...Array(randomStringLength)]
.map(() => Math.floor(Math.random() * 16).toString(16)) .map(() => Math.floor(Math.random() * 16).toString(16))
.join(""), .join(""),
keyPair[1] keyPair.id
); );
} }
}} }}
@@ -147,7 +147,7 @@ const KeyPair = ({
> >
<FontAwesomeIcon <FontAwesomeIcon
className="text-lg pl-1.5 pr-3" className="text-lg pl-1.5 pr-3"
icon={keyPair[3] == "" ? faPlus : faShuffle} icon={keyPair.value == "" ? faPlus : faShuffle}
/> />
<div className="text-sm justify-between flex flex-row w-full"> <div className="text-sm justify-between flex flex-row w-full">
<p>Generate Random Hex</p> <p>Generate Random Hex</p>
@@ -269,6 +269,7 @@ export default function Dashboard() {
setSortMethod( setSortMethod(
sortMethod == "alphabetical" ? "-alphabetical" : "alphabetical" sortMethod == "alphabetical" ? "-alphabetical" : "alphabetical"
); );
sortValuesHandler()
}; };
useEffect(() => { 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); setButtonReady(false);
sortValuesHandler()
pushKeys({ obj, workspaceId: router.query.id, env }); pushKeys({ obj, workspaceId: router.query.id, env });
/** /**
@@ -440,6 +442,21 @@ export default function Dashboard() {
setBlurred(!blurred); 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 // This function downloads the secrets as a .env file
const download = () => { const download = () => {
const file = data.map((item) => [item.key, item.value].join("=")).join("\n"); const file = data.map((item) => [item.key, item.value].join("=")).join("\n");
@@ -652,13 +669,7 @@ export default function Dashboard() {
.toLowerCase() .toLowerCase()
.includes(searchKeys.toLowerCase()) && .includes(searchKeys.toLowerCase()) &&
keyPair.type == "personal" keyPair.type == "personal"
) )?.map((keyPair) => (
.sort((a, b) =>
sortMethod == "alphabetical"
? a.key.localeCompare(b.key)
: b.key.localeCompare(a.key)
)
?.map((keyPair, index) => (
<KeyPair <KeyPair
key={keyPair.id} key={keyPair.id}
keyPair={keyPair} keyPair={keyPair}
@@ -706,13 +717,7 @@ export default function Dashboard() {
.toLowerCase() .toLowerCase()
.includes(searchKeys.toLowerCase()) && .includes(searchKeys.toLowerCase()) &&
keyPair.type == "shared" keyPair.type == "shared"
) )?.map((keyPair) => (
.sort((a, b) =>
sortMethod == "alphabetical"
? a.key.localeCompare(b.key)
: b.key.localeCompare(a.key)
)
?.map((keyPair, index) => (
<KeyPair <KeyPair
key={keyPair.id} key={keyPair.id}
keyPair={keyPair} keyPair={keyPair}