code refactoring

This commit is contained in:
2023-10-24 15:29:20 +02:00
parent 38d8bb8e40
commit 0c7415df28
4 changed files with 44 additions and 19 deletions

View File

@@ -171,8 +171,10 @@ export default function DownloadDialog({
const resetInput = () => { const resetInput = () => {
urlInputRef.current!.value = '' urlInputRef.current!.value = ''
if (customFilenameInputRef.current) {
customFilenameInputRef.current!.value = '' customFilenameInputRef.current!.value = ''
} }
}
return ( return (
<div> <div>

View File

@@ -1,13 +1,11 @@
import { tryCatch } from 'fp-ts/TaskEither' import { tryCatch } from 'fp-ts/TaskEither'
import { flow } from 'fp-ts/lib/function'
export const ffetch = <T>(url: string, opt?: RequestInit) => flow( export const ffetch = <T>(url: string, opt?: RequestInit) => tryCatch(
tryCatch(
() => fetcher<T>(url, opt), () => fetcher<T>(url, opt),
(e) => `error while fetching: ${e}` (e) => `error while fetching: ${e}`
)
) )
const fetcher = async <T>(url: string, opt?: RequestInit) => { const fetcher = async <T>(url: string, opt?: RequestInit) => {
const res = await fetch(url, opt) const res = await fetch(url, opt)

View File

@@ -66,7 +66,7 @@ export default function Downloaded() {
), ),
matchW( matchW(
(e) => { (e) => {
pushMessage(e) pushMessage(e, 'error')
navigate('/login') navigate('/login')
}, },
(d) => files$.next(d), (d) => files$.next(d),
@@ -87,24 +87,32 @@ export default function Downloaded() {
? ['.', ..._upperLevel].join('/') ? ['.', ..._upperLevel].join('/')
: _upperLevel.join('/') : _upperLevel.join('/')
fetch(`${serverAddr}/archive/downloaded`, { const task = ffetch<DirectoryEntry[]>(`${serverAddr}/archive/downloaded`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ subdir: relpath }) body: JSON.stringify({ subdir: relpath })
}) })
.then(res => res.json())
.then(data => { pipe(
files$.next(sub task,
matchW(
(l) => pushMessage(l, 'error'),
(r) => files$.next(sub
? [{ ? [{
name: '..',
isDirectory: true, isDirectory: true,
isVideo: false,
modTime: '',
name: '..',
path: upperLevel, path: upperLevel,
}, ...data] shaSum: '',
: data size: 0,
}, ...r]
: r
) )
}) )
)()
} }
const selectable$ = useMemo(() => files$.pipe( const selectable$ = useMemo(() => files$.pipe(

View File

@@ -15,6 +15,10 @@ import { useState } from 'react'
import { useNavigate } from 'react-router-dom' import { useNavigate } from 'react-router-dom'
import { useRecoilValue } from 'recoil' import { useRecoilValue } from 'recoil'
import { serverURL } from '../atoms/settings' import { serverURL } from '../atoms/settings'
import { useToast } from '../hooks/toast'
import { ffetch } from '../lib/httpClient'
import { matchW } from 'fp-ts/lib/TaskEither'
import { pipe } from 'fp-ts/lib/function'
const LoginContainer = styled(Container)({ const LoginContainer = styled(Container)({
display: 'flex', display: 'flex',
@@ -42,13 +46,15 @@ export default function Login() {
const navigate = useNavigate() const navigate = useNavigate()
const { pushMessage } = useToast()
const navigateAndReload = () => { const navigateAndReload = () => {
navigate('/') navigate('/')
window.location.reload() window.location.reload()
} }
const login = async () => { const login = async () => {
const res = await fetch(`${url}/auth/login`, { const task = ffetch(`${url}/auth/login`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@@ -56,9 +62,20 @@ export default function Login() {
body: JSON.stringify({ body: JSON.stringify({
username, username,
password, password,
}),
redirect: 'follow'
}) })
})
res.ok ? navigateAndReload() : setFormHasError(true) pipe(
task,
matchW(
(l) => {
setFormHasError(true)
pushMessage(l, 'error')
},
() => navigateAndReload()
)
)()
} }
return ( return (