import { useMutation } from '@apollo/client'; import LoadingButton from '@mui/lab/LoadingButton'; import Grid from '@mui/material/Grid'; import Skeleton from '@mui/material/Skeleton'; import Stack from '@mui/material/Stack'; import merge from 'lodash/merge'; import * as React from 'react'; import { useQueryClient } from '@tanstack/react-query'; import ColorInput from 'components/ColorInput'; import Container from 'components/Container'; import Form from 'components/Form'; import PageTitle from 'components/PageTitle'; import TextField from 'components/TextField'; import { UPDATE_CONFIG } from 'graphql/mutations/update-config.ee'; import nestObject from 'helpers/nestObject'; import useAutomatischConfig from 'hooks/useAutomatischConfig'; import useFormatMessage from 'hooks/useFormatMessage'; import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar'; import { primaryDarkColor, primaryLightColor, primaryMainColor, } from 'styles/theme'; const getPrimaryMainColor = (color) => color || primaryMainColor; const getPrimaryDarkColor = (color) => color || primaryDarkColor; const getPrimaryLightColor = (color) => color || primaryLightColor; const defaultValues = { title: 'Automatisch', 'palette.primary.main': primaryMainColor, 'palette.primary.dark': primaryDarkColor, 'palette.primary.light': primaryLightColor, }; export default function UserInterface() { const formatMessage = useFormatMessage(); const [updateConfig, { loading }] = useMutation(UPDATE_CONFIG); const { data: configData, isLoading: configLoading } = useAutomatischConfig(); const config = configData?.data; const queryClient = useQueryClient(); const enqueueSnackbar = useEnqueueSnackbar(); const configWithDefaults = merge({}, defaultValues, nestObject(config)); const handleUserInterfaceUpdate = async (uiData) => { try { const input = { title: uiData?.title, 'palette.primary.main': getPrimaryMainColor( uiData?.palette?.primary.main, ), 'palette.primary.dark': getPrimaryDarkColor( uiData?.palette?.primary.dark, ), 'palette.primary.light': getPrimaryLightColor( uiData?.palette?.primary.light, ), 'logo.svgData': uiData?.logo?.svgData, }; await updateConfig({ variables: { input, }, optimisticResponse: { updateConfig: input, }, update: async function () { queryClient.invalidateQueries({ queryKey: ['automatisch', 'config'], }); }, }); enqueueSnackbar(formatMessage('userInterfacePage.successfullyUpdated'), { variant: 'success', SnackbarProps: { 'data-test': 'snackbar-update-user-interface-success', }, }); } catch (error) { throw new Error('Failed while updating!'); } }; return ( {formatMessage('userInterfacePage.title')} {configLoading && ( )} {!configLoading && (
{formatMessage('userInterfacePage.submit')}
)}
); }