SkyeYoung commented on code in PR #3018:
URL: https://github.com/apache/apisix-dashboard/pull/3018#discussion_r2077124356


##########
src/routes/consumers/detail.$username/credentials/detail.$id.tsx:
##########
@@ -0,0 +1,127 @@
+import { createFileRoute, useParams } from '@tanstack/react-router';
+import { useTranslation } from 'react-i18next';
+import { useMutation, useSuspenseQuery } from '@tanstack/react-query';
+import PageHeader from '@/components/page/PageHeader';
+import { FormProvider, useForm } from 'react-hook-form';
+import { zodResolver } from '@hookform/resolvers/zod';
+import { FormSubmitBtn } from '@/components/form/Btn';
+import { notifications } from '@mantine/notifications';
+import { FormTOCBox } from '@/components/form-slice/FormSection';
+import { Skeleton, Button, Group } from '@mantine/core';
+import { useBoolean } from 'react-use';
+import { useEffect } from 'react';
+import { APISIX } from '@/types/schema/apisix';
+import {
+  getCredentialQueryOptions,
+  putCredentialReq,
+} from '@/apis/credentials';
+import { FormPartCredential } from 
'@/components/form-slice/FormPartCredential';
+import { pipeProduce } from '@/utils/producer';
+import { DetailCredentialsTabs } from 
'@/components/page-slice/consumers/DetailCredentialsTabs';
+
+type CredentialFormProps = {
+  readOnly: boolean;
+  setReadOnly: (v: boolean) => void;
+};
+
+const CredentialDetailForm = (props: CredentialFormProps) => {
+  const { readOnly, setReadOnly } = props;
+  const { t } = useTranslation();
+  const { username, id } = useParams({
+    from: '/consumers/detail/$username/credentials/detail/$id',
+  });
+
+  const {
+    data: credentialData,
+    isLoading,
+    refetch,
+  } = useSuspenseQuery(getCredentialQueryOptions(username, id));
+
+  const form = useForm({
+    resolver: zodResolver(APISIX.CredentialPut),
+    shouldUnregister: true,
+    shouldFocusError: true,
+    mode: 'all',
+    disabled: readOnly,
+  });
+
+  useEffect(() => {
+    if (credentialData?.value && !isLoading) {
+      form.reset(credentialData.value);
+    }
+  }, [credentialData, form, isLoading]);
+
+  const putCredential = useMutation({
+    mutationFn: putCredentialReq,
+    async onSuccess() {
+      notifications.show({
+        message: t('consumers.credentials.edit.success'),
+        color: 'green',
+      });
+      await refetch();
+      setReadOnly(true);
+    },
+  });
+
+  if (isLoading) {
+    return <Skeleton height={400} />;
+  }
+
+  return (
+    <FormProvider {...form}>
+      <form
+        onSubmit={form.handleSubmit((d) => {
+          putCredential.mutateAsync({
+            username,
+            ...pipeProduce()(d),
+          });
+        })}
+      >
+        <FormPartCredential showDate />
+        {!readOnly && (
+          <Group>
+            <FormSubmitBtn>{t('form.btn.save')}</FormSubmitBtn>
+            <Button variant="outline" onClick={() => setReadOnly(true)}>
+              {t('form.btn.cancel')}
+            </Button>
+          </Group>
+        )}
+      </form>
+    </FormProvider>
+  );
+};
+
+function RouteComponent() {
+  const { t } = useTranslation();
+  const [readOnly, setReadOnly] = useBoolean(true);
+
+  return (
+    <>
+      <DetailCredentialsTabs />
+      <PageHeader
+        title={t('consumers.credentials.edit.title')}
+        {...(readOnly && {
+          title: t('consumers.credentials.detail.title'),
+          extra: (
+            <Button
+              onClick={() => setReadOnly(false)}
+              size="compact-sm"
+              variant="gradient"
+            >
+              {t('form.btn.edit')}
+            </Button>
+          ),
+        })}
+      />
+      <FormTOCBox>
+        <CredentialDetailForm readOnly={readOnly} setReadOnly={setReadOnly} />
+      </FormTOCBox>
+    </>

Review Comment:
   Users can directly click on the Credentials tab at the top



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@apisix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to