-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboard): Users domain (#6212)
- Loading branch information
1 parent
7d5a6f8
commit 1100c21
Showing
16 changed files
with
864 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/admin-next/dashboard/src/routes/users/user-detail/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { userLoader as loader } from "./loader" | ||
export { UserDetail as Component } from "./user-detail" |
21 changes: 21 additions & 0 deletions
21
packages/admin-next/dashboard/src/routes/users/user-detail/loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { AdminUserRes } from "@medusajs/medusa" | ||
import { Response } from "@medusajs/medusa-js" | ||
import { adminProductKeys } from "medusa-react" | ||
import { LoaderFunctionArgs } from "react-router-dom" | ||
|
||
import { medusa, queryClient } from "../../../lib/medusa" | ||
|
||
const userDetailQuery = (id: string) => ({ | ||
queryKey: adminProductKeys.detail(id), | ||
queryFn: async () => medusa.admin.users.retrieve(id), | ||
}) | ||
|
||
export const userLoader = async ({ params }: LoaderFunctionArgs) => { | ||
const id = params.id | ||
const query = userDetailQuery(id!) | ||
|
||
return ( | ||
queryClient.getQueryData<Response<AdminUserRes>>(query.queryKey) ?? | ||
(await queryClient.fetchQuery(query)) | ||
) | ||
} |
9 changes: 7 additions & 2 deletions
9
packages/admin-next/dashboard/src/routes/users/user-detail/user-detail.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...in-next/dashboard/src/routes/users/user-edit/components/edit-user-form/edit-user-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { zodResolver } from "@hookform/resolvers/zod" | ||
import { User } from "@medusajs/medusa" | ||
import { Button, Drawer, Input } from "@medusajs/ui" | ||
import { useAdminUpdateUser } from "medusa-react" | ||
import { useEffect } from "react" | ||
import { useForm } from "react-hook-form" | ||
import { useTranslation } from "react-i18next" | ||
import * as zod from "zod" | ||
import { Form } from "../../../../../components/common/form" | ||
|
||
type EditUserFormProps = { | ||
user: Omit<User, "password_hash"> | ||
subscribe: (state: boolean) => void | ||
onSuccessfulSubmit: () => void | ||
} | ||
|
||
const EditUserFormSchema = zod.object({ | ||
first_name: zod.string().optional(), | ||
last_name: zod.string().optional(), | ||
}) | ||
|
||
export const EditUserForm = ({ | ||
user, | ||
subscribe, | ||
onSuccessfulSubmit, | ||
}: EditUserFormProps) => { | ||
const form = useForm<zod.infer<typeof EditUserFormSchema>>({ | ||
defaultValues: { | ||
first_name: user.first_name || "", | ||
last_name: user.last_name || "", | ||
}, | ||
resolver: zodResolver(EditUserFormSchema), | ||
}) | ||
|
||
const { | ||
formState: { isDirty }, | ||
} = form | ||
|
||
useEffect(() => { | ||
subscribe(isDirty) | ||
}, [isDirty]) | ||
|
||
const { t } = useTranslation() | ||
|
||
const { mutateAsync, isLoading } = useAdminUpdateUser(user.id) | ||
|
||
const handleSubmit = form.handleSubmit(async (values) => { | ||
await mutateAsync(values, { | ||
onSuccess: () => { | ||
onSuccessfulSubmit() | ||
}, | ||
}) | ||
}) | ||
|
||
return ( | ||
<Form {...form}> | ||
<form | ||
onSubmit={handleSubmit} | ||
className="flex flex-col overflow-hidden flex-1" | ||
> | ||
<Drawer.Body className="flex flex-col gap-y-8 overflow-y-auto flex-1 max-w-full"> | ||
<Form.Field | ||
control={form.control} | ||
name="first_name" | ||
render={({ field }) => { | ||
return ( | ||
<Form.Item> | ||
<Form.Label>{t("fields.firstName")}</Form.Label> | ||
<Form.Control> | ||
<Input {...field} /> | ||
</Form.Control> | ||
<Form.ErrorMessage /> | ||
</Form.Item> | ||
) | ||
}} | ||
/> | ||
<Form.Field | ||
control={form.control} | ||
name="last_name" | ||
render={({ field }) => { | ||
return ( | ||
<Form.Item> | ||
<Form.Label>{t("fields.lastName")}</Form.Label> | ||
<Form.Control> | ||
<Input {...field} /> | ||
</Form.Control> | ||
<Form.ErrorMessage /> | ||
</Form.Item> | ||
) | ||
}} | ||
/> | ||
</Drawer.Body> | ||
<Drawer.Footer> | ||
<div className="flex items-center justify-end gap-x-2"> | ||
<Drawer.Close asChild> | ||
<Button size="small" variant="secondary"> | ||
{t("general.cancel")} | ||
</Button> | ||
</Drawer.Close> | ||
<Button size="small" type="submit" isLoading={isLoading}> | ||
{t("general.save")} | ||
</Button> | ||
</div> | ||
</Drawer.Footer> | ||
</form> | ||
</Form> | ||
) | ||
} |
Oops, something went wrong.