Skip to content
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

Implement batch check for all workspace action #333

Merged
merged 5 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export const CreateOrganization = ({
});

const { client } = useFrontier();

async function onSubmit(data: any) {
if (!client) return;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,42 @@
'use client';

import { Button, DataTable, EmptyState, Flex, Text } from '@raystack/apsara';
import { Outlet, useNavigate, useRouterState } from '@tanstack/react-router';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { Outlet, useNavigate } from '@tanstack/react-router';
import { useMemo } from 'react';
import { useFrontier } from '~/react/contexts/FrontierContext';
import { useOrganizationDomains } from '~/react/hooks/useOrganizationDomains';
import { usePermissions } from '~/react/hooks/usePermissions';
import { V1Beta1Domain } from '~/src';
import { PERMISSIONS, shouldShowComponent } from '~/utils';
import { styles } from '../styles';
import { getColumns } from './domain.columns';

export default function Domain() {
const { client, activeOrganization: organization } = useFrontier();
const routerState = useRouterState();
const [domains, setDomains] = useState([]);
const [isDomainsLoading, setIsDomainsLoading] = useState(false);
const { isFetching, domains } = useOrganizationDomains();
const { activeOrganization: organization } = useFrontier();

const getDomains = useCallback(async () => {
try {
setIsDomainsLoading(true);
if (!organization?.id) return;
const {
// @ts-ignore
data: { domains = [] }
} = await client?.frontierServiceListOrganizationDomains(
organization?.id
);
setDomains(domains);
} catch (err) {
console.error(err);
} finally {
setIsDomainsLoading(false);
const resource = `app/organization:${organization?.id}`;
const listOfPermissionsToCheck = [
{
permission: PERMISSIONS.UpdatePermission,
resource
}
}, [client, organization?.id]);

useEffect(() => {
getDomains();
}, [getDomains, routerState.location.key]);
];

useEffect(() => {
getDomains();
}, [client, getDomains, organization?.id]);

const updatedDomains = useMemo(
() =>
isDomainsLoading
? [{ id: 1 }, { id: 2 }, { id: 3 }]
: domains.length
? domains
: [],
[isDomainsLoading, domains]
const { permissions } = usePermissions(
listOfPermissionsToCheck,
!!organization?.id
);

const { canCreateDomain } = useMemo(() => {
return {
canCreateDomain: shouldShowComponent(
permissions,
`${PERMISSIONS.UpdatePermission}::${resource}`
)
};
}, [permissions, resource]);

return (
<Flex direction="column" style={{ width: '100%' }}>
<Flex style={styles.header}>
Expand All @@ -59,7 +46,11 @@ export default function Domain() {
<Flex direction="column" style={{ gap: '24px' }}>
<AllowedEmailDomains />
{/* @ts-ignore */}
<Domains domains={updatedDomains} isLoading={isDomainsLoading} />
<Domains
domains={domains}
isLoading={isFetching}
canCreateDomain={canCreateDomain}
/>
</Flex>
</Flex>
<Outlet />
Expand All @@ -84,10 +75,12 @@ const AllowedEmailDomains = () => {

const Domains = ({
domains,
isLoading
isLoading,
canCreateDomain
}: {
domains: V1Beta1Domain[];
isLoading?: boolean;
canCreateDomain?: boolean;
}) => {
let navigate = useNavigate({ from: '/domains' });

Expand Down Expand Up @@ -115,13 +108,15 @@ const Domains = ({
/>
</Flex>

<Button
variant="primary"
style={{ width: 'fit-content' }}
onClick={() => navigate({ to: '/domains/modal' })}
>
Add Domain
</Button>
{canCreateDomain ? (
<Button
variant="primary"
style={{ width: 'fit-content' }}
onClick={() => navigate({ to: '/domains/modal' })}
>
Add Domain
</Button>
) : null}
</Flex>
</DataTable.Toolbar>
</DataTable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { Avatar, Flex, Text } from '@raystack/apsara';
import { Avatar, Flex } from '@raystack/apsara';
import Skeleton from 'react-loading-skeleton';

// @ts-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ const generalSchema = yup

export const GeneralOrganization = ({
organization,
isLoading
isLoading,
canUpdateWorkspace = false
}: {
organization?: V1Beta1Organization;
isLoading?: boolean;
canUpdateWorkspace?: boolean;
}) => {
const { client } = useFrontier();
const {
Expand Down Expand Up @@ -111,15 +113,17 @@ export const GeneralOrganization = ({
</Text>
</InputField>
</Box>
<Button
size="medium"
variant="primary"
type="submit"
style={{ width: 'fit-content' }}
disabled={isLoading || isSubmitting}
>
{isSubmitting ? 'updating...' : 'Update'}
</Button>
{canUpdateWorkspace ? (
<Button
size="medium"
variant="primary"
type="submit"
style={{ width: 'fit-content' }}
disabled={isLoading || isSubmitting}
>
{isSubmitting ? 'updating...' : 'Update'}
</Button>
) : null}
</Flex>
</form>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import { Button, Flex, Separator, Text } from '@raystack/apsara';
import { Outlet, useNavigate } from '@tanstack/react-router';
import { useMemo } from 'react';
import { useFrontier } from '~/react/contexts/FrontierContext';
import { usePermissions } from '~/react/hooks/usePermissions';
import { PERMISSIONS, shouldShowComponent } from '~/utils';
import { styles } from '../styles';
import { GeneralProfile } from './general.profile';
import { GeneralOrganization } from './general.workspace';
Expand All @@ -12,6 +15,37 @@ export default function GeneralSetting() {
activeOrganization: organization,
isActiveOrganizationLoading: isLoading
} = useFrontier();

const resource = `app/organization:${organization?.id}`;
const listOfPermissionsToCheck = [
{
permission: PERMISSIONS.UpdatePermission,
resource: resource
},
{
permission: PERMISSIONS.DeletePermission,
resource: resource
}
];

const { permissions } = usePermissions(
listOfPermissionsToCheck,
!!organization?.id
);

const { canUpdateWorkspace, canDeleteWorkspace } = useMemo(() => {
return {
canUpdateWorkspace: shouldShowComponent(
permissions,
`${PERMISSIONS.UpdatePermission}::${resource}`
),
canDeleteWorkspace: shouldShowComponent(
permissions,
`${PERMISSIONS.DeletePermission}::${resource}`
)
};
}, [permissions, resource]);

return (
<Flex direction="column" style={{ width: '100%' }}>
<Flex style={styles.header}>
Expand All @@ -22,11 +56,13 @@ export default function GeneralSetting() {
<Separator />
<GeneralOrganization
organization={organization}
canUpdateWorkspace={canUpdateWorkspace}
isLoading={isLoading}
/>
<Separator />
<GeneralDeleteOrganization isLoading={isLoading} />
<Separator />
{canDeleteWorkspace ? (
<GeneralDeleteOrganization isLoading={isLoading} />
) : null}
</Flex>
</Flex>
);
Expand All @@ -39,21 +75,27 @@ export const GeneralDeleteOrganization = ({
}) => {
const navigate = useNavigate({ from: '/' });
return (
<Flex direction="column" gap="medium">
<Text size={3} style={{ color: 'var(--foreground-muted)' }}>
If you want to permanently delete this organization and all of its data.
</Text>

<Button
variant="danger"
type="submit"
size="medium"
onClick={() => navigate({ to: '/delete' })}
disabled={isLoading}
>
Delete organization
</Button>
<Outlet />
</Flex>
<>
<Flex direction="column" gap="medium">
<Text size={3} style={{ color: 'var(--foreground-muted)' }}>
If you want to permanently delete this organization and all of its
data.
</Text>

<Button
variant="danger"
type="submit"
size="medium"
onClick={() => navigate({ to: '/delete' })}
disabled={isLoading}
>
Delete organization
</Button>
<Outlet />
</Flex>
<Separator />
</>
);
};


Loading
Loading