Skip to content

Commit

Permalink
Use dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
PzYon committed Jul 13, 2024
1 parent 915bc44 commit dd438a2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 57 deletions.
47 changes: 9 additions & 38 deletions app/src/components/details/scraps/QuickAddDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import React, { useState } from "react";
import { ScrapType } from "../../../serverApi/IScrapEntry";
import { ScrapsJournalType } from "../../../journalTypes/ScrapsJournalType";
import { styled, ToggleButton, ToggleButtonGroup } from "@mui/material";
import { ListScrapIcon, MarkdownScrapIcon } from "./ScrapsViewPage";
import { styled } from "@mui/material";
import { JournalSelector } from "../../common/JournalSelector";
import { Scrap } from "./Scrap";
import { UserRole } from "../../../serverApi/UserRole";
import { useAppContext } from "../../../AppContext";
import { getPermissionsForUser } from "../../overview/journals/useJournalPermissions";
import { ScrapType } from "../../../serverApi/IScrapEntry";

export const QuickAddDialog: React.FC<{
targetJournalId: string;
onSuccess?: () => void;
}> = ({ targetJournalId, onSuccess }) => {
const { user } = useAppContext();

const [type, setType] = useState<ScrapType>(ScrapType.Markdown);

const scrap = ScrapsJournalType.createBlank(true, targetJournalId, type);
const scrap = ScrapsJournalType.createBlank(
true,
targetJournalId,
ScrapType.Markdown,
);

const [journalId, setJournalId] = useState(scrap.parentId);

Expand All @@ -37,52 +38,22 @@ export const QuickAddDialog: React.FC<{
onChange={(journal) => setJournalId(journal.id)}
selectedJournalId={journalId}
/>
<ScrapTypeSelector>
<ToggleButtonGroup
value={type}
exclusive
sx={{ width: "100%", ".MuiButtonBase-root": { width: "50%" } }}
onChange={(_, value) => {
setType(
value !== null
? value
: type === ScrapType.Markdown
? ScrapType.List
: ScrapType.Markdown,
);
}}
>
<ToggleButton value={ScrapType.Markdown} color="primary">
<MarkdownScrapIcon />
&nbsp;Markdown
</ToggleButton>
<ToggleButton value={ScrapType.List} color="primary">
<ListScrapIcon />
&nbsp;List
</ToggleButton>
</ToggleButtonGroup>
</ScrapTypeSelector>

<ScrapContainer>
<Scrap
key={type}
scrap={scrap}
propsRenderStyle={"none"}
actionsRenderStyle={"save-only"}
onSuccess={onSuccess}
isQuickAdd={true}
targetJournalId={journalId}
changeTypeWithoutConfirmation={true}
/>
</ScrapContainer>
</>
);
};

const ScrapTypeSelector = styled("div")`
padding-top: ${(p) => p.theme.spacing(2)};
display: flex;
justify-content: start;
`;

const ScrapContainer = styled("div")`
padding-top: ${(p) => p.theme.spacing(2)};
margin-top: ${(p) => p.theme.spacing(2)};
Expand Down
3 changes: 3 additions & 0 deletions app/src/components/details/scraps/Scrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const Scrap: React.FC<{
giveFocus?: () => void;
isQuickAdd?: boolean;
targetJournalId?: string;
changeTypeWithoutConfirmation?: boolean;
}> = ({
scrap: currentScrap,
journal,
Expand All @@ -28,6 +29,7 @@ export const Scrap: React.FC<{
giveFocus,
isQuickAdd,
targetJournalId,
changeTypeWithoutConfirmation,
}) => {
const [doRender, setDoRender] = useState(hasFocus);

Expand All @@ -52,6 +54,7 @@ export const Scrap: React.FC<{
giveFocus={giveFocus}
isQuickAdd={isQuickAdd}
targetJournalId={targetJournalId}
changeTypeWithoutConfirmation={changeTypeWithoutConfirmation}
>
<ScrapInner />
</ScrapContextProvider>
Expand Down
45 changes: 26 additions & 19 deletions app/src/components/details/scraps/ScrapContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const ScrapContextProvider: React.FC<{
giveFocus?: () => void;
isQuickAdd?: boolean;
targetJournalId?: string;
changeTypeWithoutConfirmation?: boolean;
}> = ({
children,
currentScrap,
Expand All @@ -44,6 +45,7 @@ export const ScrapContextProvider: React.FC<{
giveFocus,
isQuickAdd,
targetJournalId,
changeTypeWithoutConfirmation,
}) => {
const { setAppAlert } = useAppContext();
const { renderDialog } = useDialogContext();
Expand Down Expand Up @@ -162,6 +164,23 @@ export const ScrapContextProvider: React.FC<{
genericNotes: string[],
targetType: ScrapType,
) {
function changeType() {
const newNotes = convertNotesToTargetType(targetType, genericNotes);

setNotes(newNotes);

setScrapToRender({
...scrapToRender,
notes: newNotes,
scrapType: targetType,
});
}

if (changeTypeWithoutConfirmation) {
changeType();
return;
}

renderDialog({
title: "Are you sure?",
render: (closeDialog) => {
Expand All @@ -179,19 +198,7 @@ export const ScrapContextProvider: React.FC<{
<Button
variant={"outlined"}
onClick={() => {
const newNotes = convertNotesToTargetType(
targetType,
genericNotes,
);

setNotes(newNotes);

setScrapToRender({
...scrapToRender,
notes: newNotes,
scrapType: targetType,
});

changeType();
closeDialog();
}}
>
Expand Down Expand Up @@ -284,23 +291,23 @@ export const ScrapContextProvider: React.FC<{

await upsertEntryMutation.mutateAsync({
command: {
id: currentScrap.id,
scrapType: currentScrap.scrapType,
id: scrapToRender.id,
scrapType: scrapToRender.scrapType,
notes: notesToSave,
title: parsedDate?.text ?? title,
journalAttributeValues: {},
journalId: targetJournalId ?? currentScrap.parentId,
journalId: targetJournalId ?? scrapToRender.parentId,
dateTime: new Date(),
schedule: getScheduleDefinition(
parsedDate,
currentScrap.parentId,
currentScrap?.id ?? "{0}",
scrapToRender.parentId,
scrapToRender?.id ?? "{0}",
),
} as IUpsertScrapsEntryCommand,
});

AddNewScrapStorage.clearForJournal(
isQuickAdd ? "quick-add" : currentScrap.parentId,
isQuickAdd ? "quick-add" : scrapToRender.parentId,
);
}

Expand Down

0 comments on commit dd438a2

Please sign in to comment.