11"use client"
2- import { ClientConfig , Session } from "@/folderharborweb" ;
3- import query from "@/utils/api" ;
2+ import { Session } from "@/folderharborweb" ;
3+ import { getClient , handleError } from "@/utils/api" ;
44import { db } from "@/utils/db" ;
5+ import { FHClientConfig , FHSelfInfo } from "@folderharbor/sdk" ;
56import { faLock , faTrash } from "@fortawesome/free-solid-svg-icons" ;
67import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" ;
78import { useRouter } from "next/navigation" ;
89import { useEffect , useState } from "react" ;
910
10- type SelfInfo = { id : number , username : string , sessions : { id : number , createdAt : string , expiry : string } [ ] , activeSession : number , failedLoginLockout : boolean , permissions : string [ ] } ;
1111export default function Settings ( ) {
1212 const [ session , setSession ] = useState < Session | undefined > ( ) ;
13- const [ selfInfo , setSelfInfo ] = useState < SelfInfo | undefined > ( ) ;
14- const [ clientConfig , setClientConfig ] = useState < ClientConfig | undefined > ( ) ;
13+ const [ selfInfo , setSelfInfo ] = useState < FHSelfInfo | undefined > ( ) ;
14+ const [ clientConfig , setClientConfig ] = useState < FHClientConfig | undefined > ( ) ;
1515 useEffect ( ( ) => {
1616 async function loadSession ( ) { if ( localStorage . getItem ( "activeSession" ) ) setSession ( await db . sessions . get ( parseInt ( localStorage . getItem ( "activeSession" ) ! ) ) ) ; }
1717 loadSession ( ) ;
1818 } , [ ] ) ;
1919 useEffect ( ( ) => {
2020 async function loadSelfInfo ( ) {
2121 if ( ! session ) return ;
22- const res = await query ( session , "me" ) ;
23- if ( "error" in res ) {
24- alert ( res . error ) ;
25- return ;
26- }
27- if ( "redirect" in res ) {
28- window . location . href = res . redirect ;
29- return ;
22+ try {
23+ setSelfInfo ( await getClient ( session ) . me . info ( ) ) ;
24+ } catch ( e ) {
25+ const errBody = handleError ( e as Error ) ;
26+ if ( "error" in errBody ) alert ( errBody . error ) ;
27+ if ( "redirect" in errBody ) window . location . href = errBody . redirect ;
3028 }
31- setSelfInfo ( res . body ) ;
3229 }
3330 async function loadClientConfig ( ) {
3431 if ( ! session ) return ;
35- const res = await query ( session , "clientconfig" ) ;
36- if ( "error" in res ) {
37- alert ( res . error ) ;
38- return ;
39- }
40- if ( "redirect" in res ) {
41- window . location . href = res . redirect ;
42- return ;
32+ try {
33+ setClientConfig ( await getClient ( session ) . clientconfig ( ) ) ;
34+ } catch ( e ) {
35+ const errBody = handleError ( e as Error ) ;
36+ if ( "error" in errBody ) alert ( errBody . error ) ;
37+ if ( "redirect" in errBody ) window . location . href = errBody . redirect ;
4338 }
44- setClientConfig ( { selfUsernameChanges : res . body . selfUsernameChanges , registration : res . body . registration } ) ;
4539 }
4640 loadSelfInfo ( ) ;
4741 loadClientConfig ( ) ;
@@ -53,55 +47,49 @@ export default function Settings() {
5347 </ div >
5448 ) ;
5549}
56- function SettingsForm ( { session, selfInfo, clientConfig } : { session : Session , selfInfo : SelfInfo , clientConfig : ClientConfig } ) {
50+ function SettingsForm ( { session, selfInfo, clientConfig } : { session : Session , selfInfo : FHSelfInfo , clientConfig : FHClientConfig } ) {
5751 const router = useRouter ( ) ;
5852 const [ username , setUsername ] = useState < string > ( selfInfo . username ) ;
5953 const [ password , setPassword ] = useState < string > ( "" ) ;
6054 async function clearFailedLogins ( ) {
61- const res = await query ( session , "me" , { method : "PATCH" , body : JSON . stringify ( { clearLoginAttempts : true } ) } ) ;
62- if ( "error" in res ) {
63- alert ( res . error ) ;
64- return ;
55+ try {
56+ await getClient ( session ) . me . edit ( { clearLoginAttempts : true } ) ;
57+ alert ( "Successfully reset failed login attempts!" ) ;
58+ window . location . reload ( ) ;
59+ } catch ( e ) {
60+ const errBody = handleError ( e as Error ) ;
61+ if ( "error" in errBody ) alert ( errBody . error ) ;
62+ if ( "redirect" in errBody ) window . location . href = errBody . redirect ;
6563 }
66- if ( "redirect" in res ) {
67- window . location . href = res . redirect ;
68- return ;
69- }
70- alert ( "Successfully reset failed login attempts!" ) ;
71- window . location . reload ( ) ;
7264 }
7365 async function revokeSession ( id : number ) {
74- const res = await query ( session , "me/session" , { method : "DELETE" , body : JSON . stringify ( { sessionID : id } ) } ) ;
75- if ( "error" in res ) {
76- alert ( res . error ) ;
77- return ;
78- }
79- if ( "redirect " in res ) {
66+ try {
67+ await getClient ( session ) . me . revokeSession ( id ) ;
68+ window . location . reload ( ) ;
69+ } catch ( e ) {
70+ const errBody = handleError ( e as Error ) ;
71+ if ( "error " in errBody ) alert ( errBody . error ) ;
8072 // eslint-disable-next-line react-hooks/immutability
81- window . location . href = res . redirect ;
82- return ;
73+ if ( "redirect" in errBody ) window . location . href = errBody . redirect ;
8374 }
84- window . location . reload ( ) ;
8575 }
8676 async function updateInfo ( e : React . SubmitEvent ) {
8777 e . preventDefault ( ) ;
88- const res = await query ( session , "me" , { method : "PATCH" , body : JSON . stringify ( { username : ( username !== selfInfo . username ? username : undefined ) , password : ( password !== "" ? password : undefined ) } ) } ) ;
89- if ( "error" in res ) {
90- alert ( res . error ) ;
91- return ;
92- }
93- if ( "redirect" in res ) {
94- window . location . href = res . redirect ;
95- return ;
96- }
97- alert ( `Updated your info successfully!${ password !== "" ? `\nYou have been signed out of all sessions, and will need to log in again.\nAs a reminder, your server address is "${ session . server } ", and your username is "${ username !== selfInfo . username ? username : selfInfo . username } ".` : "" } ` ) ;
98- if ( password !== "" ) {
99- db . sessions . delete ( parseInt ( localStorage . getItem ( "activeSession" ) ! ) ) ;
100- localStorage . removeItem ( "activeSession" ) ;
101- router . push ( "/" ) ;
102- return ;
78+ try {
79+ await getClient ( session ) . me . edit ( { username : ( username !== selfInfo . username ? username : undefined ) , password : ( password !== "" ? password : undefined ) } ) ;
80+ alert ( `Updated your info successfully!${ password !== "" ? `\nYou have been signed out of all sessions, and will need to log in again.\nAs a reminder, your server address is "${ session . server } ", and your username is "${ username !== selfInfo . username ? username : selfInfo . username } ".` : "" } ` ) ;
81+ if ( password !== "" ) {
82+ db . sessions . delete ( parseInt ( localStorage . getItem ( "activeSession" ) ! ) ) ;
83+ localStorage . removeItem ( "activeSession" ) ;
84+ router . push ( "/" ) ;
85+ return ;
86+ }
87+ if ( username !== selfInfo . username ) db . sessions . update ( parseInt ( localStorage . getItem ( "activeSession" ) ! ) , { username : username } ) ;
88+ } catch ( e ) {
89+ const errBody = handleError ( e as Error ) ;
90+ if ( "error" in errBody ) alert ( errBody . error ) ;
91+ if ( "redirect" in errBody ) window . location . href = errBody . redirect ;
10392 }
104- if ( username !== selfInfo . username ) db . sessions . update ( parseInt ( localStorage . getItem ( "activeSession" ) ! ) , { username : username } ) ;
10593 }
10694 return (
10795 < div className = { `grid grid-cols-1 ${ selfInfo . permissions . length >= 1 ? "md:grid-cols-3" : "md:grid-cols-2" } gap-4 md:gap-2 my-4` } >
0 commit comments