Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

855 Deleting chats from Bürokratt #929

Draft
wants to merge 1 commit into
base: v2.0.1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions GUI/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import SettingsWelcomeMessage from 'pages/Settings/SettingsWelcomeMessage';
import SettingsSessionLength from 'pages/Settings/SettingsSessionLength';
import './locale/et_EE';
import ChatPending from 'pages/Chat/ChatPending';
import DeleteChat from "./pages/Settings/DeleteChat";

const App: FC = () => {
useQuery<{
Expand All @@ -39,6 +40,7 @@ const App: FC = () => {
<Route index element={<Navigate to="/active" />} />
<Route path="/unanswered" element={<ChatUnanswered />} />
<Route path="/active" element={<ChatActive />} />
<Route path="/delete" element={<DeleteChat />} />
<Route path="/history" element={<ChatHistory />} />
<Route path="/pending" element={<ChatPending />} />
<Route path="/users" element={<SettingsUsers />} />
Expand Down
314 changes: 314 additions & 0 deletions GUI/src/pages/Settings/DeleteChat/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
import { FC, useState } from 'react';
import { useForm, Controller } from 'react-hook-form';
import { useMutation, useQuery } from '@tanstack/react-query';
import { AxiosError } from 'axios';
import { useTranslation } from 'react-i18next';
import { format, parse } from 'date-fns';
import { Button, Card, FormDatepicker, Switch, Track } from 'components';
import { OrganizationWorkingTime } from 'types/organizationWorkingTime';
import { useToast } from 'hooks/useToast';
import { apiDev } from 'services/api';
import '../SettingsWorkingTime/SettingsWorkingTime.scss';
import { getOrganizationTimeData, setOrganizationTimeData } from '../SettingsWorkingTime/data';
import withAuthorization from 'hoc/with-authorization';
import { ROLES } from 'utils/constants';

const weekdaysOptions = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
];

const DeleteChat: FC = () => {
const { t } = useTranslation();
const toast = useToast();
const { control, handleSubmit, reset, watch } =
useForm<OrganizationWorkingTime>();
const [key, setKey] = useState(0);
const isOrganizationClosedOnWeekEnds = watch('organizationClosedOnWeekEnds');
const isOrganizationTheSameOnAllWorkingDays = watch(
'organizationTheSameOnAllWorkingDays'
);
const organizationWorkingTimeWeekdays = watch(
'organizationWorkingTimeWeekdays'
);
const { data: workingTime } = useQuery<OrganizationWorkingTime>({
queryKey: ['configs/organization-working-time', 'prod'],
onSuccess: (data: any) => {
if (Object.keys(control._formValues).length > 0) return;
reset(getOrganizationTimeData(data.response));
setKey(key + 1);
},
});

const workingTimeMutation = useMutation({
mutationFn: (data: OrganizationWorkingTime) =>
apiDev.post<OrganizationWorkingTime>(
'configs/organization-working-time',
setOrganizationTimeData(data)
),
onSuccess: () => {
toast.open({
type: 'success',
title: t('global.notification'),
message: t('toast.success.updateSuccess'),
});
},
onError: (error: AxiosError) => {
toast.open({
type: 'error',
title: t('global.notificationError'),
message: error.message,
});
},
});

const handleFormSubmit = handleSubmit((data) =>
workingTimeMutation.mutate(data)
);

function sortAndJoin(array: string[]): string {
return array.toSorted((a, b) => a.localeCompare(b)).join(',');
}

function filterAndJoin(array: string[], day: string): string {
return array.filter((pd: string) => pd !== day.toLowerCase()).join(',');
}

if (!workingTime || Object.keys(control._formValues).length === 0) {
return <>Loading...</>;
}

return (
<>
<h1>{t('settings.workingTime.title')}</h1>
<p>{t('settings.workingTime.description')}</p>
<Card
key={key}
isHeaderLight={true}
isBodyDivided={true}
footer={
<Track justify="end">
<Button onClick={handleFormSubmit}>{t('global.save')}</Button>
</Track>
}
header={
<Track gap={8} direction="vertical" align="left">
<Controller
name="organizationWorkingTimeNationalHolidays"
control={control}
render={({ field }) => (
<Switch
label={t('settings.workingTime.publicHolidays')}
onLabel={t('settings.workingTime.consider').toString()}
offLabel={t('settings.workingTime.dontConsider').toString()}
onCheckedChange={field.onChange}
checked={field.value}
{...field}
/>
)}
/>
<Controller
name="organizationClosedOnWeekEnds"
control={control}
render={({ field }) => (
<Switch
label={t('settings.workingTime.closedOnWeekends')}
onLabel={t('global.yes').toString()}
offLabel={t('global.no').toString()}
onCheckedChange={field.onChange}
checked={field.value}
{...field}
/>
)}
/>
<Controller
name="organizationTheSameOnAllWorkingDays"
control={control}
render={({ field }) => (
<Switch
label={t('settings.workingTime.theSameOnAllWorkingDays')}
onLabel={t('global.yes').toString()}
offLabel={t('global.no').toString()}
onCheckedChange={field.onChange}
checked={field.value}
{...field}
/>
)}
/>
</Track>
}
>
{isOrganizationTheSameOnAllWorkingDays && (
<Track>
<label className="Label">
{t(
`${
isOrganizationClosedOnWeekEnds
? 'settings.workingTime.allWeekdaysExceptWeekend'
: 'settings.workingTime.allWeekdays'
}`
)}
</label>
<Controller
name={'organizationAllWeekdaysTimeStartISO'}
control={control}
render={({ field }) => {
return (
<div className="startTime">
<FormDatepicker
{...field}
timePicker
hideLabel
direction="row"
label=""
value={
parse(
format(field.value as Date, 'HH:mm:ss'),
'HH:mm:ss',
new Date()
) ?? new Date('0')
}
/>
</div>
);
}}
/>
<label>{t('settings.workingTime.until')}</label>
<Controller
name={'organizationAllWeekdaysTimeEndISO'}
control={control}
render={({ field }) => {
return (
<div className="endTime">
<FormDatepicker
{...field}
timePicker
hideLabel
direction="row"
label=""
value={
parse(
format(field.value as Date, 'HH:mm:ss'),
'HH:mm:ss',
new Date()
) ?? new Date('0')
}
/>
</div>
);
}}
/>
</Track>
)}
{!isOrganizationTheSameOnAllWorkingDays &&
weekdaysOptions
.filter(
(d) =>
!(
isOrganizationClosedOnWeekEnds &&
(d === 'Saturday' || d === 'Sunday')
)
)
.map((d) => (
<Track key={d}>
<label className="Label switch">
{t(`settings.weekdays.${d}`.toLowerCase())}
</label>
<Controller
name="organizationWorkingTimeWeekdays"
control={control}
render={({ field }) => (
<div>
<Switch
label=""
onLabel={t('settings.workingTime.open').toString()}
offLabel={t('settings.workingTime.closed').toString()}
onCheckedChange={(value) => {
field.onChange(
value
? sortAndJoin([
...field.value.toString().split(','),
d.toLowerCase(),
])
: filterAndJoin(
field.value.toString().split(','),
d
)
);
}}
checked={field.value?.includes(d.toLowerCase())}
{...field}
/>
</div>
)}
/>
{organizationWorkingTimeWeekdays.includes(d.toLowerCase()) && (
<Track>
<Controller
name={
`organization${d}WorkingTimeStartISO` as keyof OrganizationWorkingTime
}
control={control}
render={({ field }) => {
return (
<div className="startTime">
<FormDatepicker
{...field}
timePicker
hideLabel
direction="row"
label=""
value={
parse(
format(field.value as Date, 'HH:mm:ss'),
'HH:mm:ss',
new Date()
) ?? new Date('0')
}
/>
</div>
);
}}
/>
<label>{t('settings.workingTime.until')}</label>
<Controller
name={
`organization${d}WorkingTimeEndISO` as keyof OrganizationWorkingTime
}
control={control}
render={({ field }) => {
return (
<div className="endTime">
<FormDatepicker
{...field}
timePicker
hideLabel
direction="row"
label=""
value={
parse(
format(field.value as Date, 'HH:mm:ss'),
'HH:mm:ss',
new Date()
) ?? new Date('0')
}
/>
</div>
);
}}
/>
</Track>
)}
</Track>
))}
</Card>
</>
);
};

export default withAuthorization(DeleteChat, [ROLES.ROLE_ADMINISTRATOR]);