Skip to content

Commit

Permalink
refactor(dialog/alert): allow defining cancel button
Browse files Browse the repository at this point in the history
Using the same promise type of API that the input dialog is using, if the user closes the dialog via esc or the cancel button the promise will resolve `cancel` otherwise the promise will resolve `confirm`
  • Loading branch information
LukeWasTakenn committed Jun 17, 2022
1 parent 4a20a0c commit 3034a32
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
11 changes: 10 additions & 1 deletion resource/interface/client/alert.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
local alert = nil

function lib.alertDialog(data)
if alert then return end
alert = promise.new()

SetNuiFocus(true, true)
SendNUIMessage({
action = 'sendAlert',
data = data
})

return Citizen.Await(alert)
end

RegisterNUICallback('closeAlert', function(_, cb)
RegisterNUICallback('closeAlert', function(data, cb)
cb(1)
SetNuiFocus(false, false)
alert:resolve(data)
alert = nil
end)

RegisterNetEvent('ox_lib:alertDialog', lib.alertDialog)
20 changes: 16 additions & 4 deletions web/src/features/dialog/AlertDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface DialogProps {
header: string;
content: string;
centered?: boolean;
cancel?: boolean;
}

debugData<DialogProps>([
Expand All @@ -28,6 +29,7 @@ debugData<DialogProps>([
header: "Hello there",
content: "General kenobi \n Markdown works",
centered: true,
cancel: true,
},
},
]);
Expand All @@ -41,9 +43,9 @@ const AlertDialog: React.FC = () => {
content: "",
});

const closeAlert = () => {
const closeAlert = (button: string) => {
onClose();
fetchNui("closeAlert");
fetchNui("closeAlert", button);
};

useNuiEvent("sendAlert", (data: DialogProps) => {
Expand All @@ -59,7 +61,7 @@ const AlertDialog: React.FC = () => {
isOpen={isOpen}
isCentered={dialogData.centered}
closeOnOverlayClick={false}
onEsc={closeAlert}
onEsc={() => closeAlert("cancel")}
>
<AlertDialogOverlay />
<AlertDialogContent fontFamily="Inter">
Expand All @@ -70,7 +72,17 @@ const AlertDialog: React.FC = () => {
<ReactMarkdown>{dialogData.content}</ReactMarkdown>
</AlertDialogBody>
<AlertDialogFooter>
<Button onClick={closeAlert}>{locale.ui.confirm}</Button>
{dialogData.cancel && (
<Button onClick={() => closeAlert("cancel")} mr={3}>
{locale.ui.cancel}
</Button>
)}
<Button
colorScheme={dialogData.cancel ? "blue" : undefined}
onClick={() => closeAlert("confirm")}
>
{locale.ui.confirm}
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</Dialog>
Expand Down
2 changes: 2 additions & 0 deletions web/src/providers/LocaleProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface Locale {
title: string;
language: string;
};
cancel: string;
close: string;
confirm: string;
};
Expand All @@ -67,6 +68,7 @@ const LocaleProvider: React.FC = ({ children }) => {
title: "",
language: "",
},
cancel: "",
close: "",
confirm: "",
},
Expand Down

0 comments on commit 3034a32

Please sign in to comment.