Skip to content

Commit

Permalink
add smtp form to global settings (#1372)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsladerman authored Sep 12, 2024
1 parent b71cc42 commit b95fe2d
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 7 deletions.
4 changes: 4 additions & 0 deletions assets/src/components/settings/global/GlobalSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const directory = [
path: 'observability',
label: 'Observability',
},
{
path: 'smtp',
label: 'SMTP',
},
]

type GlobalSettingsContextType = {
Expand Down
172 changes: 172 additions & 0 deletions assets/src/components/settings/global/GlobalSettingsSMTP.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import {
Button,
Card,
Flex,
Switch,
Toast,
ValidatedInput,
} from '@pluralsh/design-system'
import { useDeploymentSettings } from 'components/contexts/DeploymentSettingsContext'
import { GqlError } from 'components/utils/Alert'
import { ScrollablePage } from 'components/utils/layout/ScrollablePage'
import {
SmtpSettingsAttributes,
useUpdateDeploymentSettingsMutation,
} from 'generated/graphql'
import { FormEvent, useState } from 'react'
import { useTheme } from 'styled-components'

import { cleanSmtpForm } from '../usermanagement/email/EmailSettingsForm'

export function GlobalSettingsSMTP() {
const theme = useTheme()
const { smtp } = useDeploymentSettings()
const [form, setForm] = useState<SmtpSettingsAttributes>({
...(cleanSmtpForm(smtp) || defaultForm),
password: '',
})
const [showToast, setShowToast] = useState(false)

const [mutation, { loading, error }] = useUpdateDeploymentSettingsMutation({
variables: {
attributes: {
smtp: form,
},
},
onCompleted: () => {
setShowToast(true)
setForm({
...form,
password: '',
})
},
})

const allowSubmit = !!(
form.server &&
form.port &&
form.sender &&
form.user &&
form.password
)

const handleSubmit = (e: FormEvent) => {
e.preventDefault()
mutation()
}

return (
<ScrollablePage heading="SMTP Settings">
<form onSubmit={handleSubmit}>
<Card
css={{
display: 'flex',
flexDirection: 'column',
gap: theme.spacing.medium,
padding: theme.spacing.xlarge,
}}
>
{error && <GqlError error={error} />}
<ValidatedInput
label="Server"
placeholder="smtp.sendrid.net"
value={form.server || ''}
onChange={(e) =>
setForm({
...form,
server: e.target.value,
})
}
/>
<ValidatedInput
label="Port"
placeholder="587"
value={form.port || ''}
onChange={(e) =>
setForm({
...form,
port: parseInt(e.target.value),
})
}
/>
<ValidatedInput
label="Sender"
hint="From address for outgoing emails"
value={form.sender || ''}
onChange={(e) =>
setForm({
...form,
sender: e.target.value,
})
}
/>
<ValidatedInput
label="User"
hint="Username for SMTP authentication"
value={form.user || ''}
onChange={(e) =>
setForm({
...form,
user: e.target.value,
})
}
/>
<ValidatedInput
label="Password"
hint="Password for SMTP authentication"
type="password"
value={form.password || ''}
onChange={(e) =>
setForm({
...form,
password: e.target.value,
})
}
/>
<Flex justify="space-between">
<Switch
checked={form.ssl}
onChange={(checked) =>
setForm({
...form,
ssl: checked,
})
}
css={{
...theme.partials.text.body2Bold,
color: theme.colors.text,
}}
>
SSL
</Switch>
<Button
type="submit"
disabled={!allowSubmit}
loading={loading}
>
Update
</Button>
</Flex>
</Card>
</form>
<Toast
severity="success"
css={{
margin: theme.spacing.large,
}}
show={showToast}
onClose={() => setShowToast(false)}
>
SMTP settings updated successfully
</Toast>
</ScrollablePage>
)
}

const defaultForm = {
server: '',
port: 587,
sender: '',
user: '',
ssl: false,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { UPDATE_SMTP } from 'components/graphql/plural'
import { useState } from 'react'
import { useTheme } from 'styled-components'

const clean = (smtp) => {
export const cleanSmtpForm = (smtp) => {
const { __typename, ...vals } = smtp || {}

return vals
}

export default function EmailSettingsForm({ smtp }) {
const theme = useTheme()
const [form, setForm] = useState(clean(smtp))
const [form, setForm] = useState(cleanSmtpForm(smtp))
const [mutation, { loading }] = useMutation(UPDATE_SMTP, {
variables: { smtp: form },
})
Expand Down
24 changes: 20 additions & 4 deletions assets/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9043,19 +9043,21 @@ export type SyncGlobalServiceMutation = { __typename?: 'RootMutationType', syncG

export type HttpConnectionFragment = { __typename?: 'HttpConnection', host: string, user?: string | null };

export type DeploymentSettingsFragment = { __typename?: 'DeploymentSettings', id: string, name: string, enabled: boolean, selfManaged?: boolean | null, insertedAt?: string | null, updatedAt?: string | null, agentHelmValues?: string | null, lokiConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, prometheusConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, artifactRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, deployerRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, createBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, readBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, writeBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, gitBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null };
export type SmtpSettingsFragment = { __typename?: 'SmtpSettings', server: string, port: number, sender: string, user: string, ssl: boolean };

export type DeploymentSettingsFragment = { __typename?: 'DeploymentSettings', id: string, name: string, enabled: boolean, selfManaged?: boolean | null, insertedAt?: string | null, updatedAt?: string | null, agentHelmValues?: string | null, lokiConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, prometheusConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, artifactRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, deployerRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, createBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, smtp?: { __typename?: 'SmtpSettings', server: string, port: number, sender: string, user: string, ssl: boolean } | null, readBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, writeBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, gitBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null };

export type UpdateDeploymentSettingsMutationVariables = Exact<{
attributes: DeploymentSettingsAttributes;
}>;


export type UpdateDeploymentSettingsMutation = { __typename?: 'RootMutationType', updateDeploymentSettings?: { __typename?: 'DeploymentSettings', id: string, name: string, enabled: boolean, selfManaged?: boolean | null, insertedAt?: string | null, updatedAt?: string | null, agentHelmValues?: string | null, lokiConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, prometheusConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, artifactRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, deployerRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, createBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, readBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, writeBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, gitBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null } | null };
export type UpdateDeploymentSettingsMutation = { __typename?: 'RootMutationType', updateDeploymentSettings?: { __typename?: 'DeploymentSettings', id: string, name: string, enabled: boolean, selfManaged?: boolean | null, insertedAt?: string | null, updatedAt?: string | null, agentHelmValues?: string | null, lokiConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, prometheusConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, artifactRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, deployerRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, createBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, smtp?: { __typename?: 'SmtpSettings', server: string, port: number, sender: string, user: string, ssl: boolean } | null, readBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, writeBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, gitBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null } | null };

export type DeploymentSettingsQueryVariables = Exact<{ [key: string]: never; }>;


export type DeploymentSettingsQuery = { __typename?: 'RootQueryType', deploymentSettings?: { __typename?: 'DeploymentSettings', id: string, name: string, enabled: boolean, selfManaged?: boolean | null, insertedAt?: string | null, updatedAt?: string | null, agentHelmValues?: string | null, lokiConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, prometheusConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, artifactRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, deployerRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, createBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, readBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, writeBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, gitBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null } | null };
export type DeploymentSettingsQuery = { __typename?: 'RootQueryType', deploymentSettings?: { __typename?: 'DeploymentSettings', id: string, name: string, enabled: boolean, selfManaged?: boolean | null, insertedAt?: string | null, updatedAt?: string | null, agentHelmValues?: string | null, lokiConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, prometheusConnection?: { __typename?: 'HttpConnection', host: string, user?: string | null } | null, artifactRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, deployerRepository?: { __typename?: 'GitRepository', id: string, url: string, health?: GitHealth | null, authMethod?: AuthMethod | null, editable?: boolean | null, error?: string | null, insertedAt?: string | null, pulledAt?: string | null, updatedAt?: string | null, urlFormat?: string | null, httpsPath?: string | null } | null, createBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, smtp?: { __typename?: 'SmtpSettings', server: string, port: number, sender: string, user: string, ssl: boolean } | null, readBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, writeBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null, gitBindings?: Array<{ __typename?: 'PolicyBinding', id?: string | null, user?: { __typename?: 'User', id: string, name: string, email: string } | null, group?: { __typename?: 'Group', id: string, name: string } | null } | null> | null } | null };

export type ObservabilityProviderFragment = { __typename?: 'ObservabilityProvider', id: string, name: string, type: ObservabilityProviderType, insertedAt?: string | null, updatedAt?: string | null };

Expand Down Expand Up @@ -10961,6 +10963,15 @@ export const HttpConnectionFragmentDoc = gql`
user
}
`;
export const SmtpSettingsFragmentDoc = gql`
fragment SmtpSettings on SmtpSettings {
server
port
sender
user
ssl
}
`;
export const DeploymentSettingsFragmentDoc = gql`
fragment DeploymentSettings on DeploymentSettings {
id
Expand All @@ -10985,6 +10996,9 @@ export const DeploymentSettingsFragmentDoc = gql`
createBindings {
...PolicyBinding
}
smtp {
...SmtpSettings
}
readBindings {
...PolicyBinding
}
Expand All @@ -10997,7 +11011,8 @@ export const DeploymentSettingsFragmentDoc = gql`
}
${HttpConnectionFragmentDoc}
${GitRepositoryFragmentDoc}
${PolicyBindingFragmentDoc}`;
${PolicyBindingFragmentDoc}
${SmtpSettingsFragmentDoc}`;
export const ObservabilityProviderFragmentDoc = gql`
fragment ObservabilityProvider on ObservabilityProvider {
id
Expand Down Expand Up @@ -21945,6 +21960,7 @@ export const namedOperations = {
GlobalService: 'GlobalService',
ServiceTemplateWithoutConfiguration: 'ServiceTemplateWithoutConfiguration',
HttpConnection: 'HttpConnection',
SmtpSettings: 'SmtpSettings',
DeploymentSettings: 'DeploymentSettings',
ObservabilityProvider: 'ObservabilityProvider',
ManagedNamespace: 'ManagedNamespace',
Expand Down
Loading

0 comments on commit b95fe2d

Please sign in to comment.