193 lines
7.0 KiB
Plaintext
193 lines
7.0 KiB
Plaintext
@using NetinaShop.AdminPanel.PWA.Extensions
|
|
|
|
@inject IRestWrapper RestWrapper
|
|
@inject IUserUtility UserUtility
|
|
@inject ISnackbar Snackbar
|
|
|
|
<MudDialog class="mx-auto">
|
|
<DialogContent>
|
|
<div class="flex flex-row">
|
|
<MudStack class="grow " Spacing="0">
|
|
<MudText Typo="Typo.h6">انتخاب یا اپلود عکس جدید</MudText>
|
|
<MudText Typo="Typo.body2">میتوانید از بین عکس های اپلود شده یکی را انتخاب کرده یا عکس جدیدی اپلود کنید</MudText>
|
|
</MudStack>
|
|
|
|
<MudFileUpload class="flex-none" T="IBrowserFile" OnFilesChanged="FileChangeForUpload">
|
|
<ButtonTemplate>
|
|
|
|
<MudButton HtmlTag="label"
|
|
class="h-full"
|
|
Variant="Variant.Filled"
|
|
Color="Color.Info"
|
|
StartIcon="@Icons.Material.Filled.CloudUpload"
|
|
for="@context.Id">
|
|
اپلود فایل جدید
|
|
</MudButton>
|
|
</ButtonTemplate>
|
|
</MudFileUpload>
|
|
</div>
|
|
|
|
<MudTextField T="string" Placeholder="جست جو بر اساس نام فایل" Adornment="Adornment.Start" Immediate="true"
|
|
Clearable="true"
|
|
ValueChanged="@SearchChanged"
|
|
AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium"
|
|
Variant="Variant.Outlined"
|
|
OnAdornmentClick="@SearchAsync"></MudTextField>
|
|
<MudContainer class="max-h-[30rem]" Style="overflow-y: scroll">
|
|
<div class="flex flex-wrap justify-center">
|
|
@foreach (var item in _files)
|
|
{
|
|
@if (item.Selected)
|
|
{
|
|
<MudImage @onclick="()=>UnSelectFile(item)" class="cursor-pointer rounded-lg mx-1 w-52 h-52 mt-2 border-solid border-4 border-blue-500" Src="@item.GetLink()" Elevation="25" />
|
|
}
|
|
else
|
|
{
|
|
<MudImage @onclick="()=>SelectFile(item)" class="cursor-pointer rounded-lg mx-1 w-52 h-52 mt-2" Src="@item.GetLink()" Elevation="25" />
|
|
}
|
|
}
|
|
</div>
|
|
</MudContainer>
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
<MudStack Row="true" class="w-full mx-4 mb-2">
|
|
|
|
|
|
<BaseButtonUi class="w-64 rounded-md" IsProcessing="@_isProcessing"
|
|
Icon="@Icons.Material.Outlined.Check"
|
|
Variant="Variant.Filled" Color="Color.Success"
|
|
OnClickCallback="SelectFile"
|
|
Content="انتخابــ" />
|
|
<MudSpacer />
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Large" Color="Color.Error" OnClick="Cancel">بستن</MudButton>
|
|
</MudStack>
|
|
</DialogActions>
|
|
</MudDialog>
|
|
@code
|
|
{
|
|
|
|
|
|
private void SelectFile(StorageFileSDto item)
|
|
{
|
|
var pastSelect = _files.FirstOrDefault(f => f.Selected);
|
|
if (pastSelect != null)
|
|
pastSelect.Selected = false;
|
|
item.Selected = true;
|
|
}
|
|
|
|
private void UnSelectFile(StorageFileSDto item) => item.Selected = false;
|
|
|
|
public void SearchChanged(string search)
|
|
{
|
|
if (search.IsNullOrEmpty() && !_search.IsNullOrEmpty())
|
|
{
|
|
_files.Clear();
|
|
_originalFiles.ForEach(f=>_files.Add(f));
|
|
}
|
|
_search = search;
|
|
}
|
|
public void SearchAsync()
|
|
{
|
|
try
|
|
{
|
|
if (_search.IsNullOrEmpty())
|
|
throw new AppException("دسته بندی برای جست جو وارد نشده است");
|
|
_files.Clear();
|
|
foreach (var storageFileSDto in _originalFiles.Where(f => f.FileName.ToLower().Trim().Contains(_search.ToLower().Trim())))
|
|
_files.Add(storageFileSDto);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Snackbar.Add(e.Message, Severity.Error);
|
|
}
|
|
}
|
|
|
|
public void SelectFile()
|
|
{
|
|
var selected = _files.FirstOrDefault(f => f.Selected);
|
|
if (selected == null)
|
|
throw new Exception("یک فایل را انتخاب کنید");
|
|
MudDialog.Close(selected);
|
|
}
|
|
[CascadingParameter]
|
|
MudDialogInstance MudDialog { get; set; }
|
|
void Cancel() => MudDialog.Cancel();
|
|
private readonly ObservableCollection<StorageFileSDto> _files = new ObservableCollection<StorageFileSDto>();
|
|
private List<StorageFileSDto> _originalFiles = new List<StorageFileSDto>();
|
|
private bool _isProcessing = false;
|
|
private string _search = string.Empty;
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
_isProcessing = true;
|
|
_files.Clear();
|
|
var token = await UserUtility.GetBearerTokenAsync();
|
|
var files = await RestWrapper.FileRestApi.GetFilesAsync(token);
|
|
files.ForEach(f => _files.Add(f));
|
|
_originalFiles = files;
|
|
}
|
|
catch (ApiException ex)
|
|
{
|
|
var exe = await ex.GetContentAsAsync<ApiResult>();
|
|
if (exe != null)
|
|
Snackbar.Add(exe.Message, Severity.Error);
|
|
Snackbar.Add(ex.Content, Severity.Error);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Snackbar.Add(e.Message, Severity.Error);
|
|
}
|
|
finally
|
|
{
|
|
_isProcessing = false;
|
|
}
|
|
|
|
await base.OnInitializedAsync();
|
|
}
|
|
|
|
private async Task FileChangeForUpload(InputFileChangeEventArgs obj)
|
|
{
|
|
try
|
|
{
|
|
_isProcessing = true;
|
|
using var memoryStream = new MemoryStream();
|
|
var file = obj.File;
|
|
var stream = file.OpenReadStream();
|
|
await stream.CopyToAsync(memoryStream);
|
|
|
|
var fileUpload = new FileUploadRequest
|
|
{
|
|
ContentType = file.ContentType,
|
|
FileName = file.Name,
|
|
FileUploadType = FileUploadType.Image,
|
|
StringBaseFile = Convert.ToBase64String(memoryStream.ToArray())
|
|
};
|
|
var token = await UserUtility.GetBearerTokenAsync();
|
|
var rest = await RestWrapper.FileRestApi.UploadFileAsync(fileUpload, token);
|
|
_files.Insert(0, new StorageFileSDto
|
|
{
|
|
FileLocation = rest.FileLocation,
|
|
FileName = rest.FileName,
|
|
FileType = StorageFileType.Image
|
|
});
|
|
}
|
|
catch (ApiException ex)
|
|
{
|
|
var exe = await ex.GetContentAsAsync<ApiResult>();
|
|
if (exe != null)
|
|
Snackbar.Add(exe.Message, Severity.Error);
|
|
Snackbar.Add(ex.Content, Severity.Error);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Snackbar.Add(e.Message, Severity.Error);
|
|
}
|
|
finally
|
|
{
|
|
_isProcessing = false;
|
|
}
|
|
}
|
|
}
|