-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathCreateCsvAccessSourcePage.tsx
More file actions
170 lines (155 loc) · 4.86 KB
/
CreateCsvAccessSourcePage.tsx
File metadata and controls
170 lines (155 loc) · 4.86 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { formatError, type GraphQLError } from "@probo/helpers";
import { usePageTitle } from "@probo/hooks";
import { useTranslate } from "@probo/i18n";
import {
Button,
Card,
Field,
PageHeader,
useToast,
} from "@probo/ui";
import { type PreloadedQuery, useMutation, usePreloadedQuery } from "react-relay";
import { Link, useNavigate } from "react-router";
import { ConnectionHandler, graphql } from "relay-runtime";
import { z } from "zod";
import type { CreateAccessSourceDialogMutation } from "#/__generated__/core/CreateAccessSourceDialogMutation.graphql";
import type { CreateCsvAccessSourcePageQuery } from "#/__generated__/core/CreateCsvAccessSourcePageQuery.graphql";
import { useFormWithSchema } from "#/hooks/useFormWithSchema";
import { useOrganizationId } from "#/hooks/useOrganizationId";
import { createAccessSourceMutation } from "./dialogs/CreateAccessSourceDialog";
export const createCsvAccessSourcePageQuery = graphql`
query CreateCsvAccessSourcePageQuery($organizationId: ID!) {
organization: node(id: $organizationId) {
__typename
... on Organization {
id
canCreateSource: permission(action: "core:access-source:create")
}
}
}
`;
const csvSchema = z.object({
name: z.string().min(1),
csvData: z.string().min(1),
});
export default function CreateCsvAccessSourcePage({
queryRef,
}: {
queryRef: PreloadedQuery<CreateCsvAccessSourcePageQuery>;
}) {
const { __ } = useTranslate();
const { toast } = useToast();
const navigate = useNavigate();
const organizationId = useOrganizationId();
const { register, handleSubmit }
= useFormWithSchema(csvSchema, {
defaultValues: {
name: "",
csvData: "",
},
});
usePageTitle(__("Add CSV Access Source"));
const { organization } = usePreloadedQuery(createCsvAccessSourcePageQuery, queryRef);
if (organization.__typename !== "Organization") {
throw new Error("Organization not found");
}
const connectionId = ConnectionHandler.getConnectionID(
organization.id,
"AccessReviewSourcesTab_accessSources",
);
const [createAccessSource, isCreating]
= useMutation<CreateAccessSourceDialogMutation>(
createAccessSourceMutation,
);
if (!organization.canCreateSource) {
return (
<Card padded>
<p className="text-txt-secondary text-sm">
{__("You do not have permission to create access sources.")}
</p>
</Card>
);
}
const onSubmit = (data: z.infer<typeof csvSchema>) => {
createAccessSource({
variables: {
input: {
organizationId,
connectorId: null,
name: data.name,
csvData: data.csvData,
},
connections: connectionId ? [connectionId] : [],
},
onCompleted(_, errors) {
if (errors?.length) {
toast({
title: __("Error"),
description: formatError(
__("Failed to create access source"),
errors as GraphQLError[],
),
variant: "error",
});
return;
}
toast({
title: __("Success"),
description: __("Access source created successfully."),
variant: "success",
});
void navigate(`/organizations/${organizationId}/access-reviews/sources`);
},
onError(error) {
toast({
title: __("Error"),
description: formatError(
__("Failed to create access source"),
error as GraphQLError,
),
variant: "error",
});
},
});
};
return (
<div className="space-y-6">
<PageHeader
title={__("Add CSV access source")}
description={__(
"Paste CSV content with a header row. This source will be saved and available in Access Reviews.",
)}
/>
<Card padded>
<form onSubmit={e => void handleSubmit(onSubmit)(e)} className="space-y-4">
<Field
label={__("Name")}
{...register("name")}
type="text"
required
/>
<Field
label={__("CSV Data")}
{...register("csvData")}
type="textarea"
placeholder="email,full_name,role,job_title,is_admin,active,external_id"
required
/>
<p className="text-txt-secondary text-sm">
{__("Supported columns: email, full_name, role, job_title, is_admin, active, external_id.")}
</p>
<div className="flex items-center justify-end gap-2">
<Button variant="secondary" asChild>
<Link to={`/organizations/${organizationId}/access-reviews/sources`}>
{__("Back")}
</Link>
</Button>
<Button disabled={isCreating} type="submit">
{__("Create")}
</Button>
</div>
</form>
</Card>
</div>
);
}