Skip to content

Commit

Permalink
dashboard/components: created MyAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
zeim839 committed Dec 20, 2023
1 parent 5abd504 commit de39021
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
35 changes: 35 additions & 0 deletions dashboard/components/MyAccount/MyAccount.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use client'

import { GetUser, IsAPISuccess } from '@/APIController/API'
import { redirect } from 'next/navigation'
import { useState } from 'react'
import { useCookies } from 'next-client-cookies'
import Modify from './modify'

import { Loading } from '@carbon/react'

export default function MyAccount() {
const cookies = useCookies()
const jwt = cookies.get('ows-jwt')
if (typeof jwt === "undefined") {
redirect("/authorize")
}

const [data, setData] = useState<Object | null>(null)
if (data === null) {
GetUser(jwt).then((res) => {
if (!IsAPISuccess(res)) {
cookies.remove('ows-jwt')
location.replace("/authorize")
return
}
setData(res)
}).catch((err) => {
cookies.remove('ows-jwt')
location.replace("/authorize")
})
return (<Loading withOverlay={true} />)
}

return (<Modify data={data} />)
}
133 changes: 133 additions & 0 deletions dashboard/components/MyAccount/modify.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
'use client'

import { useState } from 'react'
import { UpdateUser, IsAPISuccess, IsAPIFailure } from '@/APIController/API'
import { useCookies } from 'next-client-cookies'
import { Edit, EditOff } from '@carbon/icons-react'
import { Form, useTheme, TextInput, Button } from '@carbon/react'

const editButton = (isEditing : boolean, setIsEditing : Function) => {
if (isEditing) {
return (
<Button kind="danger" onClick={() => setIsEditing(false) }>
Cancel
<EditOff className="button--arrow" />
</Button>
)
}

return (
<Button onClick={() => setIsEditing(true) }>
Edit
<Edit className="button--arrow" />
</Button>
)
}

export default function Modify({ data } : any) {
const cookies = useCookies()

const headingColor = () => {
const { theme } = useTheme()
return (theme == "white") ? "black" : "white"
}

const [ isEditing, setIsEditing ] = useState(false)
const [ newData, setNewData ] = useState(data)
const [ hasError, setHasError ] = useState("")
const [ hasSuccess, setHasSuccess ] = useState(false)
const submitForm = async (e : any) => {
e.preventDefault()
setHasSuccess(false)
setHasError("")

if (newData.first_name.length > 20) {
setHasError("first name cannot be longer than 20 characters");
return
}

if (newData.last_name.length > 20) {
setHasError("last name cannot be longer than 20 characters");
return
}

if (newData.first_name.length < 2) {
setHasError("first name cannot be less than 2 characters");
return
}

if (newData.last_name.length < 2) {
setHasError("last name cannot be less than 2 characters");
return
}

UpdateUser(newData.first_name, newData.last_name, cookies.get('ows-jwt'))
.then((res) => {
if (IsAPISuccess(res)) {
setHasSuccess(true)
setIsEditing(false)
return
}

let msg = (IsAPIFailure(res) && typeof res.error != "undefined") ?
res.error : "An unknown error has occurred. Please try again later."

setHasError(msg)
}).catch((err) => {
setHasError("Server could not be reached. Please try again later")
})
}

return (
<div className="accountPage">
<Form style={{ width: "100%", maxWidth: "700px" }}>
<TextInput
id="email"
style={{ marginBottom: "15px" }}
disabled
value={data.email}
labelText="Email Address"
/>
<TextInput
id="first_name"
style={{ marginBottom: "15px" }}
value={newData.first_name}
labelText="First Name"
onChange={(e) => setNewData({ first_name: e.target.value,
last_name: newData.last_name }) }
disabled={ !isEditing }
/>
<TextInput
id="last_name"
style={{ marginBottom: "35px" }}
value={newData.last_name}
labelText="Last Name"
onChange={(e) => setNewData({ first_name: newData.first_name,
last_name: e.target.value }) }
disabled={ !isEditing }
/>
{ editButton(isEditing, setIsEditing) }
{
(isEditing) ? (
<Button type="submit" onClick={submitForm}
style={{ marginLeft: 10 }}>
Update
</Button>
) : null
}
{
(hasError != "") ? (
<p style={{ marginTop: 10, marginBottom: 5, color: 'red' }}>
Error: { hasError }
</p>) : null
}
{
(hasSuccess) ? (
<p style={{ marginTop: 10, marginBottom: 5, color: 'green' }}>
Account updated succesfully
</p>) : null
}
</Form>
</div>
)
}

0 comments on commit de39021

Please sign in to comment.