|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | + |
| 3 | +import React, { useContext, useState } from 'react'; |
| 4 | +import { |
| 5 | + Box, |
| 6 | + Button, |
| 7 | + CircularProgress, |
| 8 | + Divider, |
| 9 | + List, |
| 10 | + ListItem, |
| 11 | + ListItemText, |
| 12 | + Typography |
| 13 | +} from '../../base'; |
| 14 | +import { EVENT_TYPES, RESOURCE_TYPE } from '../../constants/constants'; |
| 15 | +import { OpenFileIcon } from '../../icons'; |
| 16 | +import { styled } from '../../theme'; |
| 17 | +import { Pattern } from '../CustomCatalog/CustomCard'; |
| 18 | +import { Modal, ModalBody, ModalFooter, PrimaryActionButtons } from '../Modal'; |
| 19 | + |
| 20 | +interface WorkspaceContentMoveModalProps { |
| 21 | + workspaceContentMoveModal: boolean; |
| 22 | + setWorkspaceContentMoveModal: (open: boolean) => void; |
| 23 | + currentWorkspace: any; |
| 24 | + type: string; |
| 25 | + selectedContent: any; |
| 26 | + refetch?: () => void; |
| 27 | + useGetWorkspacesQuery: any; |
| 28 | + isCreateWorkspaceAllowed: boolean; |
| 29 | + isMoveDesignAllowed: boolean; |
| 30 | + isMoveViewAllowed: boolean; |
| 31 | + assignDesignToWorkspace: (params: { workspaceId: string; designId: string }) => Promise<any>; |
| 32 | + assignViewToWorkspace: (params: { workspaceId: string; viewId: string }) => Promise<any>; |
| 33 | + WorkspaceModalContext: React.Context<any>; |
| 34 | + notify: any; |
| 35 | + router: any; |
| 36 | + currentOrgId: string; |
| 37 | +} |
| 38 | + |
| 39 | +interface Workspace { |
| 40 | + id: string; |
| 41 | + name: string; |
| 42 | +} |
| 43 | + |
| 44 | +const WorkspaceItem = styled(ListItem)({ |
| 45 | + borderRadius: '8px' |
| 46 | +}); |
| 47 | + |
| 48 | +const CurrentWorkspaceSection = styled(Typography)(({ theme }) => ({ |
| 49 | + marginBottom: '1rem', |
| 50 | + color: theme.palette.text.secondary |
| 51 | +})); |
| 52 | + |
| 53 | +const NoWorkspacesContainer = styled('div')(({ theme }) => ({ |
| 54 | + padding: '1rem', |
| 55 | + textAlign: 'center', |
| 56 | + color: theme.palette.text.secondary, |
| 57 | + border: `1px dashed ${theme.palette.border.strong}`, |
| 58 | + borderRadius: '8px', |
| 59 | + margin: '1rem 0', |
| 60 | + display: 'flex', |
| 61 | + flexDirection: 'column', |
| 62 | + alignItems: 'center', |
| 63 | + gap: '1rem' |
| 64 | +})); |
| 65 | + |
| 66 | +const getModalTitle = (type: string, selectedContent: any, multiSelectedContent: any[]): string => { |
| 67 | + const itemCount = multiSelectedContent?.length || 1; |
| 68 | + const resourceName = selectedContent?.name || itemCount; |
| 69 | + const resourceType = type === RESOURCE_TYPE.DESIGN ? 'Design' : 'View'; |
| 70 | + |
| 71 | + return `Move ${resourceName} ${resourceType}${itemCount > 1 ? 's' : ''}`; |
| 72 | +}; |
| 73 | + |
| 74 | +const WorkspaceContentMoveModal: React.FC<WorkspaceContentMoveModalProps> = ({ |
| 75 | + workspaceContentMoveModal, |
| 76 | + setWorkspaceContentMoveModal, |
| 77 | + currentWorkspace, |
| 78 | + type, |
| 79 | + selectedContent, |
| 80 | + refetch, |
| 81 | + useGetWorkspacesQuery, |
| 82 | + isCreateWorkspaceAllowed, |
| 83 | + isMoveDesignAllowed, |
| 84 | + isMoveViewAllowed, |
| 85 | + assignDesignToWorkspace, |
| 86 | + assignViewToWorkspace, |
| 87 | + WorkspaceModalContext, |
| 88 | + notify, |
| 89 | + router, |
| 90 | + currentOrgId |
| 91 | +}) => { |
| 92 | + const { setMultiSelectedContent, multiSelectedContent, closeModal } = |
| 93 | + useContext(WorkspaceModalContext); |
| 94 | + const { data: workspaceData, isLoading } = useGetWorkspacesQuery( |
| 95 | + { |
| 96 | + page: 0, |
| 97 | + pagesize: 'all', |
| 98 | + order: 'updated_at desc', |
| 99 | + orgID: currentOrgId |
| 100 | + }, |
| 101 | + { |
| 102 | + skip: !currentOrgId |
| 103 | + } |
| 104 | + ); |
| 105 | + |
| 106 | + const filteredWorkspaces = workspaceData?.workspaces?.filter( |
| 107 | + (workspace: Workspace) => workspace.id !== currentWorkspace.id |
| 108 | + ); |
| 109 | + const [selectedWorkspaceForMove, setSelectedWorkspaceForMove] = useState<Workspace | null>(null); |
| 110 | + |
| 111 | + const handleMove = async (): Promise<void> => { |
| 112 | + setWorkspaceContentMoveModal(false); |
| 113 | + |
| 114 | + try { |
| 115 | + const moveDesign = async (designId: string): Promise<void> => { |
| 116 | + await assignDesignToWorkspace({ |
| 117 | + workspaceId: selectedWorkspaceForMove!.id, |
| 118 | + designId |
| 119 | + }); |
| 120 | + }; |
| 121 | + |
| 122 | + const moveView = async (viewId: string): Promise<void> => { |
| 123 | + await assignViewToWorkspace({ |
| 124 | + workspaceId: selectedWorkspaceForMove!.id, |
| 125 | + viewId |
| 126 | + }); |
| 127 | + }; |
| 128 | + |
| 129 | + if (RESOURCE_TYPE.DESIGN === type) { |
| 130 | + if (multiSelectedContent.length > 0) { |
| 131 | + await Promise.all(multiSelectedContent.map((design: Pattern) => moveDesign(design.id))); |
| 132 | + setMultiSelectedContent([]); |
| 133 | + } |
| 134 | + if (selectedContent) { |
| 135 | + await moveDesign(selectedContent.id); |
| 136 | + } |
| 137 | + } else { |
| 138 | + if (multiSelectedContent.length > 0) { |
| 139 | + await Promise.all(multiSelectedContent.map((view: { id: string }) => moveView(view.id))); |
| 140 | + setMultiSelectedContent([]); |
| 141 | + } |
| 142 | + if (selectedContent) { |
| 143 | + await moveView(selectedContent.id); |
| 144 | + } |
| 145 | + } |
| 146 | + if (refetch) { |
| 147 | + refetch(); |
| 148 | + } |
| 149 | + notify({ |
| 150 | + message: `Successfully moved ${type === RESOURCE_TYPE.DESIGN ? 'design' : 'view'}${multiSelectedContent.length > 1 ? 's' : ''} to ${selectedWorkspaceForMove!.name}`, |
| 151 | + event_type: EVENT_TYPES.SUCCESS |
| 152 | + }); |
| 153 | + } catch (error) { |
| 154 | + notify({ |
| 155 | + message: `Failed to move ${type === RESOURCE_TYPE.DESIGN ? 'design' : 'view'}. Please try again.`, |
| 156 | + event_type: EVENT_TYPES.ERROR |
| 157 | + }); |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + const handleCreateWorkspace = (): void => { |
| 162 | + closeModal(); |
| 163 | + setWorkspaceContentMoveModal(false); |
| 164 | + router.push('/management/workspaces'); |
| 165 | + }; |
| 166 | + |
| 167 | + const isMoveAllowed = type === RESOURCE_TYPE.DESIGN ? isMoveDesignAllowed : isMoveViewAllowed; |
| 168 | + |
| 169 | + return ( |
| 170 | + <Modal |
| 171 | + open={workspaceContentMoveModal} |
| 172 | + headerIcon={<OpenFileIcon />} |
| 173 | + closeModal={() => setWorkspaceContentMoveModal(false)} |
| 174 | + title={getModalTitle(type, selectedContent, multiSelectedContent)} |
| 175 | + > |
| 176 | + <ModalBody> |
| 177 | + <CurrentWorkspaceSection> |
| 178 | + Current Workspace: <strong>{currentWorkspace.name}</strong> |
| 179 | + </CurrentWorkspaceSection> |
| 180 | + <Divider /> |
| 181 | + <Box display="flex" flexDirection="column" marginTop={'1rem'}> |
| 182 | + {isLoading ? ( |
| 183 | + <CircularProgress size={24} /> |
| 184 | + ) : !filteredWorkspaces?.length ? ( |
| 185 | + <NoWorkspacesContainer> |
| 186 | + <Typography>No other workspaces available to move content to.</Typography> |
| 187 | + <Button |
| 188 | + variant="contained" |
| 189 | + color="primary" |
| 190 | + onClick={handleCreateWorkspace} |
| 191 | + disabled={!isCreateWorkspaceAllowed} |
| 192 | + > |
| 193 | + Create Workspace |
| 194 | + </Button> |
| 195 | + </NoWorkspacesContainer> |
| 196 | + ) : ( |
| 197 | + <> |
| 198 | + <Typography style={{ marginBottom: '0.5rem' }}> |
| 199 | + Select destination workspace |
| 200 | + </Typography> |
| 201 | + |
| 202 | + <List> |
| 203 | + {filteredWorkspaces.map((workspace: Workspace) => ( |
| 204 | + <WorkspaceItem |
| 205 | + key={workspace.id} |
| 206 | + selected={selectedWorkspaceForMove?.id === workspace.id} |
| 207 | + onClick={() => isMoveAllowed && setSelectedWorkspaceForMove(workspace)} |
| 208 | + disabled={!isMoveAllowed} |
| 209 | + > |
| 210 | + <ListItemText primary={workspace.name} /> |
| 211 | + </WorkspaceItem> |
| 212 | + ))} |
| 213 | + </List> |
| 214 | + </> |
| 215 | + )} |
| 216 | + </Box> |
| 217 | + </ModalBody> |
| 218 | + <ModalFooter variant="filled"> |
| 219 | + <PrimaryActionButtons |
| 220 | + primaryText={'Move'} |
| 221 | + secondaryText="Cancel" |
| 222 | + primaryButtonProps={{ |
| 223 | + onClick: handleMove, |
| 224 | + disabled: |
| 225 | + isLoading || |
| 226 | + !selectedWorkspaceForMove || |
| 227 | + !filteredWorkspaces?.length || |
| 228 | + !isMoveAllowed |
| 229 | + }} |
| 230 | + secondaryButtonProps={{ |
| 231 | + onClick: () => setWorkspaceContentMoveModal(false) |
| 232 | + }} |
| 233 | + /> |
| 234 | + </ModalFooter> |
| 235 | + </Modal> |
| 236 | + ); |
| 237 | +}; |
| 238 | + |
| 239 | +export default WorkspaceContentMoveModal; |
0 commit comments