-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Nv 2452 tenant create + update a tenant sidebar #3863
Merged
ainouzgali
merged 8 commits into
stacked-tenants-support
from
nv-2452-tenant-create-a-tenant-sidebar
Jul 31, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5cc66a0
feat: create and update tenant sidebar
ainouzgali 0e1d220
Merge remote-tracking branch 'origin/stacked-tenants-support' into nv…
ainouzgali 247ad91
feat: after pr comments
ainouzgali 19c17cf
Merge remote-tracking branch 'origin/stacked-tenants-support' into nv…
ainouzgali 6e60c64
fix: allow return 'data' as response prop
djabarovgeorge a124733
feat: add comment with for the new condition
djabarovgeorge c05230c
fix: typo
djabarovgeorge 4a7999e
Merge pull request #3876 from novuhq/fix-response-interceptor-to-retu…
djabarovgeorge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
import { ICreateTenantDto, IUpdateTenantDto } from '@novu/shared'; | ||
import { api } from './api.client'; | ||
|
||
export function getTenants({ page = 0, limit = 10 } = {}) { | ||
return api.getFullResponse('/v1/tenants', { page, limit }); | ||
} | ||
|
||
export function createTenant(data: ICreateTenantDto) { | ||
return api.post(`/v1/tenants`, data); | ||
} | ||
|
||
export function updateTenant(identifier: string, data: IUpdateTenantDto) { | ||
return api.patch(`/v1/tenants/${identifier}`, data); | ||
} | ||
|
||
export function getTenantByIdentifier(identifier: string) { | ||
return api.get(`/v1/tenants/${identifier}`); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { ROUTES } from '../../constants/routes.enum'; | ||
import { CreateTenantSidebar } from './components/CreateTenantSidebar'; | ||
|
||
export function CreateTenantPage() { | ||
const navigate = useNavigate(); | ||
|
||
const onClose = () => { | ||
navigate(ROUTES.TENANTS); | ||
}; | ||
|
||
const onTenantCreated = (identifier: string) => { | ||
navigate(`${ROUTES.TENANTS}/${identifier}`); | ||
}; | ||
|
||
return <CreateTenantSidebar isOpened onTenantCreated={onTenantCreated} onClose={onClose} />; | ||
} |
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,21 +1,32 @@ | ||
import React, { useCallback } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { Outlet, useNavigate } from 'react-router-dom'; | ||
import { Row } from 'react-table'; | ||
import { ITenantEntity } from '@novu/shared'; | ||
|
||
import PageContainer from '../../components/layout/components/PageContainer'; | ||
import PageHeader from '../../components/layout/components/PageHeader'; | ||
import { TenantsList } from './components/list/TenantsList'; | ||
import { ROUTES } from '../../constants/routes.enum'; | ||
|
||
export function TenantsPage() { | ||
const navigate = useNavigate(); | ||
|
||
const onAddTenantClickCallback = useCallback(() => { | ||
// navigate(); | ||
navigate(ROUTES.TENANTS_CREATE); | ||
}, [navigate]); | ||
|
||
const onRowClickCallback = useCallback( | ||
(item: Row<ITenantEntity>) => { | ||
navigate(`${ROUTES.TENANTS}/${item.original.identifier}`); | ||
}, | ||
[navigate] | ||
); | ||
|
||
return ( | ||
<PageContainer title="Tenants"> | ||
<PageHeader title="Tenants" /> | ||
<TenantsList onAddTenantClick={onAddTenantClickCallback} /> | ||
<TenantsList onAddTenantClick={onAddTenantClickCallback} onRowClickCallback={onRowClickCallback} /> | ||
<Outlet /> | ||
</PageContainer> | ||
); | ||
} |
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 { useNavigate, useParams } from 'react-router-dom'; | ||
|
||
import { ROUTES } from '../../constants/routes.enum'; | ||
import { UpdateTenantSidebar } from './components/UpdateTenantSidebar'; | ||
|
||
export function UpdateTenantPage() { | ||
const { identifier } = useParams(); | ||
ainouzgali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const navigate = useNavigate(); | ||
|
||
const onClose = () => { | ||
navigate(ROUTES.TENANTS); | ||
}; | ||
|
||
if (!identifier) { | ||
navigate(ROUTES.TENANTS); | ||
|
||
return null; | ||
} | ||
|
||
return <UpdateTenantSidebar isOpened onClose={onClose} tenantIdentifier={identifier} />; | ||
} |
129 changes: 129 additions & 0 deletions
129
apps/web/src/pages/tenants/components/CreateTenantSidebar.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,129 @@ | ||
import { useEffect } from 'react'; | ||
import { Group, Stack } from '@mantine/core'; | ||
import { useForm } from 'react-hook-form'; | ||
import { useQueryClient, useMutation } from '@tanstack/react-query'; | ||
import slugify from 'slugify'; | ||
|
||
import { ICreateTenantDto, ITenantEntity } from '@novu/shared'; | ||
|
||
import { Button, colors, Sidebar, Text, Title, Tooltip } from '../../../design-system'; | ||
import { createTenant } from '../../../api/tenants'; | ||
import { errorMessage, successMessage } from '../../../utils/notifications'; | ||
import { QueryKeys } from '../../../api/query.keys'; | ||
import { TenantFormCommonFields } from './TenantFormCommonFields'; | ||
import { defaultFormValues, ITenantForm } from './UpdateTenantSidebar'; | ||
|
||
export function CreateTenantSidebar({ | ||
isOpened, | ||
onTenantCreated, | ||
onClose, | ||
}: { | ||
isOpened: boolean; | ||
onTenantCreated: (identifier: string) => void; | ||
onClose: () => void; | ||
}) { | ||
const queryClient = useQueryClient(); | ||
|
||
const { mutateAsync: createTenantMutation, isLoading: isLoadingCreate } = useMutation< | ||
ITenantEntity, | ||
{ error: string; message: string; statusCode: number }, | ||
ICreateTenantDto | ||
>(createTenant, { | ||
onSuccess: async () => { | ||
await queryClient.refetchQueries({ | ||
predicate: ({ queryKey }) => queryKey.includes(QueryKeys.tenantsList), | ||
}); | ||
successMessage('New tenant has been created!'); | ||
}, | ||
onError: (e: any) => { | ||
errorMessage(e.message || 'Unexpected error'); | ||
}, | ||
}); | ||
|
||
const { | ||
handleSubmit, | ||
control, | ||
formState: { isValid, isDirty }, | ||
setValue, | ||
watch, | ||
} = useForm<ITenantForm>({ | ||
shouldUseNativeValidation: false, | ||
defaultValues: defaultFormValues, | ||
}); | ||
|
||
const name = watch('name'); | ||
const identifier = watch('identifier'); | ||
|
||
useEffect(() => { | ||
const newIdentifier = slugify(name, { | ||
lower: true, | ||
strict: true, | ||
}); | ||
|
||
if (newIdentifier === identifier) { | ||
return; | ||
} | ||
|
||
setValue('identifier', newIdentifier); | ||
}, [name]); | ||
|
||
const onCreateTenant = async (data) => { | ||
const { identifier: tenantIdentifier } = await createTenantMutation({ | ||
name: data.name, | ||
identifier: data.identifier, | ||
...(data.data ? { data: JSON.parse(data.data) } : {}), | ||
}); | ||
|
||
if (!tenantIdentifier) { | ||
onClose(); | ||
} else { | ||
onTenantCreated(tenantIdentifier); | ||
} | ||
}; | ||
|
||
return ( | ||
<Sidebar | ||
isOpened={isOpened} | ||
onClose={onClose} | ||
onSubmit={(e) => { | ||
handleSubmit(onCreateTenant)(e); | ||
e.stopPropagation(); | ||
}} | ||
customHeader={ | ||
<Stack h={80} spacing={8}> | ||
<Group h={40}> | ||
<Title size={2}>Create a tenant</Title> | ||
</Group> | ||
<Text color={colors.B40}> | ||
Tenants are isolated user scopes in your product, e.g., accounts or workspaces. | ||
</Text> | ||
</Stack> | ||
} | ||
customFooter={ | ||
<Group ml="auto"> | ||
<Button variant={'outline'} onClick={onClose} data-test-id="create-tenant-sidebar-cancel"> | ||
Cancel | ||
</Button> | ||
<Tooltip | ||
sx={{ position: 'absolute' }} | ||
disabled={isDirty} | ||
label={'Fill in the name and identifier to create the tenant'} | ||
> | ||
<span> | ||
<Button | ||
loading={isLoadingCreate} | ||
disabled={!isDirty || !isValid} | ||
submit | ||
data-test-id="create-tenant-sidebar-submit" | ||
> | ||
Create | ||
</Button> | ||
</span> | ||
</Tooltip> | ||
</Group> | ||
} | ||
> | ||
<TenantFormCommonFields control={control} /> | ||
</Sidebar> | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this one won't break anything?
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.
No, all inputs should be with border-radius of 7px. But usually it was just the default from mantine