Source: Pages/Budget/Transaction/EditTransaction.js

  1. import React, { useState} from 'react';
  2. import Dialog from '@mui/material/Dialog';
  3. import DialogActions from '@mui/material/DialogActions';
  4. import DialogContent from '@mui/material/DialogContent';
  5. import DialogTitle from '@mui/material/DialogTitle';
  6. import Button from '@mui/material/Button';
  7. import IconButton from '@mui/material/IconButton';
  8. import EditIcon from '@mui/icons-material/Edit';
  9. import TextField from '@mui/material/TextField';
  10. import Stack from '@mui/material/Stack';
  11. import { DatePicker } from '@mui/x-date-pickers/DatePicker';
  12. import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
  13. import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
  14. import dayjs from 'dayjs';
  15. /**
  16. * Component for editing an existing transaction. It consists of a pop-up
  17. * window with input fields which represents the data associated with
  18. * the transaction.
  19. *
  20. * @param props.data The transaction data which are to be displayed in the editor.
  21. * @param props.type {String} The type of the transaction which is either "Income" or "Expense".
  22. * @param props.updateTransaction Function for updating a given transaction.
  23. */
  24. function EditTransaction(props) {
  25. /** Boolean indicating whether to display the pop-up window or not. */
  26. const [open, setOpen] = useState(false);
  27. /** The new transaction with its given data. */
  28. const [transaction, setTransaction] = useState({
  29. tname: '',
  30. value: '',
  31. description: '',
  32. date: '',
  33. });
  34. /**
  35. * Activates upon pressing the button for editing a transaction. It
  36. * displays the pop-up window and inserts the current values of the
  37. * transaction in the input fields.
  38. */
  39. const handleClickOpen = () => {
  40. setTransaction({
  41. tname: props.data.row.tname,
  42. value: props.data.row.value,
  43. description: props.data.row.description,
  44. date: props.data.row.date,
  45. });
  46. setOpen(true);
  47. };
  48. /**
  49. * Activates upon pressing the cancel button. It will close
  50. * the pop-up window upon activation.
  51. */
  52. const handleClose = () => {
  53. setOpen(false);
  54. };
  55. /**
  56. * Activates upon changes in any of the input-fields except the DatePicker.
  57. * Updates the transaction variable with the values read from the input-
  58. * fields.
  59. *
  60. * @param event The event which triggers the function.
  61. */
  62. const handleChange = (event) => {
  63. setTransaction({
  64. ...transaction,
  65. [event.target.name]: event.target.value
  66. });
  67. };
  68. /**
  69. * Activates upon pressing the add button. It calls
  70. * on the updateTransaction function provided by the props
  71. * parameter of the EditTransaction component with the
  72. * updated transaction, the transaction id and the type
  73. * of transaction as parameters. After, it will close the
  74. * pop-up window.
  75. */
  76. const handleSave = () => {
  77. props.updateTransaction(transaction, props.data.id, props.type);
  78. handleClose();
  79. };
  80. /**
  81. * Activates upon changes in the DatePicker component.
  82. * Updates the transaction date variable with the value
  83. * read from the DatePicker input on the YYYY-MM-DD format.
  84. *
  85. * @param value The date which are picked in the DatePicker component.
  86. */
  87. const handleDateChange = (value) => {
  88. value = value.format('YYYY-MM-DD');
  89. setTransaction({
  90. ...transaction,
  91. ['date']: value
  92. });
  93. };
  94. return (
  95. <div>
  96. <IconButton onClick = {handleClickOpen}>
  97. <EditIcon color = "primary" />
  98. </IconButton>
  99. <Dialog open = {open} onClose = {handleClose}>
  100. <DialogTitle>Edit Transaction</DialogTitle>
  101. <DialogContent>
  102. <Stack spacing = {2} mt = {1}>
  103. <TextField label = "Name" name = "tname"
  104. autoFocus
  105. variant = "standard" value = {transaction.tname}
  106. onChange = {handleChange}
  107. />
  108. <TextField label = "Value" name = "value"
  109. autoFocus
  110. variant = "standard" value = {transaction.value}
  111. onChange = {handleChange}
  112. />
  113. <TextField label = "Description" name = "description"
  114. autoFocus
  115. variant = "standard" value = {transaction.description}
  116. onChange = {handleChange}
  117. />
  118. <LocalizationProvider dateAdapter={AdapterDayjs}>
  119. <DatePicker
  120. name = "date"
  121. defaultValue={dayjs(transaction.date)}
  122. onChange={(value) => handleDateChange(value)}
  123. />
  124. </LocalizationProvider>
  125. </Stack>
  126. </DialogContent>
  127. <DialogActions>
  128. <Button onClick = {handleClose}>Cancel</Button>
  129. <Button onClick = {handleSave}>Save</Button>
  130. </DialogActions>
  131. </Dialog>
  132. </div>
  133. );
  134. }
  135. export default EditTransaction;