Skip to content

Commit

Permalink
Get it basically working
Browse files Browse the repository at this point in the history
  • Loading branch information
dsamojlenko committed Oct 22, 2024
1 parent 30e86d3 commit 20923f5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,80 @@ import React, { useCallback, useEffect, useState } from "react";
import { useTranslation } from "@i18n/client";
import { Button } from "@clientComponents/globals";
import { Dialog, useDialogRef } from "@formBuilder/components/shared/Dialog";
import { useCustomEvent } from "@lib/hooks/useCustomEvent";
import {
APIKeyCustomEventDetails,
CustomEventDetails,
EventKeys,
useCustomEvent,
} from "@lib/hooks/useCustomEvent";

interface ApiKeyEvents extends CustomEvent {

Check failure on line 13 in app/(gcforms)/[locale]/(form administration)/form-builder/[id]/components/dialogs/APIKeyDialog.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(gcforms)/[locale]/(form administration)/form-builder/[id]/components/dialogs/APIKeyDialog.tsx#L13

[@typescript-eslint/no-unused-vars] 'ApiKeyEvents' is defined but never used.
detail: {
onDownload: () => void;
};
detail: {
onDownload: () => void;
};
}


export const ApiKeyDialog = () => {
const dialog = useDialogRef();
const { Event } = useCustomEvent();
const { t } = useTranslation("form-builder");
const [handler, setHandler] = useState<ApiKeyEvents | null>(null);
const dialog = useDialogRef();
const { Event } = useCustomEvent();
const { t } = useTranslation("form-builder");
const [handler, setHandler] = useState<APIKeyCustomEventDetails | null>(null);

const [isOpen, setIsOpen] = useState(false);
const [isOpen, setIsOpen] = useState(false);

const handleClose = () => {
dialog.current?.close();
setIsOpen(false);
};
const handleClose = () => {
dialog.current?.close();
setIsOpen(false);
};

const handleOpenDialog = useCallback((detail: ApiKeyEvents) => {
if (detail) {
setHandler(detail);
setIsOpen(true);
}
}, []);
const handleOpenDialog = useCallback((detail: CustomEventDetails) => {
if (detail) {
setHandler(detail);
setIsOpen(true);
}
}, []);

useEffect(() => {
Event.on("open-api-key-dialog", handleOpenDialog({
detail: {
onDownload: () => {
alert("Download");
}
},
}));
return () => {
Event.off("open-api-key-dialog", handleOpenDialog());
};
}, [Event, handleOpenDialog]);
const handleSave = () => {
handler?.download();
handleClose();
};

useEffect(() => {
Event.on(EventKeys.openApiKeyDialog, handleOpenDialog);
return () => {
Event.off(EventKeys.openApiKeyDialog, handleOpenDialog);
};
}, [Event, handleOpenDialog]);

const actions = (
<>
<Button
theme="secondary"
onClick={() => {
dialog.current?.close();
handleClose && handleClose();
}}
>
{t("dynamicRow.dialog.cancel")}
</Button>
<Button
className="ml-5"
theme="primary"
onClick={() => {
alert("Save");
}}
dataTestId="confirm-delete"
>
{t("dynamicRow.dialog.save")}
</Button>
</>
);
const actions = (
<>
<Button
theme="secondary"
onClick={() => {
dialog.current?.close();
handleClose && handleClose();
}}
>
{t("dynamicRow.dialog.cancel")}
</Button>
<Button className="ml-5" theme="primary" onClick={handleSave} dataTestId="confirm-delete">
{t("dynamicRow.dialog.save")}
</Button>
</>
);

return (
<>
{isOpen && (
<Dialog
handleClose={handleClose}
dialogRef={dialog}
actions={actions}
title={t("dynamicRow.dialog.title")}
>
<div className="p-5">
Body
</div>
</Dialog>
)}
</>
);
};
return (
<>
{isOpen && (
<Dialog
handleClose={handleClose}
dialogRef={dialog}
actions={actions}
title={t("dynamicRow.dialog.title")}
>
<div className="p-5">Body</div>
</Dialog>
)}
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "../../actions";
import { useParams } from "next/navigation";
import { useState } from "react";
import { useCustomEvent } from "@lib/hooks/useCustomEvent";
import { EventKeys, useCustomEvent } from "@lib/hooks/useCustomEvent";
import { ApiKeyType } from "@lib/types/form-builder-types";

const _createKey = async (templateId: string) => {

Check failure on line 15 in app/(gcforms)/[locale]/(form administration)/form-builder/[id]/settings/api/components/client/ApiKey.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

app/(gcforms)/[locale]/(form administration)/form-builder/[id]/settings/api/components/client/ApiKey.tsx#L15

[@typescript-eslint/no-unused-vars] '_createKey' is assigned a value but never used.
Expand Down Expand Up @@ -48,10 +48,10 @@ export const ApiKey = ({ keyExists }: { keyExists?: boolean }) => {
if (Array.isArray(id)) return null;

const openDialog = () => {
Event.fire("open-api-key-dialog", {
Event.fire(EventKeys.openApiKeyDialog, {
download: () => {
downloadKey(JSON.stringify(key), id);
}
},
});
};

Expand All @@ -75,12 +75,14 @@ export const ApiKey = ({ keyExists }: { keyExists?: boolean }) => {
</Button>
</>
) : (
<Button theme="primary" onClick={async () => {
const key = await _createKey(id);
setKey(key);
openDialog();
}
}>
<Button
theme="primary"
onClick={async () => {
// const key = await _createKey(id);
setKey({ type: "", keyId: "", userId: "", key: "" });
openDialog();
}}
>
{t("settings.api.generateKey")}
</Button>
)}
Expand Down
26 changes: 17 additions & 9 deletions lib/hooks/useCustomEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ import { useRef } from "react";
* }
*/

export type CustomEventDetails<T = undefined> = T;
type FireFunction = <T>(eventName: string, detail?: CustomEventDetails<T>) => void;
export const EventKeys = {
openApiKeyDialog: "open-api-key-dialog",
} as const;


type Event = {
fire: FireFunction;
on: <T>(eventName: string, callback: (detail: CustomEventDetails<T>) => void) => void;
off: <T>(eventName: string, callback: (detail: CustomEventDetails<T>) => void) => void;
export type APIKeyCustomEventDetails = {
download: () => void;
};

export type CustomEventDetails = APIKeyCustomEventDetails | undefined;

// export type CustomEventDetails<T = undefined> = T;
// type FireFunction = <T>(eventName: string, detail?: CustomEventDetails<T>) => void;

// type Event = {
// fire: FireFunction;
// on: <T>(eventName: string, callback: (detail: CustomEventDetails<T>) => void) => void;
// off: <T>(eventName: string, callback: (detail: CustomEventDetails<T>) => void) => void;
// };

export const useCustomEvent = () => {
// Attach listeners to a documentRef instead of document directly
// https://www.preciousorigho.com/articles/a-better-way-to-create-event-listeners-in-react
Expand All @@ -30,14 +39,13 @@ export const useCustomEvent = () => {
documentRef.current = window.document;
}


const Event = {
/**
* Fire an event, pass an optional payload
* @param eventName string
* @param data CustomEventDetails
*/
fire: (eventName, detail) => {
fire: (eventName: string, detail: CustomEventDetails = undefined) => {
const event = new CustomEvent(eventName, { detail });
documentRef.current && documentRef.current.dispatchEvent(event);
},
Expand Down

0 comments on commit 20923f5

Please sign in to comment.