-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #525 from alan-turing-institute/ui-updates
Some simple ui updates
- Loading branch information
Showing
8 changed files
with
289 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use client' | ||
|
||
import React, { Dispatch, SetStateAction, useEffect, useState } from 'react' | ||
import CaseSheet from '../ui/case-sheet' | ||
import useStore from '@/data/store' | ||
import CaseEditForm from './CaseEditForm' | ||
import { AlertModal } from '../modals/alertModal' | ||
|
||
interface CaseDetailsProps { | ||
isOpen: boolean | ||
setOpen: Dispatch<SetStateAction<boolean>> | ||
} | ||
|
||
const CaseDetails = ({ isOpen, setOpen }: CaseDetailsProps) => { | ||
const [isMounted, setIsMounted] = useState(false); | ||
const [loading, setLoading] = useState(false) | ||
const [unresolvedChanges, setUnresolvedChanges] = useState(false) | ||
const [alertOpen, setAlertOpen] = useState(false) | ||
|
||
const { assuranceCase } = useStore(); | ||
|
||
useEffect(() => { | ||
setIsMounted(true); | ||
}, []); | ||
|
||
if (!isMounted) { | ||
return null; | ||
} | ||
|
||
const handleClose = () => { | ||
setOpen(false) | ||
setAlertOpen(false) | ||
setUnresolvedChanges(false) | ||
} | ||
|
||
const onChange = (open: boolean) => { | ||
if (unresolvedChanges) { | ||
setAlertOpen(true) | ||
} else { | ||
handleClose() | ||
} | ||
}; | ||
|
||
return ( | ||
<CaseSheet | ||
title={`Update Assurance Case`} | ||
description={`Use this form to update your assurance case name and description.`} | ||
isOpen={isOpen} | ||
onClose={handleClose} | ||
onChange={onChange} | ||
> | ||
<div className='my-6'> | ||
<CaseEditForm onClose={handleClose} setUnresolvedChanges={setUnresolvedChanges}/> | ||
<AlertModal | ||
isOpen={alertOpen} | ||
onClose={() => setAlertOpen(false)} | ||
onConfirm={handleClose} | ||
loading={loading} | ||
message={'You have changes that have not been updated. Would you like to discard these changes?'} | ||
confirmButtonText={'Yes, discard changes!'} | ||
cancelButtonText={'No, keep editing'} | ||
/> | ||
</div> | ||
</CaseSheet> | ||
) | ||
} | ||
|
||
export default CaseDetails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
'use client' | ||
|
||
import React, { Dispatch, SetStateAction, useEffect, useState } from 'react' | ||
import { | ||
Form, | ||
FormControl, | ||
FormDescription, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "@/components/ui/form" | ||
import { Input } from "@/components/ui/input" | ||
import { z } from "zod" | ||
import { zodResolver } from "@hookform/resolvers/zod" | ||
import { useForm } from "react-hook-form" | ||
import { Textarea } from "../ui/textarea" | ||
import { Button } from '../ui/button' | ||
import useStore from '@/data/store'; | ||
import { CloudFog, Loader, Loader2, LockIcon, LockKeyhole } from 'lucide-react' | ||
import { getLayoutedElements } from '@/lib/layout-helper' | ||
import { useLoginToken } from '@/hooks/useAuth' | ||
import { findItemById, updateAssuranceCase, updateAssuranceCaseNode, caseItemDescription } from '@/lib/case-helper' | ||
|
||
const formSchema = z.object({ | ||
name: z.string().min(2, { | ||
message: "Name must be at least 2 characters.", | ||
}).optional(), | ||
description: z.string().min(2, { | ||
message: "Description must be atleast 2 characters" | ||
}) | ||
}) | ||
|
||
interface CaseEditFormProps { | ||
onClose: () => void | ||
setUnresolvedChanges: Dispatch<SetStateAction<boolean>> | ||
}; | ||
|
||
const CaseEditForm: React.FC<CaseEditFormProps> = ({ | ||
onClose, | ||
setUnresolvedChanges | ||
}) => { | ||
const { assuranceCase, setAssuranceCase } = useStore(); | ||
const [token] = useLoginToken(); | ||
const [loading, setLoading] = useState(false) | ||
|
||
const form = useForm<z.infer<typeof formSchema>>({ | ||
resolver: zodResolver(formSchema), | ||
defaultValues: assuranceCase || { | ||
name: '', | ||
description: '' | ||
} | ||
}); | ||
|
||
async function onSubmit(values: z.infer<typeof formSchema>) { | ||
setLoading(true) | ||
const updateItem = { | ||
name: values.name, | ||
description: values.description | ||
} | ||
|
||
const url = `${process.env.NEXT_PUBLIC_API_URL}/api/cases/${assuranceCase.id}/` | ||
const requestOptions: RequestInit = { | ||
method: "PUT", | ||
headers: { | ||
Authorization: `Token ${token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(updateItem) | ||
} | ||
const response = await fetch(url, requestOptions); | ||
if(!response.ok) { | ||
console.log('Render a new error') | ||
} | ||
|
||
setLoading(false) | ||
setAssuranceCase({ ...assuranceCase, name: updateItem.name, description: updateItem.description }) | ||
onClose() | ||
} | ||
|
||
useEffect(() => { | ||
form.watch((values, { name }) => { | ||
if (name === 'description' || name === 'name') { | ||
setUnresolvedChanges(true); | ||
} | ||
}); | ||
}, [form.watch, setUnresolvedChanges]); | ||
|
||
return ( | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8 mt-6"> | ||
<FormField | ||
control={form.control} | ||
name="name" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Name</FormLabel> | ||
<FormControl> | ||
<Input {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="description" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Description</FormLabel> | ||
<FormControl> | ||
<Textarea rows={8} {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<div className='flex justify-start items-center gap-3'> | ||
<Button type="submit" className="bg-indigo-500 hover:bg-indigo-600 dark:text-white" disabled={loading}> | ||
{loading ? ( | ||
<span className='flex justify-center items-center gap-2'><Loader2 className='w-4 h-4 animate-spin' />Updating...</span> | ||
) : ( | ||
<span>Update</span> | ||
)} | ||
</Button> | ||
</div> | ||
</form> | ||
</Form> | ||
) | ||
} | ||
|
||
export default CaseEditForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.