#how can disable days of week?

5 messages · Page 1 of 1 (latest)

velvet ridge
#

Hey friends, i need disable a days of week that i don-t have in the array in my table, i try whit this code but is not working, do you have any idea about how can fix this?

i have this in my table row availabledays

["Lunes"]
["Sabado","Lunes","Jueves","Miercoles"]
["Miercoles","Lunes","Viernes","Sabado","Martes","Jueves","Domingo"]

This is my code:

#
Forms\Components\Select::make('common_area_id')
    ->label('Tipo de área')
    ->required()
    ->relationship('commonareas', 'nombre')
    ->reactive()
    ->afterStateUpdated(function ($state, callable $get, callable $set) {
    $commonArea = CommonArea::find($state);
    if($commonArea) {
      $set('valor', $commonArea->valor);
      $set('tramoreserva', "{$commonArea->rangefrom} - {$commonArea->rangeuntil}");
      $set('availabledays', $commonArea->availabledays);
      // Show available days
      //dd($commonArea->availabledays);
    } else {
      $set('valor', '');
      $set('tramoreserva', '');
      $set('availabledays', []);
      }
    }),
Forms\Components\Hidden::make('availabledays')
    ->reactive(),
Forms\Components\DatePicker::make('fecha')
    ->label('Fecha')
    ->minDate(now()->toDateString()) // Fecha mínima de solicitud la actual o mayor
    ->required()
    ->reactive()
    ->afterStateUpdated(function ($state, callable $get, callable $set) {
      $commonArea = CommonArea::find($get('common_area_id'));
    if ($commonArea && !empty($commonArea->availabledays)) {
      $set('availabledays', $commonArea->availabledays);
    } else {
      $set('availabledays', []);
      }
    })
    ->disabledDates(function (callable $get) {
      $availableDays = $get('availabledays');
    if (!$availableDays) {
       return [];
    }
    // Mapeo de los días de la semana a sus índices correspondientes
    $dayMap = [
      'Domingo'   => 0,
      'Lunes'     => 1,
      'Martes'    => 2,
      'Miercoles' => 3,
      'Jueves'    => 4,
      'Viernes'   => 5,
      'Sabado'    => 6,
    ];
    $allowedDays = array_map(fn($day) => $dayMap[$day] ?? null, $availableDays);
    $allowedDays = array_filter($allowedDays, fn($day) => $day !== null);
    $unavailableDays = array_diff([0,1,2,3,4,5,6], $allowedDays);
    return function (\Carbon\Carbon $date) use ($unavailableDays) {
    return in_array($date->dayOfWeek, $unavailableDays);
    };
    }),
#

So my plan is that if Monday exists in my array, it will only be shown as available to select the date Monday, the others that are disabled, if in another row I have Monday, Wednesday and Friday, that these 3 days are available, the others that are deactivated...

torpid dagger
#

Check this one, this code will disable Monday options by implementing disableOptionWhen method

  Forms\Components\Select::make('days')
    ->options(['mon' => 'Monday', 'tue' => 'Tuesday'])
    ->disableOptionWhen(function ($value) {
      $toBeDisabled = ['mon'];
      return in_array($value, $toBeDisabled);
    }),
velvet ridge
# torpid dagger Check this one, this code will disable Monday options by implementing ```disable...

that it's not working bro, but thank you, i fix my problem whit this code!

Forms\Components\Select::make('common_area_id')
                    ->label('Tipo de área')
                    ->required()
                    ->relationship('commonareas', 'nombre')
                    ->reactive()
                    ->afterStateUpdated(function ($state, callable $get, callable $set) use ($tenant) {
                        $commonArea = CommonArea::find($state);
                        if ($commonArea) {
                            $set('valor', $commonArea->valor);
                            $set('tramoreserva', "{$commonArea->rangefrom} - {$commonArea->rangeuntil}");
                            $set('availabledays', $commonArea->availabledays ?? []); // Asegura que sea un array

                            // Obtener reservas existentes para el área común seleccionada
                            $reservasExistentes = ReserveArea::where('common_area_id', $state)
                                ->where('company_id', $tenant->id) // Filtrar por tenant logeado
                                ->where('fecha', '>=', now()->toDateString()) // Solo reservas futuras
                                ->pluck('fecha');

                            // Deshabilitar los días que ya están reservados
                            $set('disabledDays', $reservasExistentes->toArray());
                        } else {
                            $set('valor', '');
                            $set('tramoreserva', '');
                            $set('availabledays', []);
                            $set('disabledDays', []);
                        }
                    }),