Skip to content

Commit

Permalink
🍞 quick toast with api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
olgam4 committed Jul 13, 2022
1 parent 76e5706 commit 36e42fa
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
42 changes: 42 additions & 0 deletions src/components/toaster/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { Component } from 'solid-js'

type Toast = {
id: string
message: string
}

const createToaster = () => {
const [toasts, setToasts] = createSignal<Toast[]>([])

const toast = (message: string) => {
const id = Math.random().toString()
setTimeout(() => {
setToasts(toasts => toasts.filter(t => t.id !== id))
}, 1000)
setToasts(toasts => [...toasts, { id, message }])
}

return {
toasts,
toast,
}
}

interface Props extends ReturnType<typeof createToaster> {}

const Toaster: Component<Props> = (props) => {
return (
<div class="absolute p-5 top-0 right-0">
<For each={props.toasts()} children={({ message }) => (
<div class="bg-white p-2 mb-5 rounded">
{message}
</div>
)}/>
</div>
)
}

export {
createToaster,
Toaster as default,
}
35 changes: 28 additions & 7 deletions src/layouts/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Button from '@components/button'
import ReloadPrompt from '@components/reload'
import Form from '@components/form'
import { createInput } from '@components/form/input'
import Toaster, { createToaster } from '@components/toaster'

const ReloadPromptCheck = typeof window !== 'undefined' ?
() => <ReloadPrompt />
Expand All @@ -17,23 +18,39 @@ export default function () {
let linkRef: Ref<any>
const counter = createCounter()
const [_, { locale }] = useI18n()
const nameHook = createInput('name')
const toasterHook = createToaster()

const nextLanguage = () => {
const next = locale() === 'en' ? 'fr' : 'en'
locale(next)
}

const nameHook = createInput('name')

const onSubmit = () => {
linkRef.click()
}

const phoneCall = async () => {
const response = await fetch('/api/phone', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
phone: '+33123456789',
}),
})
const data = await response.json()

console.log(data)
toasterHook.toast(JSON.stringify(data))
}

return (
<div class="full flex-center flex-col bg-gray-100/75">
<ReloadPromptCheck />
<Image image={bat} />
<div class="flex -mt-10 mb-5">
<div class="flex -mt-10 items-center">
<Form inputs={[nameHook]} onSubmit={onSubmit} />
<Link ref={linkRef} href={`/hi/${nameHook.value()}`}>
<div class="i-carbon-arrow-right btn w-7 h-7" />
Expand All @@ -42,15 +59,19 @@ export default function () {
<div class="flex items-end space-x-6">
<Counter {...counter}/>
<a class="btn" href="https://github.com/olgam4/bat" target="_blank">
<div class="i-carbon-logo-github w-7 h-7" />
<div class="i-carbon-logo-github w-6 h-6" />
</a>
<Link href="/random">
<div class="btn i-carbon-location-hazard w-7 h-7" />
<Link href={Math.round((Math.random() * 100000)).toString()}>
<div class="btn i-carbon-location-hazard w-6 h-6" />
</Link>
<Button onClick={() => phoneCall()}>
<div class="i-carbon-phone-voice w-6 h-6" />
</Button>
<Button onClick={() => nextLanguage()}>
<div class="i-carbon-language w-7 h-7" />
<div class="i-carbon-language w-6 h-6" />
</Button>
</div>
<Toaster {...toasterHook} />
</div>
)
}
File renamed without changes.
2 changes: 1 addition & 1 deletion tailwind.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
plugins: [
Icons({
carbon: {
icons: ['language', 'arrow-left', 'arrow-right', 'location-hazard', 'logo-github', 'moonrise', 'face-dissatisfied-filled']
icons: ['language', 'arrow-left', 'arrow-right', 'location-hazard', 'logo-github', 'moonrise', 'face-dissatisfied-filled', 'phone-voice']
}
}),
],
Expand Down

1 comment on commit 36e42fa

@vercel
Copy link

@vercel vercel bot commented on 36e42fa Jul 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

bat – ./

bat.glo.quebec
bat-olgam4.vercel.app
bat-git-main-olgam4.vercel.app
bat-brown.vercel.app

Please sign in to comment.