-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
191 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...menu-option-desktop-editor-open-single/menu-option-desktop-editor-open-single.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import MoreMenu from "../../atoms/more-menu/more-menu"; | ||
import MenuOptionDesktopEditorOpenSingle from "./menu-option-desktop-editor-open-single"; | ||
|
||
export default { | ||
title: "components/molecules/menu-option-desktop-editor-open-single" | ||
}; | ||
|
||
export const Default = () => { | ||
return ( | ||
<MoreMenu enableMoreMenu={true} setEnableMoreMenu={() => {}}> | ||
<MenuOptionDesktopEditorOpenSingle | ||
subPath="/test.jpg" | ||
collections={true} | ||
isReadOnly={false} | ||
/> | ||
</MoreMenu> | ||
); | ||
}; | ||
|
||
Default.storyName = "default (no dialog)"; | ||
|
||
export const ReadOnly = () => { | ||
return ( | ||
<MoreMenu enableMoreMenu={true} setEnableMoreMenu={() => {}}> | ||
<MenuOptionDesktopEditorOpenSingle subPath="/test.jpg" collections={true} isReadOnly={true} /> | ||
</MoreMenu> | ||
); | ||
}; | ||
|
||
ReadOnly.storyName = "readonly"; |
101 changes: 101 additions & 0 deletions
101
...lecules/menu-option-desktop-editor-open-single/menu-option-desktop-editor-open-single.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import React, { memo, useState } from "react"; | ||
import useFetch from "../../../hooks/use-fetch"; | ||
import useGlobalSettings from "../../../hooks/use-global-settings"; | ||
import useHotKeys from "../../../hooks/use-keyboard/use-hotkeys"; | ||
import { IEnvFeatures } from "../../../interfaces/IEnvFeatures"; | ||
import localization from "../../../localization/localization.json"; | ||
import FetchPost from "../../../shared/fetch/fetch-post"; | ||
import { Language } from "../../../shared/language"; | ||
import { UrlQuery } from "../../../shared/url-query"; | ||
import MenuOption from "../../atoms/menu-option/menu-option"; | ||
import Notification, { NotificationType } from "../../atoms/notification/notification"; | ||
|
||
interface IMenuOptionDesktopEditorOpenSingleProps { | ||
subPath: string; | ||
collections: boolean; | ||
isReadOnly: boolean; | ||
setEnableMoreMenu?: React.Dispatch<React.SetStateAction<boolean>>; | ||
} | ||
|
||
export async function OpenDesktopSingle( | ||
subPath: string, | ||
collections: boolean, | ||
setIsError: React.Dispatch<React.SetStateAction<string>>, | ||
messageDesktopEditorUnableToOpen: string, | ||
isReadOnly: boolean | ||
) { | ||
if (isReadOnly) { | ||
return; | ||
} | ||
const urlOpen = new UrlQuery().UrlApiDesktopEditorOpen(); | ||
|
||
const bodyParams = new URLSearchParams(); | ||
bodyParams.append("f", subPath); | ||
bodyParams.append("collections", collections.toString()); | ||
|
||
const openDesktopResult = await FetchPost(urlOpen, bodyParams.toString()); | ||
if (openDesktopResult.statusCode >= 300) { | ||
setIsError(messageDesktopEditorUnableToOpen); | ||
} | ||
} | ||
|
||
const MenuOptionDesktopEditorOpenSingle: React.FunctionComponent<IMenuOptionDesktopEditorOpenSingleProps> = | ||
memo(({ subPath, collections, isReadOnly }) => { | ||
// Check API to know if feature is needed! | ||
const featuresResult = useFetch(new UrlQuery().UrlApiFeaturesAppSettings(), "get"); | ||
const dataFeatures = featuresResult?.data as IEnvFeatures | undefined; | ||
|
||
// Get language keys | ||
const settings = useGlobalSettings(); | ||
const language = new Language(settings.language); | ||
const MessageDesktopEditorUnableToOpen = language.key( | ||
localization.MessageDesktopEditorUnableToOpen | ||
); | ||
|
||
// for showing a notification | ||
const [isError, setIsError] = useState(""); | ||
|
||
/** | ||
* Open editor with keys | ||
*/ | ||
useHotKeys({ key: "e", ctrlKeyOrMetaKey: true }, () => { | ||
OpenDesktopSingle( | ||
subPath, | ||
collections, | ||
setIsError, | ||
MessageDesktopEditorUnableToOpen, | ||
isReadOnly | ||
).then(() => { | ||
// do nothing | ||
}); | ||
}); | ||
|
||
return ( | ||
<> | ||
{isError !== "" ? ( | ||
<Notification callback={() => setIsError("")} type={NotificationType.danger}> | ||
{isError} | ||
</Notification> | ||
) : null} | ||
|
||
{dataFeatures?.openEditorEnabled === true ? ( | ||
<MenuOption | ||
isReadOnly={isReadOnly} | ||
testName={"menu-option-desktop-editor-open"} | ||
onClickKeydown={() => | ||
OpenDesktopSingle( | ||
subPath, | ||
collections, | ||
setIsError, | ||
MessageDesktopEditorUnableToOpen, | ||
isReadOnly | ||
) | ||
} | ||
localization={localization.MessageDesktopEditorOpenSingleFile} | ||
/> | ||
) : null} | ||
</> | ||
); | ||
}); | ||
|
||
export default MenuOptionDesktopEditorOpenSingle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters