-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathMaxProjectsSection.tsx
More file actions
115 lines (105 loc) · 3.54 KB
/
MaxProjectsSection.tsx
File metadata and controls
115 lines (105 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { Form } from "@remix-run/react";
import { useEffect, useState } from "react";
import { Button } from "~/components/primitives/Buttons";
import { FormError } from "~/components/primitives/FormError";
import { Header2 } from "~/components/primitives/Headers";
import { Input } from "~/components/primitives/Input";
import { Label } from "~/components/primitives/Label";
import { Paragraph } from "~/components/primitives/Paragraph";
import * as Property from "~/components/primitives/PropertyTable";
export const MAX_PROJECTS_INTENT = "set-max-projects";
export const MAX_PROJECTS_SAVED_VALUE = "max-projects";
type FieldErrors = Record<string, string[] | undefined> | null;
type Props = {
maximumProjectCount: number;
errors: FieldErrors;
savedJustNow: boolean;
isSubmitting: boolean;
};
export function MaxProjectsSection({
maximumProjectCount,
errors,
savedJustNow,
isSubmitting,
}: Props) {
const hasFieldErrors = !!errors && Object.keys(errors).length > 0;
const fieldError = (field: string) =>
errors && field in errors ? errors[field]?.[0] : undefined;
const [isEditing, setIsEditing] = useState(hasFieldErrors);
const [value, setValue] = useState(String(maximumProjectCount));
useEffect(() => {
if (hasFieldErrors) setIsEditing(true);
}, [hasFieldErrors]);
useEffect(() => {
if (savedJustNow && !hasFieldErrors) setIsEditing(false);
}, [savedJustNow, hasFieldErrors]);
return (
<section className="flex flex-col gap-3 rounded-md border border-charcoal-700 bg-charcoal-800 p-4">
<div className="flex items-center justify-between">
<Header2>Maximum projects</Header2>
{!isEditing && (
<Button
variant="tertiary/small"
onClick={() => setIsEditing(true)}
disabled={isSubmitting}
>
Edit
</Button>
)}
</div>
{savedJustNow && (
<div className="rounded-md border border-green-600/40 bg-green-600/10 px-3 py-2">
<Paragraph variant="small" className="text-green-500">
Saved.
</Paragraph>
</div>
)}
{!isEditing ? (
<Property.Table>
<Property.Item>
<Property.Label>Limit</Property.Label>
<Property.Value>
{maximumProjectCount.toLocaleString()}
</Property.Value>
</Property.Item>
</Property.Table>
) : (
<Form method="post" className="flex flex-col gap-3 pt-2">
<input type="hidden" name="intent" value={MAX_PROJECTS_INTENT} />
<div className="flex flex-col gap-1">
<Label>Maximum projects</Label>
<Input
name="maximumProjectCount"
type="number"
min={1}
value={value}
onChange={(e) => setValue(e.target.value)}
required
/>
<FormError>{fieldError("maximumProjectCount")}</FormError>
</div>
<div className="flex items-center gap-2">
<Button
type="submit"
variant="primary/medium"
disabled={isSubmitting || !value.trim()}
>
Save
</Button>
<Button
type="button"
variant="tertiary/medium"
onClick={() => {
setValue(String(maximumProjectCount));
setIsEditing(false);
}}
disabled={isSubmitting}
>
Cancel
</Button>
</div>
</Form>
)}
</section>
);
}