saleor-dashboard/src/staff/components/StaffDetailsPage/StaffDetailsPage.tsx

202 lines
7.1 KiB
TypeScript
Raw Normal View History

import { Card, CardContent, Typography } from "@material-ui/core";
import AccountPermissionGroups from "@saleor/components/AccountPermissionGroups";
2019-06-19 14:40:52 +00:00
import AppHeader from "@saleor/components/AppHeader";
Apps (#599) * create Apps view * create more app components, generate types and messages * apps refactor, update snapshots * show error message in tooltip when app installation fail * update apps components and view, add apps list to storybook * update defaultMessages * create app details view * update AppListPage with Skeleton component * create app activate/deactivate dialogs, create app details stories * add AppHeader to AppDetailsPage * update defaultMessages * update AppDetails view and components after review * create custom app details view * refactor webhooks * update webhooks fixtures * update WebhookDetailsPage story * update strings * create CustomAppCreate view and components * update AppListPage story * create AppInstall view and page * handle errors in AppInstall view * update defaultMessages * add AppInstallPage to storybook * add status prop to MessageManager * update defaultMessages * remove service account section * remove service account routes * remove as operator from notify status * add notifications for app installations * update styles for deactivated app * update app installations with local storage * update defaultMessages * AppInstall update * dd delete button to ongoin installations table * fix active installations condition * fix error messages in AppsList * update defaultMessages * add iframe to AppDetailsPage * create AppDetailsSettingsPage * install macaw-ui * apps styles clean up * update schema, fixtures * few apps updates * WebhookCreate - fix onBack button name * WebhookCreatePage story update * rename apps table from external to thirdparty * update defaultMessages * fix test, update snapshots * AppDetailsSettings - add token to headers * fix first number in local apps query * app details settings - use shop domain host * add onSettingsRowClick to InstalledApps * resolve conflicts * update changelog and messages * add noopener noreferrer do app privacy link * update snapshots * update snapshots * updates after review * update defaultMessages * CustomAppDetails - add missing notify status
2020-07-22 10:54:15 +00:00
import AccountStatus from "@saleor/components/AppStatus";
2019-06-19 14:40:52 +00:00
import CardSpacer from "@saleor/components/CardSpacer";
import CardTitle from "@saleor/components/CardTitle";
2019-06-19 14:40:52 +00:00
import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton";
import Container from "@saleor/components/Container";
import Form from "@saleor/components/Form";
import Grid from "@saleor/components/Grid";
import { MultiAutocompleteChoiceType } from "@saleor/components/MultiAutocompleteSelectField";
2019-06-19 14:40:52 +00:00
import PageHeader from "@saleor/components/PageHeader";
import SaveButtonBar from "@saleor/components/SaveButtonBar";
import { StaffErrorFragment } from "@saleor/fragments/types/StaffErrorFragment";
2019-10-16 15:18:29 +00:00
import useLocale from "@saleor/hooks/useLocale";
import useStateFromProps from "@saleor/hooks/useStateFromProps";
import { sectionNames } from "@saleor/intl";
import { getUserName } from "@saleor/misc";
import { SearchPermissionGroups_search_edges_node } from "@saleor/searches/types/SearchPermissionGroups";
import { FetchMoreProps, SearchPageProps } from "@saleor/types";
import createMultiAutocompleteSelectHandler from "@saleor/utils/handlers/multiAutocompleteSelectChangeHandler";
import React from "react";
import { useIntl } from "react-intl";
import { StaffMemberDetails_user } from "../../types/StaffMemberDetails";
import StaffPassword from "../StaffPassword/StaffPassword";
2019-10-16 15:18:29 +00:00
import StaffPreferences from "../StaffPreferences";
2019-06-19 14:40:52 +00:00
import StaffProperties from "../StaffProperties/StaffProperties";
export interface StaffDetailsFormData {
email: string;
2019-06-19 14:40:52 +00:00
firstName: string;
isActive: boolean;
2019-06-19 14:40:52 +00:00
lastName: string;
permissionGroups: string[];
2019-06-19 14:40:52 +00:00
}
export interface StaffDetailsPageProps extends SearchPageProps {
availablePermissionGroups: SearchPermissionGroups_search_edges_node[];
2019-06-19 14:40:52 +00:00
canEditAvatar: boolean;
2019-10-16 15:18:29 +00:00
canEditPreferences: boolean;
2019-06-19 14:40:52 +00:00
canEditStatus: boolean;
canRemove: boolean;
disabled: boolean;
fetchMorePermissionGroups: FetchMoreProps;
2019-06-19 14:40:52 +00:00
saveButtonBarState: ConfirmButtonTransitionState;
staffMember: StaffMemberDetails_user;
errors: StaffErrorFragment[];
2019-06-19 14:40:52 +00:00
onBack: () => void;
2019-12-03 16:14:30 +00:00
onChangePassword: () => void;
2019-06-19 14:40:52 +00:00
onDelete: () => void;
onImageDelete: () => void;
2020-10-22 11:33:29 +00:00
onSubmit: (data: StaffDetailsFormData) => Promise<any[]>;
2019-06-19 14:40:52 +00:00
onImageUpload(file: File);
}
const StaffDetailsPage: React.FC<StaffDetailsPageProps> = ({
availablePermissionGroups,
2019-06-19 14:40:52 +00:00
canEditAvatar,
2019-10-16 15:18:29 +00:00
canEditPreferences,
2019-06-19 14:40:52 +00:00
canEditStatus,
canRemove,
disabled,
errors,
fetchMorePermissionGroups,
initialSearch,
2019-06-19 14:40:52 +00:00
onBack,
2019-12-03 16:14:30 +00:00
onChangePassword,
2019-06-19 14:40:52 +00:00
onDelete,
onImageDelete,
onImageUpload,
onSearchChange,
onSubmit,
saveButtonBarState,
staffMember
2019-06-19 14:40:52 +00:00
}: StaffDetailsPageProps) => {
const intl = useIntl();
2019-10-16 15:18:29 +00:00
const { locale, setLocale } = useLocale();
const [
permissionGroupsDisplayValues,
setPermissionGroupsDisplayValues
] = useStateFromProps<MultiAutocompleteChoiceType[]>(
(staffMember?.permissionGroups || []).map(group => ({
disabled: !group.userCanManage,
label: group.name,
value: group.id
})) || []
);
const initialForm: StaffDetailsFormData = {
email: staffMember?.email || "",
firstName: staffMember?.firstName || "",
isActive: !!staffMember?.isActive,
lastName: staffMember?.lastName || "",
permissionGroups: staffMember?.permissionGroups.map(pg => pg.id) || []
2019-06-19 14:40:52 +00:00
};
2019-10-16 15:18:29 +00:00
2019-06-19 14:40:52 +00:00
return (
<Form initial={initialForm} onSubmit={onSubmit} confirmLeave>
{({ data: formData, change, hasChanged, submit, toggleValue }) => {
const permissionGroupsChange = createMultiAutocompleteSelectHandler(
toggleValue,
setPermissionGroupsDisplayValues,
permissionGroupsDisplayValues,
availablePermissionGroups?.map(group => ({
label: group.name,
value: group.id
})) || []
);
return (
<Container>
<AppHeader onBack={onBack}>
{intl.formatMessage(sectionNames.staff)}
</AppHeader>
<PageHeader title={getUserName(staffMember)} />
<Grid>
<div>
<StaffProperties
errors={errors}
data={formData}
disabled={disabled}
canEditAvatar={canEditAvatar}
staffMember={staffMember}
onChange={change}
onImageUpload={onImageUpload}
onImageDelete={onImageDelete}
/>
{canEditPreferences && (
<>
<CardSpacer />
<StaffPassword onChangePassword={onChangePassword} />
</>
)}
</div>
<div>
{canEditPreferences && (
<StaffPreferences
locale={locale}
onLocaleChange={setLocale}
2019-10-16 15:18:29 +00:00
/>
)}
{canEditStatus && (
<>
<Card>
<CardTitle
title={intl.formatMessage({
defaultMessage: "Permissions",
description: "dialog header"
})}
/>
<CardContent>
<Typography>
{intl.formatMessage({
defaultMessage: "User is assigned to:",
description: "card description"
})}
</Typography>
<AccountPermissionGroups
formData={formData}
disabled={disabled}
errors={errors}
initialSearch={initialSearch}
availablePermissionGroups={availablePermissionGroups}
onChange={permissionGroupsChange}
onSearchChange={onSearchChange}
displayValues={permissionGroupsDisplayValues}
{...fetchMorePermissionGroups}
/>
</CardContent>
</Card>
<CardSpacer />
<AccountStatus
data={formData}
disabled={disabled}
label={intl.formatMessage({
defaultMessage: "User is active",
description: "checkbox label"
})}
onChange={change}
/>
</>
)}
</div>
</Grid>
<SaveButtonBar
disabled={disabled || !hasChanged}
state={saveButtonBarState}
onCancel={onBack}
onSave={submit}
onDelete={canRemove ? onDelete : undefined}
/>
</Container>
);
}}
2019-06-19 14:40:52 +00:00
</Form>
);
};
StaffDetailsPage.displayName = "StaffDetailsPage";
export default StaffDetailsPage;