-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rwa): able to change the asset contract (#2686)
- Loading branch information
1 parent
0f3479b
commit 2237938
Showing
28 changed files
with
350 additions
and
38 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,2 @@ | ||
--- | ||
--- |
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
66 changes: 62 additions & 4 deletions
66
packages/apps/rwa-demo/src/components/AssetProvider/AssetProvider.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
98 changes: 98 additions & 0 deletions
98
packages/apps/rwa-demo/src/components/AssetSwitch/AssetForm.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,98 @@ | ||
import { LOCALSTORAGE_ASSETS_KEY } from '@/constants'; | ||
import { getLocalStorageKey } from '@/utils/getLocalStorageKey'; | ||
import { MonoAdd, MonoEditNote, MonoRemove } from '@kadena/kode-icons'; | ||
import { Button, TextField } from '@kadena/kode-ui'; | ||
import { useNotifications } from '@kadena/kode-ui/patterns'; | ||
import type { ChangeEventHandler, FC, FormEventHandler } from 'react'; | ||
import { useRef, useState } from 'react'; | ||
import type { IAsset } from '../AssetProvider/AssetProvider'; | ||
|
||
interface IProps { | ||
asset?: IAsset; | ||
} | ||
|
||
export const AssetForm: FC<IProps> = ({ asset }) => { | ||
const ref = useRef<HTMLInputElement | null>(null); | ||
const { addNotification } = useNotifications(); | ||
const [value, setValue] = useState(asset?.name ?? ''); | ||
|
||
const handleChange: ChangeEventHandler<HTMLInputElement> = (e) => { | ||
setValue(e.currentTarget.value); | ||
}; | ||
|
||
const handleRemove = (data: IAsset) => { | ||
const key = getLocalStorageKey(LOCALSTORAGE_ASSETS_KEY); | ||
const assets = JSON.parse(localStorage.getItem(key) ?? '[]').filter( | ||
(a: IAsset) => a.uuid !== data.uuid, | ||
); | ||
localStorage.setItem(key, JSON.stringify(assets)); | ||
window.dispatchEvent(new Event(key)); | ||
|
||
addNotification({ | ||
label: 'Asset removed', | ||
intent: 'negative', | ||
}); | ||
}; | ||
|
||
const handleSave: FormEventHandler<HTMLFormElement> = () => { | ||
const key = getLocalStorageKey(LOCALSTORAGE_ASSETS_KEY); | ||
const assets = JSON.parse(localStorage.getItem(key) ?? '[]'); | ||
|
||
let newAssets = [...assets]; | ||
if (!asset) { | ||
const newAsset = { | ||
uuid: crypto.randomUUID(), | ||
name: value, | ||
}; | ||
newAssets.push(newAsset); | ||
|
||
addNotification({ | ||
label: 'Asset added', | ||
intent: 'positive', | ||
}); | ||
} else { | ||
newAssets = newAssets.map((v) => { | ||
if (v.uuid === asset.uuid) { | ||
return { ...v, name: value }; | ||
} | ||
return v; | ||
}); | ||
|
||
addNotification({ | ||
label: 'Asset Changed', | ||
intent: 'positive', | ||
}); | ||
} | ||
|
||
setValue(''); | ||
localStorage.setItem(key, JSON.stringify(newAssets)); | ||
window.dispatchEvent(new Event(key)); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSave}> | ||
<TextField | ||
ref={ref} | ||
value={value} | ||
label="New" | ||
isRequired | ||
onChange={handleChange} | ||
endAddon={ | ||
<> | ||
<Button | ||
type="submit" | ||
isDisabled={!value} | ||
endVisual={!asset ? <MonoAdd /> : <MonoEditNote />} | ||
/> | ||
{asset?.uuid && ( | ||
<Button | ||
onPress={() => handleRemove(asset)} | ||
endVisual={<MonoRemove />} | ||
/> | ||
)} | ||
</> | ||
} | ||
/> | ||
</form> | ||
); | ||
}; |
70 changes: 70 additions & 0 deletions
70
packages/apps/rwa-demo/src/components/AssetSwitch/AssetSwitch.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,70 @@ | ||
import { useAsset } from '@/hooks/asset'; | ||
import { MonoSettings } from '@kadena/kode-icons'; | ||
import { | ||
Button, | ||
ContextMenu, | ||
ContextMenuDivider, | ||
ContextMenuItem, | ||
} from '@kadena/kode-ui'; | ||
import { | ||
RightAside, | ||
RightAsideContent, | ||
RightAsideHeader, | ||
useLayout, | ||
} from '@kadena/kode-ui/patterns'; | ||
import type { FC } from 'react'; | ||
import { useState } from 'react'; | ||
import { AssetForm } from './AssetForm'; | ||
|
||
export const AssetSwitch: FC = () => { | ||
const { assets, asset, setAsset } = useAsset(); | ||
const [openSide, setOpenSide] = useState(false); | ||
const { setIsRightAsideExpanded, isRightAsideExpanded } = useLayout(); | ||
|
||
return ( | ||
<> | ||
{isRightAsideExpanded && openSide && ( | ||
<RightAside | ||
isOpen | ||
onClose={() => { | ||
setIsRightAsideExpanded(false); | ||
setOpenSide(false); | ||
}} | ||
> | ||
<RightAsideHeader label="Assets" /> | ||
<RightAsideContent> | ||
{assets.map((ass) => ( | ||
<AssetForm key={ass.uuid} asset={ass} /> | ||
))} | ||
<AssetForm /> | ||
</RightAsideContent> | ||
</RightAside> | ||
)} | ||
<ContextMenu | ||
trigger={ | ||
<Button isCompact variant="outlined"> | ||
{asset ? asset.name : 'Select an asset'} | ||
</Button> | ||
} | ||
> | ||
{assets.map((ass) => ( | ||
<ContextMenuItem | ||
onClick={() => setAsset(ass)} | ||
key={ass.uuid} | ||
label={ass.name} | ||
/> | ||
))} | ||
<ContextMenuDivider /> | ||
|
||
<ContextMenuItem | ||
onClick={() => { | ||
setIsRightAsideExpanded(true); | ||
setOpenSide(true); | ||
}} | ||
endVisual={<MonoSettings />} | ||
label="Asset Settings" | ||
/> | ||
</ContextMenu> | ||
</> | ||
); | ||
}; |
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
Oops, something went wrong.