-
-
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.
Merge pull request #428 from shawakash/client_set
feat: adds local update button
- Loading branch information
Showing
25 changed files
with
720 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"use client"; | ||
import { CommandMenu } from '@/components/combo-box'; | ||
import { Button } from '@/components/ui/button'; | ||
import { langs } from '@/lib/contants'; | ||
import { updateLocale } from '@/lib/helper'; | ||
import { getLocaleName } from '@/lib/utils'; | ||
import { Locales } from '@paybox/common'; | ||
import { clientJwtAtom, localeAtom } from '@paybox/recoil'; | ||
import { ChevronRight } from 'lucide-react'; | ||
import React, { useEffect } from 'react' | ||
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; | ||
import { toast } from 'sonner'; | ||
|
||
function LocaleButton({ | ||
locale | ||
}: { | ||
locale: Locales | ||
}) { | ||
const [open, setOpen] = React.useState(false); | ||
let [changedLocale, setChangedLocale] = React.useState<string>(); | ||
const [localeState, setLocale] = useRecoilState(localeAtom); | ||
const jwt = useRecoilValue(clientJwtAtom); | ||
|
||
useEffect(() => { | ||
setLocale(locale); | ||
}, [locale]); | ||
|
||
useEffect(() => { | ||
if (changedLocale) { | ||
if(jwt) { | ||
toast.promise(updateLocale(jwt, changedLocale as Locales), { | ||
loading: 'Updating locale...', | ||
success: () => { | ||
setLocale(changedLocale as Locales); | ||
return 'Locale updated successfully'; | ||
}, | ||
error: ({msg}) => { | ||
return msg || 'Failed to update locale'; | ||
} | ||
}); | ||
} else { | ||
toast.error('Auth Token not found. Please login again.'); | ||
|
||
} | ||
} | ||
}, [changedLocale]); | ||
|
||
return ( | ||
<> | ||
<div className='w-full' onClick={() => { | ||
setOpen(true); | ||
}}> | ||
<Button variant={"secondary"} className="w-full justify-between"> | ||
<span className="">Display Language</span> | ||
<span className="text-muted-foreground flex gap-x-1 items-center">{getLocaleName(locale)} <ChevronRight className="w-4 h-4" /></span> | ||
</Button> | ||
</div> | ||
<CommandMenu | ||
onSelect={setChangedLocale} | ||
options={langs} | ||
setOpen={setOpen} | ||
strokeKey='l' | ||
open={open} | ||
heading='Languages' | ||
selected={localeState} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
export default LocaleButton |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"use client"; | ||
import { | ||
Command, | ||
CommandDialog, | ||
CommandEmpty, | ||
CommandGroup, | ||
CommandInput, | ||
CommandItem, | ||
CommandList, | ||
CommandSeparator, | ||
CommandShortcut, | ||
} from "@/components/ui/command" | ||
import React from "react" | ||
import { Button } from "./ui/button" | ||
import { CheckCheck } from "lucide-react"; | ||
|
||
|
||
interface Option { | ||
name: string, | ||
value: string, | ||
} | ||
|
||
export function CommandMenu({ | ||
open, | ||
setOpen, | ||
onSelect, | ||
options, | ||
strokeKey, | ||
heading, | ||
selected | ||
}: { | ||
open: boolean, | ||
setOpen: React.Dispatch<React.SetStateAction<boolean>> | ||
onSelect: (value: string) => void, | ||
options: Option[], | ||
strokeKey: string, | ||
heading: string, | ||
selected: string | ||
}) { | ||
|
||
React.useEffect(() => { | ||
const down = (e: KeyboardEvent) => { | ||
if (e.key === strokeKey && (e.metaKey || e.ctrlKey)) { | ||
e.preventDefault() | ||
setOpen((open) => !open) | ||
} | ||
} | ||
document.addEventListener("keydown", down) | ||
return () => document.removeEventListener("keydown", down) | ||
}, []) | ||
|
||
return ( | ||
<CommandDialog open={open} onOpenChange={setOpen}> | ||
<CommandInput placeholder="Type to make life simpler..." /> | ||
<CommandList> | ||
<CommandEmpty>No results found.</CommandEmpty> | ||
<CommandGroup heading={heading}> | ||
{options.map((option) => ( | ||
<CommandItem | ||
className="" | ||
key={option.value} | ||
> | ||
<Button | ||
className="w-full h-fit gap-x-2 flex justify-between p-0 cursor-pointer border-none bg-transparent" variant={"secondary"} | ||
onClick={() => { | ||
setOpen(false); | ||
onSelect(option.value); | ||
}} | ||
> | ||
<div className="">{option.name}</div> | ||
{selected === option.value && <CheckCheck />} | ||
</Button> | ||
</CommandItem> | ||
))} | ||
</CommandGroup> | ||
<CommandSeparator /> | ||
</CommandList> | ||
|
||
|
||
</CommandDialog> | ||
) | ||
} |
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,13 @@ | ||
export const langs = [ | ||
{ name: 'English', value: 'en' }, | ||
{ name: 'Español', value: 'es' }, | ||
{ name: 'Français', value: 'fr' }, | ||
{ name: 'Deutsch', value: 'de' }, | ||
{ name: 'Italiano', value: 'it' }, | ||
{ name: 'Português', value: 'pt' }, | ||
{ name: 'Русский', value: 'ru' }, | ||
{ name: '中文', value: 'zh' }, | ||
{ name: '日本語', value: 'ja' }, | ||
{ name: '한국어', value: 'ko' }, | ||
{ name: 'हिन्दी', value: 'hi' }, | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { Locales } from "@paybox/common"; | ||
import { type ClassValue, clsx } from "clsx"; | ||
import { twMerge } from "tailwind-merge"; | ||
import { langs } from "./contants"; | ||
|
||
export function cn(...inputs: ClassValue[]) { | ||
return twMerge(clsx(inputs)); | ||
} | ||
|
||
export const getLocaleName = (locale: Locales): string => { | ||
return langs.find(({value}) => value == locale)?.name || ""; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Chain } from "@paybox/zeus"; | ||
import { HASURA_URL, JWT } from "../config"; | ||
import { HASURA_ADMIN_SERCRET, Locales, dbResStatus } from "@paybox/common"; | ||
|
||
const chain = Chain(HASURA_URL, { | ||
headers: { | ||
Authorization: `Bearer ${JWT}`, | ||
"x-hasura-admin-secret": HASURA_ADMIN_SERCRET, | ||
}, | ||
}); | ||
|
||
/** | ||
* @param clientId | ||
*/ | ||
export const getLocale = async ( | ||
clientId: string | ||
): Promise<{ | ||
status: dbResStatus, | ||
locale?: Locales | ||
}> => { | ||
const response = await chain("query")({ | ||
client_settings: [{ | ||
limit: 1, | ||
where: { | ||
clientId: {_eq: clientId} | ||
}, | ||
}, { | ||
locale: true | ||
}] | ||
}, {operationName: "getLocale"}); | ||
if(response.client_settings[0].locale) { | ||
return { | ||
locale: response.client_settings[0].locale as Locales, | ||
status: dbResStatus.Ok | ||
} | ||
} | ||
return { | ||
status: dbResStatus.Error | ||
} | ||
} |
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
Oops, something went wrong.