|  | 
|  | 1 | +// import dependencies | 
|  | 2 | +import { useState } from 'react'; | 
|  | 3 | +import { toast } from 'react-toastify'; | 
|  | 4 | +import { Input } from '@lunalytics/ui'; | 
|  | 5 | + | 
|  | 6 | +// import local files | 
|  | 7 | +import Modal from '../../../ui/modal'; | 
|  | 8 | +import SwitchWithText from '../../../ui/switch'; | 
|  | 9 | +import useTokensContext from '../../../../context/tokens'; | 
|  | 10 | +import { createPostRequest } from '../../../../services/axios'; | 
|  | 11 | +import { PermissionsBits } from '../../../../../shared/permissions/bitFlags'; | 
|  | 12 | +import TokenValidator from '../../../../../shared/validators/token'; | 
|  | 13 | + | 
|  | 14 | +const permissionsWithDescription = [ | 
|  | 15 | +  { | 
|  | 16 | +    permission: PermissionsBits.VIEW_MONITORS, | 
|  | 17 | +    title: 'View Monitors', | 
|  | 18 | +    description: | 
|  | 19 | +      'Tokens with this permission will be able to view all monitors.', | 
|  | 20 | +  }, | 
|  | 21 | +  { | 
|  | 22 | +    permission: PermissionsBits.MANAGE_MONITORS, | 
|  | 23 | +    title: 'Manage Monitors', | 
|  | 24 | +    description: | 
|  | 25 | +      'Tokens with this permission will be able to create, edit and delete monitors.', | 
|  | 26 | +  }, | 
|  | 27 | +  { | 
|  | 28 | +    permission: PermissionsBits.VIEW_NOTIFICATIONS, | 
|  | 29 | +    title: 'View Notifications', | 
|  | 30 | +    description: | 
|  | 31 | +      'Tokens with this permission will be able to view all notifications.', | 
|  | 32 | +  }, | 
|  | 33 | +  { | 
|  | 34 | +    permission: PermissionsBits.MANAGE_NOTIFICATIONS, | 
|  | 35 | +    title: 'Manage Notifications', | 
|  | 36 | +    description: | 
|  | 37 | +      'Tokens with this permission will be able to create, edit and delete notifications.', | 
|  | 38 | +  }, | 
|  | 39 | +  { | 
|  | 40 | +    permission: PermissionsBits.VIEW_STATUS_PAGES, | 
|  | 41 | +    title: 'View Status Pages', | 
|  | 42 | +    description: | 
|  | 43 | +      'Tokens with this permission will be able to view all status pages.', | 
|  | 44 | +  }, | 
|  | 45 | +  { | 
|  | 46 | +    permission: PermissionsBits.MANAGE_STATUS_PAGES, | 
|  | 47 | +    title: 'Manage Status Pages', | 
|  | 48 | +    description: | 
|  | 49 | +      'Tokens with this permission will be able to create, edit and delete status pages.', | 
|  | 50 | +  }, | 
|  | 51 | +  { | 
|  | 52 | +    permission: PermissionsBits.VIEW_INCIDENTS, | 
|  | 53 | +    title: 'View Incidents', | 
|  | 54 | +    description: | 
|  | 55 | +      'Tokens with this permission will be able to view all incidents.', | 
|  | 56 | +  }, | 
|  | 57 | +  { | 
|  | 58 | +    permission: PermissionsBits.MANAGE_INCIDENTS, | 
|  | 59 | +    title: 'Manage Incidents', | 
|  | 60 | +    description: | 
|  | 61 | +      'Tokens with this permission will be able to create, edit and delete incidents.', | 
|  | 62 | +  }, | 
|  | 63 | +  { | 
|  | 64 | +    permission: PermissionsBits.MANAGE_TEAM, | 
|  | 65 | +    title: 'Manage Team', | 
|  | 66 | +    description: | 
|  | 67 | +      'Tokens with this permission will be able to manage the team members.', | 
|  | 68 | +  }, | 
|  | 69 | +  { | 
|  | 70 | +    permission: PermissionsBits.ADMINISTRATOR, | 
|  | 71 | +    title: 'Administrator', | 
|  | 72 | +    description: | 
|  | 73 | +      'Tokens with this permission will have every permission and will be able to bypass any restrictions.', | 
|  | 74 | +  }, | 
|  | 75 | +]; | 
|  | 76 | + | 
|  | 77 | +const SettingsApiConfigureModal = ({ | 
|  | 78 | +  closeModal, | 
|  | 79 | +  tokenId = '', | 
|  | 80 | +  tokenName = '', | 
|  | 81 | +  tokenPermissions = 0, | 
|  | 82 | +  isEdit = false, | 
|  | 83 | +}) => { | 
|  | 84 | +  const { addToken, updateTokenPermission } = useTokensContext(); | 
|  | 85 | + | 
|  | 86 | +  const [name, setName] = useState(tokenName); | 
|  | 87 | +  const [perms, setPermission] = useState(tokenPermissions); | 
|  | 88 | + | 
|  | 89 | +  const changePermission = (isChecked, permission) => { | 
|  | 90 | +    if (isChecked) { | 
|  | 91 | +      setPermission(perms | permission); | 
|  | 92 | +    } else { | 
|  | 93 | +      setPermission(perms & ~permission); | 
|  | 94 | +    } | 
|  | 95 | +  }; | 
|  | 96 | + | 
|  | 97 | +  const handleEditOrCreateToken = async () => { | 
|  | 98 | +    try { | 
|  | 99 | +      const path = isEdit ? '/api/tokens/update' : '/api/tokens/create'; | 
|  | 100 | + | 
|  | 101 | +      const isInvalid = TokenValidator({ | 
|  | 102 | +        name, | 
|  | 103 | +        token: tokenId, | 
|  | 104 | +        permission: perms, | 
|  | 105 | +        isEdit, | 
|  | 106 | +      }); | 
|  | 107 | + | 
|  | 108 | +      if (isInvalid) { | 
|  | 109 | +        return toast.error(isInvalid); | 
|  | 110 | +      } | 
|  | 111 | + | 
|  | 112 | +      const response = await createPostRequest(path, { | 
|  | 113 | +        name, | 
|  | 114 | +        token: tokenId, | 
|  | 115 | +        permission: perms, | 
|  | 116 | +      }); | 
|  | 117 | + | 
|  | 118 | +      const data = response?.data || {}; | 
|  | 119 | + | 
|  | 120 | +      if (isEdit) { | 
|  | 121 | +        updateTokenPermission(data.token, data.permission, data.name); | 
|  | 122 | +      } else { | 
|  | 123 | +        addToken(data); | 
|  | 124 | +      } | 
|  | 125 | + | 
|  | 126 | +      const message = isEdit | 
|  | 127 | +        ? 'Token has been updated successfully' | 
|  | 128 | +        : 'Token has been created successfully'; | 
|  | 129 | + | 
|  | 130 | +      toast.success(message); | 
|  | 131 | + | 
|  | 132 | +      closeModal(); | 
|  | 133 | +    } catch (error) { | 
|  | 134 | +      if (error.response?.status === 401) { | 
|  | 135 | +        closeModal(); | 
|  | 136 | +        return; | 
|  | 137 | +      } | 
|  | 138 | + | 
|  | 139 | +      toast.error( | 
|  | 140 | +        `Error occured while ${isEdit ? 'editing' : 'creating'} token` | 
|  | 141 | +      ); | 
|  | 142 | +      closeModal(); | 
|  | 143 | +    } | 
|  | 144 | +  }; | 
|  | 145 | + | 
|  | 146 | +  return ( | 
|  | 147 | +    <Modal.Container contentProps={{ style: { width: '850px' } }}> | 
|  | 148 | +      <Modal.Title>{isEdit ? 'Update' : 'Create'} API Token</Modal.Title> | 
|  | 149 | +      <Modal.Message> | 
|  | 150 | +        <div style={{ display: 'flex', flexDirection: 'column', gap: '10px' }}> | 
|  | 151 | +          <Input | 
|  | 152 | +            title="Token Name" | 
|  | 153 | +            id="name" | 
|  | 154 | +            type="text" | 
|  | 155 | +            placeholder="Lunalytics" | 
|  | 156 | +            onChange={(event) => setName(event.target.value)} | 
|  | 157 | +            value={name} | 
|  | 158 | +            subtitle="This will be automatically generated if one is not provided." | 
|  | 159 | +          /> | 
|  | 160 | + | 
|  | 161 | +          <div> | 
|  | 162 | +            <div className="input-label">Token Permissions</div> | 
|  | 163 | +            <div className="input-short-description"> | 
|  | 164 | +              Permissions are used to restrict what the token can access. | 
|  | 165 | +            </div> | 
|  | 166 | +            <div | 
|  | 167 | +              style={{ | 
|  | 168 | +                gap: '10px', | 
|  | 169 | +                display: 'flex', | 
|  | 170 | +                flexDirection: 'column', | 
|  | 171 | +                padding: '15px 0 20px 0', | 
|  | 172 | +              }} | 
|  | 173 | +            > | 
|  | 174 | +              {permissionsWithDescription.map((permission) => ( | 
|  | 175 | +                <div | 
|  | 176 | +                  key={title} | 
|  | 177 | +                  style={{ | 
|  | 178 | +                    borderBottom: '1px solid var(--accent-700)', | 
|  | 179 | +                    paddingBottom: '10px', | 
|  | 180 | +                  }} | 
|  | 181 | +                > | 
|  | 182 | +                  <SwitchWithText | 
|  | 183 | +                    key={permission.title} | 
|  | 184 | +                    label={permission.title} | 
|  | 185 | +                    shortDescription={permission.description} | 
|  | 186 | +                    onChange={(event) => | 
|  | 187 | +                      changePermission( | 
|  | 188 | +                        event.target.checked, | 
|  | 189 | +                        permission.permission | 
|  | 190 | +                      ) | 
|  | 191 | +                    } | 
|  | 192 | +                    checked={ | 
|  | 193 | +                      perms & permission.permission || | 
|  | 194 | +                      perms === PermissionsBits.ADMINISTRATOR || | 
|  | 195 | +                      perms & PermissionsBits.ADMINISTRATOR | 
|  | 196 | +                    } | 
|  | 197 | +                  /> | 
|  | 198 | +                </div> | 
|  | 199 | +              ))} | 
|  | 200 | +            </div> | 
|  | 201 | +          </div> | 
|  | 202 | +        </div> | 
|  | 203 | +      </Modal.Message> | 
|  | 204 | +      <Modal.Actions> | 
|  | 205 | +        <Modal.Button id="manage-close-button" onClick={closeModal}> | 
|  | 206 | +          Close | 
|  | 207 | +        </Modal.Button> | 
|  | 208 | +        <Modal.Button | 
|  | 209 | +          id="manage-create-button" | 
|  | 210 | +          color="green" | 
|  | 211 | +          onClick={handleEditOrCreateToken} | 
|  | 212 | +        > | 
|  | 213 | +          {isEdit ? 'Update' : 'Create'} | 
|  | 214 | +        </Modal.Button> | 
|  | 215 | +      </Modal.Actions> | 
|  | 216 | +    </Modal.Container> | 
|  | 217 | +  ); | 
|  | 218 | +}; | 
|  | 219 | + | 
|  | 220 | +SettingsApiConfigureModal.displayName = 'SettingsApiConfigureModal'; | 
|  | 221 | + | 
|  | 222 | +export default SettingsApiConfigureModal; | 
0 commit comments