-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WEB: Discard changes working fine when switching tab of app settings #312
Changes from 4 commits
0f7950f
384a3c1
f5bd8ea
4034a10
d94539f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||
import { useCallback } from 'react'; | ||||||
import useCustomSwr from '~/root/lib/client/hooks/use-custom-swr'; | ||||||
import { useConsoleApi } from '../server/gql/api-provider'; | ||||||
|
||||||
export const useIsOwner = ({ accountName }: { accountName: string }) => { | ||||||
const api = useConsoleApi(); | ||||||
const { data: teamMembers, isLoading: teamMembersLoading } = useCustomSwr( | ||||||
`${accountName}-owners`, | ||||||
async () => { | ||||||
return api.listMembershipsForAccount({ | ||||||
accountName, | ||||||
}); | ||||||
} | ||||||
); | ||||||
|
||||||
const { data: currentUser, isLoading: currentUserLoading } = useCustomSwr( | ||||||
'current-user', | ||||||
async () => { | ||||||
return api.whoAmI(); | ||||||
} | ||||||
); | ||||||
|
||||||
const isOwner = useCallback(() => { | ||||||
if (!teamMembers || !currentUser) return false; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): Use block braces for ifs, whiles, etc. (
Suggested change
ExplanationIt is recommended to always use braces and create explicit statement blocks.Using the allowed syntax to just write a single statement can lead to very confusing |
||||||
|
||||||
const owner = teamMembers.find((member) => member.role === 'account_owner'); | ||||||
|
||||||
return owner?.user?.email === currentUser?.email; | ||||||
}, [teamMembers, currentUser]); | ||||||
|
||||||
return { | ||||||
isOwner: isOwner(), | ||||||
isLoading: teamMembersLoading || currentUserLoading, | ||||||
}; | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,10 @@ import NoResultsFound from '~/console/components/no-results-found'; | |
import { IShowDialog } from '~/console/components/types.d'; | ||
import { useAppState } from '~/console/page-components/app-states'; | ||
import useForm from '~/root/lib/client/hooks/use-form'; | ||
import { | ||
DISCARD_ACTIONS, | ||
useUnsavedChanges, | ||
} from '~/root/lib/client/hooks/use-unsaved-changes'; | ||
import Yup from '~/root/lib/server/helpers/yup'; | ||
import { NonNullableString } from '~/root/lib/types/common'; | ||
import AppDialog from './app-dialogs'; | ||
|
@@ -205,9 +209,11 @@ const EnvironmentVariablesList = ({ | |
}; | ||
|
||
export const EnvironmentVariables = () => { | ||
const { setContainer, getContainer } = useAppState(); | ||
const { setContainer, getContainer, getReadOnlyContainer, readOnlyApp } = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider refactoring the component to reduce complexity and improve code organization. The changes have introduced unnecessary complexity to this component. While the new functionality is valuable, it can be implemented more efficiently. Consider the following improvements:
const { values, setValues, submit, reset } = useForm({
initialValues: getReadOnlyContainer().env || [],
validationSchema: Yup.array(entry),
onSubmit: (val) => {
setContainer((c) => ({ ...c, env: val }));
},
});
useEffect(() => {
submit();
}, [values]);
useEffect(() => {
if (performAction === DISCARD_ACTIONS.DISCARD_CHANGES) {
reset();
}
}, [performAction, reset]);
useEffect(() => {
reset();
}, [readOnlyApp, reset]); These changes will maintain the new functionality while significantly reducing the complexity of the component. The form handling becomes more straightforward, and the concerns of managing unsaved changes are separated from the direct form manipulation. |
||
useAppState(); | ||
|
||
const [showCSDialog, setShowCSDialog] = useState<IShowDialog>(null); | ||
const { performAction } = useUnsavedChanges(); | ||
|
||
const entry = Yup.object({ | ||
type: Yup.string().oneOf(['config', 'secret']).notRequired(), | ||
|
@@ -231,10 +237,16 @@ export const EnvironmentVariables = () => { | |
.notRequired(), | ||
}); | ||
|
||
const { values, setValues, submit } = useForm({ | ||
initialValues: getContainer().env, | ||
const { | ||
values, | ||
setValues, | ||
submit, | ||
resetValues: reset, | ||
} = useForm({ | ||
initialValues: getReadOnlyContainer().env || null, | ||
validationSchema: Yup.array(entry), | ||
onSubmit: (val) => { | ||
// @ts-ignore | ||
setContainer((c) => ({ | ||
...c, | ||
env: val, | ||
|
@@ -334,6 +346,25 @@ export const EnvironmentVariables = () => { | |
}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (performAction === DISCARD_ACTIONS.DISCARD_CHANGES) { | ||
// if (app.ciBuildId) { | ||
// setIsEdited(false); | ||
// } | ||
reset(); | ||
// @ts-ignore | ||
// setBuildData(readOnlyApp?.build); | ||
} | ||
|
||
// else if (performAction === 'init') { | ||
// setIsEdited(false); | ||
// } | ||
}, [performAction]); | ||
|
||
useEffect(() => { | ||
reset(); | ||
}, [readOnlyApp]); | ||
|
||
return ( | ||
<> | ||
<form | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Add error handling to API calls in useIsOwner hook
Consider adding error handling for the API calls in this custom hook. This will make the hook more robust and provide better feedback in case of network issues or API errors.