|
| 1 | +import { |
| 2 | + Dropdown, |
| 3 | + Select, |
| 4 | + Item, |
| 5 | + Menu, |
| 6 | + Skeleton, |
| 7 | + Tooltip, |
| 8 | + Separator, |
| 9 | +} from '@appquality/unguess-design-system'; |
| 10 | +import { Field } from '@zendeskgarden/react-dropdowns'; |
| 11 | +import { useEffect, useState } from 'react'; |
| 12 | +import { |
| 13 | + Bug, |
| 14 | + useGetCampaignsByCidCustomStatusesQuery, |
| 15 | + usePatchCampaignsByCidBugsAndBidMutation, |
| 16 | +} from 'src/features/api'; |
| 17 | +import styled from 'styled-components'; |
| 18 | +import { theme as globalTheme } from 'src/app/theme'; |
| 19 | +import { useTranslation } from 'react-i18next'; |
| 20 | +import { BugStateIcon } from 'src/common/components/BugStateIcon'; |
| 21 | +import { getBugStateLabel } from 'src/common/components/utils/getBugStateLabel'; |
| 22 | +import { Label } from './Label'; |
| 23 | + |
| 24 | +const StyledItem = styled(Item)` |
| 25 | + display: flex; |
| 26 | + align-items: center; |
| 27 | +
|
| 28 | + > svg { |
| 29 | + margin-right: ${({ theme }) => theme.space.xs}; |
| 30 | + } |
| 31 | +`; |
| 32 | + |
| 33 | +const SelectedItem = styled.div` |
| 34 | + display: flex; |
| 35 | + align-items: center; |
| 36 | +
|
| 37 | + > svg { |
| 38 | + margin-right: ${({ theme }) => theme.space.xs}; |
| 39 | + } |
| 40 | +`; |
| 41 | + |
| 42 | +type DropdownItem = { |
| 43 | + id: number; |
| 44 | + text: string; |
| 45 | + slug: string; |
| 46 | + icon: React.ReactNode; |
| 47 | +}; |
| 48 | + |
| 49 | +const BugStateDropdown = ({ bug }: { bug: Bug }) => { |
| 50 | + const { t } = useTranslation(); |
| 51 | + const { custom_status } = bug; |
| 52 | + const [selectedItem, setSelectedItem] = useState<DropdownItem | undefined>(); |
| 53 | + const [options, setOptions] = useState<DropdownItem[]>([]); |
| 54 | + const [patchBug] = usePatchCampaignsByCidBugsAndBidMutation(); |
| 55 | + const { |
| 56 | + data: cpBugStates, |
| 57 | + isLoading, |
| 58 | + isFetching, |
| 59 | + isError, |
| 60 | + } = useGetCampaignsByCidCustomStatusesQuery({ |
| 61 | + cid: bug.campaign_id.toString(), |
| 62 | + }); |
| 63 | + |
| 64 | + const sortStates = (a: DropdownItem, b: DropdownItem) => { |
| 65 | + if (a.id < b.id) return -1; |
| 66 | + if (a.id > b.id) return 1; |
| 67 | + return 0; |
| 68 | + }; |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + if (cpBugStates) { |
| 72 | + setOptions( |
| 73 | + cpBugStates |
| 74 | + .map((bugState) => ({ |
| 75 | + id: bugState.id, |
| 76 | + slug: bugState.name, |
| 77 | + text: getBugStateLabel(bugState.name as BugState, t), |
| 78 | + icon: ( |
| 79 | + <BugStateIcon |
| 80 | + {...globalTheme.colors.byBugState[bugState.name as BugState]} |
| 81 | + /> |
| 82 | + ), |
| 83 | + })) |
| 84 | + .sort(sortStates) |
| 85 | + ); |
| 86 | + } |
| 87 | + }, [cpBugStates]); |
| 88 | + |
| 89 | + useEffect(() => { |
| 90 | + setSelectedItem( |
| 91 | + options.find((bugStatus) => bugStatus.id === custom_status.id) |
| 92 | + ); |
| 93 | + }, [custom_status, options]); |
| 94 | + |
| 95 | + if (isError) return null; |
| 96 | + |
| 97 | + return ( |
| 98 | + <div> |
| 99 | + <Label style={{ marginBottom: globalTheme.space.xxs }}> |
| 100 | + {t('__BUGS_PAGE_BUG_DETAIL_STATE_LABEL')} |
| 101 | + </Label> |
| 102 | + {isLoading || isFetching ? ( |
| 103 | + <Skeleton |
| 104 | + height="30px" |
| 105 | + style={{ borderRadius: globalTheme.borderRadii.md }} |
| 106 | + /> |
| 107 | + ) : ( |
| 108 | + <Dropdown |
| 109 | + selectedItem={selectedItem} |
| 110 | + onSelect={async (item: DropdownItem) => { |
| 111 | + await patchBug({ |
| 112 | + cid: bug.campaign_id.toString(), |
| 113 | + bid: bug.id.toString(), |
| 114 | + body: { |
| 115 | + custom_status_id: item.id, |
| 116 | + }, |
| 117 | + }); |
| 118 | + setSelectedItem(item); |
| 119 | + }} |
| 120 | + downshiftProps={{ |
| 121 | + itemToString: (item: DropdownItem) => item && item.slug, |
| 122 | + }} |
| 123 | + > |
| 124 | + <Field> |
| 125 | + {bug.status.id === 4 ? ( |
| 126 | + <Tooltip |
| 127 | + appendToNode={document.body} |
| 128 | + type="light" |
| 129 | + content={t('__BUGS_PAGE_BUG_DETAIL_NEED_REVIEW_TOOLTIP')} |
| 130 | + > |
| 131 | + <Select isCompact disabled> |
| 132 | + <SelectedItem> |
| 133 | + {t('__BUGS_PAGE_BUG_DETAIL_NEED_REVIEW')} |
| 134 | + </SelectedItem> |
| 135 | + </Select> |
| 136 | + </Tooltip> |
| 137 | + ) : ( |
| 138 | + <Select isCompact> |
| 139 | + <SelectedItem> |
| 140 | + {selectedItem?.icon} {selectedItem?.text} |
| 141 | + </SelectedItem> |
| 142 | + </Select> |
| 143 | + )} |
| 144 | + </Field> |
| 145 | + <Menu> |
| 146 | + {options && |
| 147 | + options.map((item, i) => ( |
| 148 | + <> |
| 149 | + {i === 5 && <Separator />} |
| 150 | + <StyledItem key={item.slug} value={item}> |
| 151 | + {item.icon} {item.text} |
| 152 | + </StyledItem> |
| 153 | + </> |
| 154 | + ))} |
| 155 | + </Menu> |
| 156 | + </Dropdown> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + ); |
| 160 | +}; |
| 161 | + |
| 162 | +export default BugStateDropdown; |
0 commit comments