-
-
Notifications
You must be signed in to change notification settings - Fork 658
/
ChangeRequestTitle.tsx
104 lines (99 loc) · 3.53 KB
/
ChangeRequestTitle.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React, { FC, useState } from 'react';
import { Box, Button, IconButton, styled, Typography } from '@mui/material';
import Input from 'component/common/Input/Input';
import { ChangeRequestType } from '../../changeRequest.types';
import Edit from '@mui/icons-material/Edit';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import { useChangeRequestApi } from 'hooks/api/actions/useChangeRequestApi/useChangeRequestApi';
import { formatUnknownError } from 'utils/formatUnknownError';
import useToast from 'hooks/useToast';
const StyledBox = styled(Box)(({ theme }) => ({
display: 'flex',
flexDirection: 'row',
width: '100%',
'& > div': { width: '100%' },
justifyContent: 'space-between',
marginBottom: theme.spacing(1),
}));
export const StyledHeader = styled(Typography)(({ theme }) => ({
display: 'flex',
alignItems: 'center',
marginRight: theme.spacing(1),
fontSize: theme.fontSizes.mainHeader,
}));
export const ChangeRequestTitle: FC<{
environmentChangeRequest: ChangeRequestType;
title: string;
setTitle: React.Dispatch<React.SetStateAction<string>>;
}> = ({ environmentChangeRequest, title, setTitle, children }) => {
const [isDisabled, setIsDisabled] = useState(true);
const { updateTitle } = useChangeRequestApi();
const { setToastData, setToastApiError } = useToast();
const toggleEditState = () => {
setIsDisabled(!isDisabled);
};
const saveTitle = async () => {
toggleEditState();
try {
await updateTitle(
environmentChangeRequest.project,
environmentChangeRequest.id,
title,
);
setToastData({
type: 'success',
title: 'Change request title updated!',
});
} catch (error: unknown) {
setToastApiError(formatUnknownError(error));
}
};
return (
<StyledBox>
<ConditionallyRender
condition={isDisabled}
show={children}
elseShow={
<Input
label='Change request title'
id='group-name'
value={title}
fullWidth
onChange={(e) => setTitle(e.target.value)}
disabled={isDisabled}
/>
}
/>
<ConditionallyRender
condition={isDisabled}
show={
<IconButton onClick={toggleEditState}>
<Edit />
</IconButton>
}
/>
<ConditionallyRender
condition={!isDisabled}
show={
<>
<Button
variant='contained'
color='primary'
sx={(theme) => ({ marginLeft: theme.spacing(2) })}
onClick={() => saveTitle()}
>
Save
</Button>
<Button
sx={(theme) => ({ marginLeft: theme.spacing(1) })}
variant='outlined'
onClick={toggleEditState}
>
Cancel
</Button>{' '}
</>
}
/>
</StyledBox>
);
};