-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented warning when record existing
resolves #70 -refactored getLatestRecord
- Loading branch information
Showing
7 changed files
with
173 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
Button, | ||
Accordion, | ||
AccordionButton, | ||
AccordionItem, | ||
AccordionIcon, | ||
Box, | ||
AccordionPanel, | ||
Text, | ||
} from "@chakra-ui/react"; | ||
|
||
import Link from "next/link"; | ||
|
||
export function FormWarning({ heading, message, onHide }) { | ||
return ( | ||
<Box bg="yellow.900" m={2} borderRadius="md"> | ||
<Accordion w="100%" allowToggle> | ||
<AccordionItem borderStyle="none"> | ||
<h2> | ||
<AccordionButton> | ||
<Box as="span" flex="1" textAlign="left"> | ||
{heading} | ||
</Box> | ||
<AccordionIcon /> | ||
</AccordionButton> | ||
</h2> | ||
<AccordionPanel maxH="180px" overflow="auto" pb={4}> | ||
<Text>{message}</Text> | ||
<Button mt={2} variant="link" onClick={onHide}> | ||
Hide warning | ||
</Button> | ||
</AccordionPanel> | ||
</AccordionItem> | ||
</Accordion> | ||
</Box> | ||
); | ||
} |
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 was deleted.
Oops, something went wrong.
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,23 @@ | ||
import { getLatestRecord } from "serverActions/getLatestRecord"; | ||
|
||
export async function getDefaultValues() { | ||
/** | ||
* Getting initial values for the RecordForm | ||
* | ||
* @returns object to use as react-form-hook defaultValues for RecordForm | ||
* or null if no records in db | ||
*/ | ||
|
||
const latestRecord = await getLatestRecord(); | ||
if (latestRecord === null) return null; | ||
|
||
const initialValues = { | ||
date: latestRecord.date, | ||
institutions: latestRecord.institutions.map((institution) => ({ | ||
...institution, | ||
isDeleted: false, | ||
})), | ||
}; | ||
|
||
return initialValues; | ||
} |
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,15 @@ | ||
export function isInCurrentMonth(unixDateInMs) { | ||
/** | ||
* Check if provided unix date is in current month | ||
* | ||
* @returns boolean | ||
*/ | ||
|
||
const date = new Date(unixDateInMs); | ||
const now = new Date(); | ||
|
||
return ( | ||
date.getFullYear() === now.getFullYear() && | ||
date.getMonth() === now.getMonth() | ||
); | ||
} |
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,40 +1,36 @@ | ||
/* | ||
Server Action used to get latest record from mongodb. | ||
Latest record then transfomed into format, which is used by react-hook-form in RecordForm | ||
as defaultValues. | ||
*/ | ||
|
||
// TODO pass errors to RecordForm | ||
|
||
"use server"; | ||
|
||
import connect from "utils/mongooseConnect"; | ||
import Record from "models/record"; | ||
|
||
export async function getLatestRecord() { | ||
try { | ||
await connect(); | ||
const latestRecord = await Record.find() | ||
.sort({ createdAt: -1 }) | ||
.limit(1) | ||
.exec(); | ||
const institutionsList = latestRecord[0]?.institutions.toObject({ | ||
transform: function (doc, ret) { | ||
delete ret._id; | ||
return ret; | ||
}, | ||
}); | ||
/** | ||
* Getting latest record from mongodb | ||
* | ||
* @throws Error retreiving record, when mongoose can't perform Record.find | ||
* | ||
* @returns list of institutions with assets | ||
* to be used by react-hook-form in RecordForm as defaultValues | ||
*/ | ||
|
||
const initialValues = { | ||
institutions: institutionsList?.map((institution) => ({ | ||
...institution, | ||
isDeleted: false, | ||
})), | ||
}; | ||
await connect(); | ||
|
||
return initialValues; | ||
let recordsList = null; | ||
try { | ||
recordsList = await Record.find().sort({ createdAt: -1 }).limit(1).exec(); | ||
} catch (error) { | ||
throw error; | ||
throw new Error("Error retreiving record:", error.message); | ||
} | ||
|
||
if (recordsList.length === 0) return null; | ||
|
||
const latestRecord = recordsList[0].toObject({ | ||
transform: function (doc, ret) { | ||
delete ret._id; | ||
delete ret.__v; | ||
return ret; | ||
}, | ||
}); | ||
|
||
return latestRecord; | ||
} |
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