refactor: datepicker code

This commit is contained in:
Aaryan Khandelwal 2023-02-02 18:33:46 +05:30
parent b2eab805e9
commit 563921d0cf
6 changed files with 26 additions and 35 deletions

View File

@ -61,9 +61,8 @@ export const CycleModal: React.FC<CycleModalProps> = (props) => {
if (workspaceSlug && projectId) { if (workspaceSlug && projectId) {
const payload = { const payload = {
...formValues, ...formValues,
start_date: formValues.start_date ? renderDateFormat(formValues.start_date) : null,
end_date: formValues.end_date ? renderDateFormat(formValues.end_date) : null,
}; };
if (initialData) { if (initialData) {
updateCycle(initialData.id, payload); updateCycle(initialData.id, payload);
} else { } else {

View File

@ -208,7 +208,6 @@ export const CreateUpdateIssueModal: React.FC<IssuesModalProps> = ({
const payload: Partial<IIssue> = { const payload: Partial<IIssue> = {
...formData, ...formData,
target_date: formData.target_date ? renderDateFormat(formData.target_date ?? "") : null,
}; };
if (!data) await createIssue(payload); if (!data) await createIssue(payload);

View File

@ -111,8 +111,6 @@ export const CreateUpdateModuleModal: React.FC<Props> = ({ isOpen, setIsOpen, da
const payload: Partial<IModule> = { const payload: Partial<IModule> = {
...formData, ...formData,
start_date: formData.start_date ? renderDateFormat(formData.start_date ?? "") : null,
target_date: formData.target_date ? renderDateFormat(formData.target_date ?? "") : null,
}; };
if (!data) await createModule(payload); if (!data) await createModule(payload);

View File

@ -1,6 +1,8 @@
// react-datepicker // react-datepicker
import DatePicker from "react-datepicker"; import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css"; import "react-datepicker/dist/react-datepicker.css";
// helpers
import { renderDateFormat } from "helpers/date-time.helper";
type Props = { type Props = {
renderAs?: "input" | "button"; renderAs?: "input" | "button";
@ -30,16 +32,7 @@ export const CustomDatePicker: React.FC<Props> = ({
selected={value ? new Date(value) : null} selected={value ? new Date(value) : null}
onChange={(val) => { onChange={(val) => {
if (!val) onChange(null); if (!val) onChange(null);
else { else onChange(renderDateFormat(val));
const year = val.getFullYear();
let month: number | string = val.getMonth() + 1;
let date: number | string = val.getDate();
if (date < 10) date = `0${date}`;
if (month < 10) month = `0${month}`;
onChange(`${year}-${month}-${date}`);
}
}} }}
className={`${className} ${ className={`${className} ${
renderAs === "input" renderAs === "input"

View File

@ -1,4 +1,6 @@
export const renderDateFormat = (date: string | Date) => { export const renderDateFormat = (date: string | Date | null) => {
if (!date) return "N/A";
var d = new Date(date), var d = new Date(date),
month = "" + (d.getMonth() + 1), month = "" + (d.getMonth() + 1),
day = "" + d.getDate(), day = "" + d.getDate(),

View File

@ -108,25 +108,6 @@ const IssueDetailsPage: NextPage<UserAuth> = (props) => {
defaultValues, defaultValues,
}); });
useEffect(() => {
if (issueDetails) {
mutateIssueActivities();
reset({
...issueDetails,
blockers_list:
issueDetails.blockers_list ??
issueDetails.blocker_issues?.map((issue) => issue.blocker_issue_detail?.id),
blocked_list:
issueDetails.blocked_list ??
issueDetails.blocked_issues?.map((issue) => issue.blocked_issue_detail?.id),
assignees_list:
issueDetails.assignees_list ?? issueDetails.assignee_details?.map((user) => user.id),
labels_list: issueDetails.labels_list ?? issueDetails.labels,
labels: issueDetails.labels_list ?? issueDetails.labels,
});
}
}, [issueDetails, reset, mutateIssueActivities]);
const submitChanges = useCallback( const submitChanges = useCallback(
(formData: Partial<IIssue>) => { (formData: Partial<IIssue>) => {
if (!workspaceSlug || !projectId || !issueId) return; if (!workspaceSlug || !projectId || !issueId) return;
@ -186,6 +167,25 @@ const IssueDetailsPage: NextPage<UserAuth> = (props) => {
}); });
}; };
useEffect(() => {
if (issueDetails) {
mutateIssueActivities();
reset({
...issueDetails,
blockers_list:
issueDetails.blockers_list ??
issueDetails.blocker_issues?.map((issue) => issue.blocker_issue_detail?.id),
blocked_list:
issueDetails.blocked_list ??
issueDetails.blocked_issues?.map((issue) => issue.blocked_issue_detail?.id),
assignees_list:
issueDetails.assignees_list ?? issueDetails.assignee_details?.map((user) => user.id),
labels_list: issueDetails.labels_list ?? issueDetails.labels,
labels: issueDetails.labels_list ?? issueDetails.labels,
});
}
}, [issueDetails, reset, mutateIssueActivities]);
const isNotAllowed = props.isGuest || props.isViewer; const isNotAllowed = props.isGuest || props.isViewer;
return ( return (