|
| 1 | +import {css} from '@emotion/react'; |
| 2 | +import styled from '@emotion/styled'; |
| 3 | + |
| 4 | +import { |
| 5 | + addErrorMessage, |
| 6 | + addLoadingMessage, |
| 7 | + addSuccessMessage, |
| 8 | +} from 'sentry/actionCreators/indicator'; |
| 9 | +import type {ModalRenderProps} from 'sentry/actionCreators/modal'; |
| 10 | +import {openModal} from 'sentry/actionCreators/modal'; |
| 11 | +import {Flex} from 'sentry/components/core/layout/flex'; |
| 12 | +import FieldFromConfig from 'sentry/components/forms/fieldFromConfig'; |
| 13 | +import Form from 'sentry/components/forms/form'; |
| 14 | +import {space} from 'sentry/styles/space'; |
| 15 | +import type {Organization} from 'sentry/types/organization'; |
| 16 | +import {fetchMutation, useMutation} from 'sentry/utils/queryClient'; |
| 17 | + |
| 18 | +interface ToggleConsolePlatformsModalProps extends ModalRenderProps { |
| 19 | + onSuccess: () => void; |
| 20 | + organization: Organization; |
| 21 | +} |
| 22 | + |
| 23 | +function ToggleConsolePlatformsModal({ |
| 24 | + Header, |
| 25 | + Body, |
| 26 | + closeModal, |
| 27 | + organization, |
| 28 | + onSuccess, |
| 29 | +}: ToggleConsolePlatformsModalProps) { |
| 30 | + const {enabledConsolePlatforms = []} = organization; |
| 31 | + |
| 32 | + const {isPending, mutate: updateConsolePlatforms} = useMutation({ |
| 33 | + mutationFn: (platforms: Record<string, boolean>) => { |
| 34 | + return fetchMutation({ |
| 35 | + method: 'PUT', |
| 36 | + url: `/organizations/${organization.slug}/`, |
| 37 | + data: { |
| 38 | + enabledConsolePlatforms: Object.keys(platforms).reduce((acc, key) => { |
| 39 | + if (platforms[key]) { |
| 40 | + acc.push(key); |
| 41 | + } |
| 42 | + return acc; |
| 43 | + }, [] as string[]), |
| 44 | + }, |
| 45 | + }); |
| 46 | + }, |
| 47 | + onMutate: () => { |
| 48 | + addLoadingMessage(`Updating console platforms for ${organization.slug}\u2026`); |
| 49 | + }, |
| 50 | + onSuccess: () => { |
| 51 | + addSuccessMessage(`Console platforms updated for ${organization.slug}`); |
| 52 | + onSuccess(); |
| 53 | + }, |
| 54 | + onError: () => { |
| 55 | + addErrorMessage(`Failed to update console platforms for ${organization.slug}`); |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + return ( |
| 60 | + <Form |
| 61 | + onSubmit={data => updateConsolePlatforms(data as Record<string, boolean>)} |
| 62 | + onCancel={closeModal} |
| 63 | + saveOnBlur={false} |
| 64 | + initialData={{ |
| 65 | + playstation: enabledConsolePlatforms.includes('playstation'), |
| 66 | + 'nintendo-switch': enabledConsolePlatforms.includes('nintendo-switch'), |
| 67 | + xbox: enabledConsolePlatforms.includes('xbox'), |
| 68 | + }} |
| 69 | + submitLabel="Save" |
| 70 | + submitDisabled={isPending} |
| 71 | + > |
| 72 | + <Header closeButton> |
| 73 | + <Flex align="center" gap={space(2)}> |
| 74 | + <h4>Toggle Console Platforms</h4> |
| 75 | + </Flex> |
| 76 | + </Header> |
| 77 | + <Body> |
| 78 | + <p |
| 79 | + css={css` |
| 80 | + margin: 0; |
| 81 | + `} |
| 82 | + > |
| 83 | + Toggle consoles to allow users in this organization to create console projects |
| 84 | + and view private setup instructions. |
| 85 | + </p> |
| 86 | + <div |
| 87 | + css={css` |
| 88 | + margin: 0 -${space(4)}; |
| 89 | + `} |
| 90 | + > |
| 91 | + <StyledFieldFromConfig |
| 92 | + field={{ |
| 93 | + name: 'playstation', |
| 94 | + type: 'boolean', |
| 95 | + label: 'Playstation', |
| 96 | + help: 'Toggle the Playstation console platform for this organization.', |
| 97 | + }} |
| 98 | + flexibleControlStateSize |
| 99 | + inline |
| 100 | + /> |
| 101 | + <StyledFieldFromConfig |
| 102 | + field={{ |
| 103 | + name: 'nintendo-switch', |
| 104 | + type: 'boolean', |
| 105 | + label: 'Nintendo Switch', |
| 106 | + help: 'Toggle the Nintendo Switch console platform for this organization.', |
| 107 | + }} |
| 108 | + flexibleControlStateSize |
| 109 | + inline |
| 110 | + /> |
| 111 | + <StyledFieldFromConfig |
| 112 | + field={{ |
| 113 | + name: 'xbox', |
| 114 | + type: 'boolean', |
| 115 | + label: 'Xbox', |
| 116 | + help: 'Toggle the Xbox console platform for this organization.', |
| 117 | + }} |
| 118 | + flexibleControlStateSize |
| 119 | + inline |
| 120 | + /> |
| 121 | + </div> |
| 122 | + </Body> |
| 123 | + </Form> |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +const StyledFieldFromConfig = styled(FieldFromConfig)` |
| 128 | + padding-left: ${space(4)}; |
| 129 | + &:last-child { |
| 130 | + padding-bottom: 0; |
| 131 | + } |
| 132 | +`; |
| 133 | + |
| 134 | +export function openToggleConsolePlatformsModal({ |
| 135 | + organization, |
| 136 | + onSuccess, |
| 137 | +}: { |
| 138 | + onSuccess: () => void; |
| 139 | + organization: Organization; |
| 140 | +}) { |
| 141 | + return openModal(deps => ( |
| 142 | + <ToggleConsolePlatformsModal |
| 143 | + {...deps} |
| 144 | + organization={organization} |
| 145 | + onSuccess={onSuccess} |
| 146 | + /> |
| 147 | + )); |
| 148 | +} |
0 commit comments