Skip to content

Commit

Permalink
Adapt entry dialog to show existing values in edit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Jan 11, 2024
1 parent be9242b commit 4d62dac
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
24 changes: 12 additions & 12 deletions web/src/components/entry-dialog/EntryDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ export type EntryFormSchema = {

type EntryDialogProps = DialogProps & {
editEntry?: Entry;
date?: Dayjs;
};

const EntryDialog = ({ editEntry, ...props }: EntryDialogProps) => {
console.log("edit entry", editEntry);
const EntryDialog = ({ editEntry, date, ...props }: EntryDialogProps) => {
const theme = useTheme();
const fullScreen = useMediaQuery(theme.breakpoints.down("md"));
const [addWorkdayEntryMutation] = useMutation(AddWorkdayEntryDocument);

const { handleSubmit, control, reset } = useForm<EntryFormSchema>({
defaultValues: {
date: dayjs(),
duration: "",
product: "",
activity: "",
issue: "",
client: "",
},
});
const defaultValues: EntryFormSchema = {
date: date ? dayjs(date) : dayjs(),
duration: editEntry?.duration.toString() || "",
product: editEntry?.product || "",
activity: editEntry?.activity || "",
issue: editEntry?.issue || "",
client: editEntry?.client || "",
};

const { handleSubmit, control, reset } = useForm<EntryFormSchema>({ defaultValues });

const onSubmit: SubmitHandler<EntryFormSchema> = async (formValues) => {
const { date, duration, product, activity, issue, client } = formValues;
Expand Down
27 changes: 23 additions & 4 deletions web/src/components/workday-accordion/EditEntryButton.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import EditIcon from "@mui/icons-material/Edit";
import { IconButton } from "@mui/material";
import { Dayjs } from "dayjs";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { Entry } from "../../graphql/generated/graphql";
import EntryDialog from "../entry-dialog/EntryDialog";

const EditEntryButton = () => {
type EditEntryButtonProps = {
entry: Entry;
date: Dayjs;
};

const EditEntryButton = ({ entry, date }: EditEntryButtonProps) => {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const toggleOpen = () => setOpen((prev) => !prev);

return (
<IconButton aria-label={t("controls.editEntry")} color="primary" size="small">
<EditIcon fontSize="inherit" />
</IconButton>
<>
<IconButton
aria-label={t("controls.editEntry")}
color="primary"
size="small"
onClick={toggleOpen}
>
<EditIcon fontSize="inherit" />
</IconButton>
<EntryDialog open={open} onClose={toggleOpen} editEntry={entry} date={date} />
</>
);
};

Expand Down
2 changes: 1 addition & 1 deletion web/src/components/workday-accordion/EntryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const EntryTable = ({ workday }: EntryTableProps) => {
<TableCell>{entry.issue}</TableCell>
<TableCell>{entry.client}</TableCell>
<TableCell>
<EditEntryButton />
<EditEntryButton entry={entry} date={dayjs(workday.date)} />
<DeleteEntryButton entryKey={entry.key} date={dayjs(workday.date)} />
</TableCell>
</TableRow>
Expand Down

0 comments on commit 4d62dac

Please sign in to comment.