# All files / src/components/admin UpdateDemoLimitDialog.tsx

0% Statements 0/39
0% Branches 0/13
0% Functions 0/6
0% Lines 0/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184

     'use client';

    import { useTranslation } from 'react-i18next';
    import useLoadNamespace from '@/hooks/useLoadNamespace';

    import React from 'react';
    import { useMutation, useQueryClient } from '@tanstack/react-query';
    import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog';
    import { Button } from '@/components/ui/button';
    import { Input } from '@/components/ui/input';
    import { Alert, AlertDescription } from '@/components/ui/alert';
    import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
    import { Clock, AlertTriangle } from 'lucide-react';
    import { useForm } from 'react-hook-form';
    import { zodResolver } from '@hookform/resolvers/zod';
    import { z } from 'zod';
    import { userService } from '@/services/user';
    import { User, UpdateResellerDemoLimitRequest } from '@/types';
    import { extractErrorMessage } from '@/lib/error-message';
    import i18n from '@/lib/i18n';

    interface UpdateDemoLimitDialogProps {
      isOpen: boolean;
      onClose: () => void;
      reseller: User;
    }

    const updateDemoLimitSchema = z.object({
      daily_demo_limit: z.number().min(0, i18n.t('errors.invalidData')).max(100, i18n.t('errors.invalidData'))});

    type UpdateDemoLimitFormData = z.infer<typeof updateDemoLimitSchema>;

    export default function UpdateDemoLimitDialog({ isOpen, onClose, reseller }: UpdateDemoLimitDialogProps) {
      useLoadNamespace('admin/demos');
      const { t } = useTranslation('admin/demos');
      const queryClient = useQueryClient();

      // Form for updating demo limit
      const form = useForm<UpdateDemoLimitFormData>({
        resolver: zodResolver(updateDemoLimitSchema),
        defaultValues: {
          daily_demo_limit: reseller.daily_demo_limit || 0}});

      // Update demo limit mutation
      const updateDemoLimitMutation = useMutation({
        mutationFn: async (data: UpdateDemoLimitFormData) => {
          const request: UpdateResellerDemoLimitRequest = {
            daily_demo_limit: data.daily_demo_limit};

          const result = await userService.updateResellerDemoLimit(reseller.id, request);
          if (result.success) {
            return result.data;
          }
          throw new Error(extractErrorMessage(result.error, t('common.serverError')));
        },
        onSuccess: () => {
          queryClient.invalidateQueries({ queryKey: ['users'] });
          queryClient.invalidateQueries({ queryKey: ['resellers'] });
          onClose();
          form.reset();
        }});

      const handleSubmit = (data: UpdateDemoLimitFormData) => {
        updateDemoLimitMutation.mutate(data);
      };

      const handleClose = () => {
        onClose();
        form.reset();
        updateDemoLimitMutation.reset();
      };

      return (
        <Dialog open={isOpen} onOpenChange={handleClose}>
          <DialogContent className="!max-w-[65vw] !w-[65vw] max-h-[75vh] overflow-y-auto sm:!max-w-[65vw] md:!max-w-[65vw] lg:!max-w-[65vw]" style={{ width: '65vw', maxWidth: '65vw' }}>
            <DialogHeader>
              <DialogTitle className="flex items-center gap-2">
                <Clock className="h-5 w-5" />
                {t('demos.updateLimitDialog.title', { username: reseller.username })}
              </DialogTitle>
              <DialogDescription>
                {t('demos.updateLimitDialog.description')}
              </DialogDescription>
            </DialogHeader>

            <Form {...form}>
              <form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-6">
                <div className="space-y-4">
                  {/* Current Status */}
                  <div className="bg-gray-50 p-4 rounded-lg">
                    <h4 className="font-medium mb-2">{t('demos.updateLimitDialog.currentStatus')}</h4>
                    <div className="grid grid-cols-2 gap-4 text-sm">
                      <div>
                        <span className="text-gray-600">{t('demos.updateLimitDialog.currentLimit')}</span>
                        <span className="ml-2 font-medium">{reseller.daily_demo_limit || 0} {t('demos.updateLimitDialog.demosDay')}</span>
                      </div>
                      <div>
                        <span className="text-gray-600">{t('demos.updateLimitDialog.createdToday')}</span>
                        <span className="ml-2 font-medium">{reseller.demos_created_today || 0}</span>
                      </div>
                    </div>
                  </div>

                  {/* Demo Limit Input */}
                  <FormField
                    control={form.control}
                    name="daily_demo_limit"
                    render={({ field }) => (
                      <FormItem>
                        <FormLabel>{t('demos.updateLimitDialog.dailyDemoLimit')}</FormLabel>
                        <FormControl>
                          <Input
                            type="number"
                            min="0"
                            max="100"
                            placeholder={t('demos.updateLimitDialog.placeholder')}
                            {...field}
                            onChange={(e) => field.onChange(parseInt(e.target.value) || 0)}
                          />
                        </FormControl>
                        <FormMessage />
                        <p className="text-sm text-gray-600">
                          {t('demos.updateLimitDialog.help')}
                        </p>
                      </FormItem>
                    )}
                  />

                  {/* Demo Information */}
                  <div className="bg-blue-50 p-4 rounded-lg">
                    <h4 className="font-medium mb-2 text-blue-900">{t('demos.updateLimitDialog.demoUserInfo')}</h4>
                    <div className="space-y-2 text-sm text-blue-800">
                      <div className="flex items-center gap-2">
                        <Clock className="h-4 w-4" />
                        <span><strong>{t('demos.updateLimitDialog.info.durationLabel')}</strong> {t('demos.updateLimitDialog.info.durationValue')}</span>
                      </div>
                      <div className="flex items-center gap-2">
                        <Clock className="h-4 w-4" />
                        <span><strong>{t('demos.updateLimitDialog.info.accessLabel')}</strong> {t('demos.updateLimitDialog.info.accessValue')}</span>
                      </div>
                      <div className="flex items-center gap-2">
                        <Clock className="h-4 w-4" />
                        <span><strong>{t('demos.updateLimitDialog.info.devicesLabel')}</strong> {t('demos.updateLimitDialog.info.devicesValue')}</span>
                      </div>
                      <div className="flex items-center gap-2">
                        <AlertTriangle className="h-4 w-4" />
                        <span><strong>{t('demos.updateLimitDialog.info.autoCleanupLabel')}</strong> {t('demos.updateLimitDialog.info.autoCleanupValue')}</span>
                      </div>
                    </div>
                  </div>
                </div>

                {updateDemoLimitMutation.error && (
                  <Alert variant="destructive">
                    <AlertTriangle className="h-4 w-4" />
                    <AlertDescription>
                      {updateDemoLimitMutation.error.message}
                    </AlertDescription>
                  </Alert>
                )}

                <div className="flex justify-end space-x-2">
                  <Button
                    type="button"
                    variant="outline"
                    onClick={handleClose}
                    disabled={updateDemoLimitMutation.isPending}
                  >
                    {t('demos.updateLimitDialog.cancel')}
                  </Button>
                  <Button
                    type="submit"
                    disabled={updateDemoLimitMutation.isPending}
                  >
                    {updateDemoLimitMutation.isPending ? t('demos.updateLimitDialog.updating') : t('demos.updateLimitDialog.updateButton')}
                  </Button>
                </div>
              </form>
            </Form>
          </DialogContent>
        </Dialog>
      );
    }