From 0c7415df28bd3016b4b44d1a33722cf625637af8 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Tue, 24 Oct 2023 15:29:20 +0200 Subject: [PATCH] code refactoring --- frontend/src/components/DownloadDialog.tsx | 4 +++- frontend/src/lib/httpClient.ts | 10 ++++----- frontend/src/views/Archive.tsx | 26 ++++++++++++++-------- frontend/src/views/Login.tsx | 23 ++++++++++++++++--- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/frontend/src/components/DownloadDialog.tsx b/frontend/src/components/DownloadDialog.tsx index 7521e0d..0259746 100644 --- a/frontend/src/components/DownloadDialog.tsx +++ b/frontend/src/components/DownloadDialog.tsx @@ -171,7 +171,9 @@ export default function DownloadDialog({ const resetInput = () => { urlInputRef.current!.value = '' - customFilenameInputRef.current!.value = '' + if (customFilenameInputRef.current) { + customFilenameInputRef.current!.value = '' + } } return ( diff --git a/frontend/src/lib/httpClient.ts b/frontend/src/lib/httpClient.ts index 7fdeb01..e97a52f 100644 --- a/frontend/src/lib/httpClient.ts +++ b/frontend/src/lib/httpClient.ts @@ -1,13 +1,11 @@ import { tryCatch } from 'fp-ts/TaskEither' -import { flow } from 'fp-ts/lib/function' -export const ffetch = (url: string, opt?: RequestInit) => flow( - tryCatch( - () => fetcher(url, opt), - (e) => `error while fetching: ${e}` - ) +export const ffetch = (url: string, opt?: RequestInit) => tryCatch( + () => fetcher(url, opt), + (e) => `error while fetching: ${e}` ) + const fetcher = async (url: string, opt?: RequestInit) => { const res = await fetch(url, opt) diff --git a/frontend/src/views/Archive.tsx b/frontend/src/views/Archive.tsx index 49acc16..d350258 100644 --- a/frontend/src/views/Archive.tsx +++ b/frontend/src/views/Archive.tsx @@ -66,7 +66,7 @@ export default function Downloaded() { ), matchW( (e) => { - pushMessage(e) + pushMessage(e, 'error') navigate('/login') }, (d) => files$.next(d), @@ -87,24 +87,32 @@ export default function Downloaded() { ? ['.', ..._upperLevel].join('/') : _upperLevel.join('/') - fetch(`${serverAddr}/archive/downloaded`, { + const task = ffetch(`${serverAddr}/archive/downloaded`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ subdir: relpath }) }) - .then(res => res.json()) - .then(data => { - files$.next(sub + + pipe( + task, + matchW( + (l) => pushMessage(l, 'error'), + (r) => files$.next(sub ? [{ - name: '..', isDirectory: true, + isVideo: false, + modTime: '', + name: '..', path: upperLevel, - }, ...data] - : data + shaSum: '', + size: 0, + }, ...r] + : r ) - }) + ) + )() } const selectable$ = useMemo(() => files$.pipe( diff --git a/frontend/src/views/Login.tsx b/frontend/src/views/Login.tsx index 73f2d9d..2b92fa3 100644 --- a/frontend/src/views/Login.tsx +++ b/frontend/src/views/Login.tsx @@ -15,6 +15,10 @@ import { useState } from 'react' import { useNavigate } from 'react-router-dom' import { useRecoilValue } from 'recoil' 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)({ display: 'flex', @@ -42,13 +46,15 @@ export default function Login() { const navigate = useNavigate() + const { pushMessage } = useToast() + const navigateAndReload = () => { navigate('/') window.location.reload() } const login = async () => { - const res = await fetch(`${url}/auth/login`, { + const task = ffetch(`${url}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' @@ -56,9 +62,20 @@ export default function Login() { body: JSON.stringify({ username, password, - }) + }), + redirect: 'follow' }) - res.ok ? navigateAndReload() : setFormHasError(true) + + pipe( + task, + matchW( + (l) => { + setFormHasError(true) + pushMessage(l, 'error') + }, + () => navigateAndReload() + ) + )() } return (