-
Notifications
You must be signed in to change notification settings - Fork 1
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 #46 from Code-4-Community/ml-patch-admin-notes
Patch Admin Notes
- Loading branch information
Showing
10 changed files
with
193 additions
and
14 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,55 @@ | ||
import { APIGatewayEvent } from 'aws-lambda'; | ||
import { createTableIfNotExists } from '../db/createTable.js'; | ||
import { updateDocumentAdminNotes } from '../db/utils.js'; | ||
import { AdminNotes, adminNotesSchema } from '../schema/schema.js'; | ||
/** | ||
* An HTTP post method to add one form to the QLDB table. | ||
*/ | ||
export const patchFormHandler = async (event: APIGatewayEvent) => { | ||
const headers = { | ||
'Access-Control-Allow-Headers': 'Content-Type, Access-Control-Allow-Origin', | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'PATCH', | ||
}; | ||
|
||
if (event.httpMethod !== 'PATCH') { | ||
return { | ||
statusCode: 400, | ||
headers, | ||
body: `patchMethod only accepts PATCH method, you tried: ${event.httpMethod} method.`, | ||
}; | ||
} | ||
// All log statements are written to CloudWatch | ||
console.info('received:', event); | ||
|
||
const id = event.pathParameters!.id!; | ||
const JSONbody = JSON.parse(event.body!); | ||
let notes: AdminNotes; | ||
try { | ||
notes = adminNotesSchema.parse(JSONbody); | ||
} catch (error) { | ||
const errorMessage = 'Admin notes does not match schema. Error: ' + error; | ||
console.error(errorMessage); | ||
return { | ||
statusCode: 400, | ||
headers, | ||
body: errorMessage, | ||
}; | ||
} | ||
|
||
try { | ||
await createTableIfNotExists(); | ||
await updateDocumentAdminNotes(id, notes); | ||
return { | ||
statusCode: 201, | ||
headers, | ||
}; | ||
} catch (error) { | ||
console.error(error); | ||
return { | ||
statusCode: 500, | ||
headers, | ||
body: 'Error updating database.', | ||
}; | ||
} | ||
}; |
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,78 @@ | ||
import { Button, Flex, Heading, Text, Textarea } from '@chakra-ui/react'; | ||
import { useState } from 'react'; | ||
import { AdminNotes } from '../../types/formSchema'; | ||
import { patchAdminNotes } from '../../utils/sendRequest'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
interface AdminNotesProps { | ||
notes: AdminNotes; | ||
} | ||
|
||
export const ViewAdminNotes: React.FC<AdminNotesProps> = ({ notes }) => { | ||
const [savedNotes, setSavedNotes] = useState(notes); | ||
const [newNote, setNewNote] = useState(''); | ||
const [error, setError] = useState<null | string>(null); | ||
|
||
const { id } = useParams(); | ||
const onSave = () => { | ||
if (id) { | ||
const newSavedNotes = [ | ||
{ note: newNote, updatedAt: new Date() }, | ||
...savedNotes, | ||
]; | ||
patchAdminNotes(id, newSavedNotes) | ||
.then(() => { | ||
setSavedNotes(newSavedNotes); | ||
setNewNote(''); | ||
}) | ||
.catch((e) => { | ||
setError('Error encountered while saving note.'); | ||
console.error(e); | ||
}); | ||
} | ||
}; | ||
return ( | ||
<Flex flexDirection="column" marginTop="40px"> | ||
<Heading size="md" marginBottom="16px"> | ||
Admin Notes | ||
</Heading> | ||
<Flex flexDirection="column" maxWidth="60ch"> | ||
{error && ( | ||
<Text color="red" marginBottom="8px"> | ||
{error} | ||
</Text> | ||
)} | ||
|
||
<Textarea | ||
placeholder="Create a new note" | ||
value={newNote} | ||
onChange={(e) => { | ||
setNewNote(e.target.value); | ||
}} | ||
/> | ||
|
||
<Button | ||
colorScheme="teal" | ||
margin="16px 0px 40px" | ||
isDisabled={newNote === ''} | ||
onClick={onSave} | ||
> | ||
Save | ||
</Button> | ||
</Flex> | ||
<Heading size="md" marginBottom="16px"> | ||
Previous Notes | ||
</Heading> | ||
{savedNotes | ||
.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime()) | ||
.map((note) => ( | ||
<Flex flexDirection="column" key={`${note.note}`}> | ||
<Text size="xs" color="gray.500" margin="4px"> | ||
Last updated: {note.updatedAt.toLocaleString()} | ||
</Text> | ||
<Textarea maxWidth="60ch" value={note.note} readOnly={true} /> | ||
</Flex> | ||
))} | ||
</Flex> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
// TODO: For testing locally, replace with aws api gateway url | ||
export const BASE_URL = | ||
'https://bk3ffpsl08.execute-api.us-east-1.amazonaws.com/Prod/'; | ||
'https://bk3ffpsl08.execute-api.us-east-1.amazonaws.com/Prod'; | ||
|
||
export const GET_ALL_FORMS_URL = `${BASE_URL}/forms`; | ||
|
||
export const GET_FORM_BY_ID_URL = (id: string) => `${BASE_URL}/form/${id}`; | ||
|
||
export const POST_FORM_URL = `${BASE_URL}/form`; | ||
|
||
export const PATCH_ADMIN_NOTES_URL = (id: string) => | ||
`${BASE_URL}/form/${id}/notes`; |
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