birzha_mobile/lib/models/attributes/miscelaneous.dart

95 lines
2.9 KiB
Dart

import 'dart:convert';
import 'package:birzha/models/attributes/attribute.dart';
import 'package:birzha/models/products/post.dart';
import 'package:birzha/models/settings/settingsModel.dart';
import 'package:birzha/services/requests.dart';
class MiscAttributeOption extends PostAttribute<int>
with AttributeWithValueNameMixin, TranslatableMixin {
MiscAttributeOption({required Map<String, dynamic> data}) : super(data) {
configureTranslationModel();
}
@override
String get label =>
translationModel?.translationOf('name', jSON['name'] ?? '') ??
jSON['name'] ??
'';
@override
String get primaryKeyField => 'id';
@override
String get value => label;
}
class MiscAttribute extends PostAttribute<String>
with
KeyIndexedAttributeMixin,
SelectableAttributeMixin<String, AttributeWithValueNameMixin>,
OptionedAttributeMixin {
MiscAttribute._(
{required this.options, required String key, required String labelKey})
: super({'key': key, 'label': labelKey});
static Future<MiscAttribute> getAttrbute(
{required String key, required String labelKey, required Uri uri}) async {
var options =
await FutureGetList<MiscAttributeOption>(uri, parser: (response) {
final map = jsonDecode(response.body);
final products = map?['data'] as List;
return products
.map<MiscAttributeOption>(
(jsonData) => MiscAttributeOption(data: jsonData))
.toList();
}).fetch();
return MiscAttribute._(options: options, key: key, labelKey: labelKey);
}
static Future<List<MiscAttribute>> getAttrbutes() async {
return [
await MiscAttribute.getAttrbute(
key: 'country',
labelKey: 'country',
uri: baseUrl(path: kApiPath + '/countries')),
await MiscAttribute.getAttrbute(
key: 'measure_id',
labelKey: 'measure',
uri: baseUrl(path: kApiPath + '/measures')),
await MiscAttribute.getAttrbute(
key: 'currency_id',
labelKey: 'currency',
uri: baseUrl(path: kApiPath + '/currencies')),
await MiscAttribute.getAttrbute(
key: 'payment_term_id',
labelKey: 'paymentTerm',
uri: baseUrl(
path: kApiPath + '/terms', queryParameters: {'type': 'payment'})),
await MiscAttribute.getAttrbute(
key: 'delivery_term_id',
labelKey: 'deliveryTerm',
uri: baseUrl(
path: kApiPath + '/terms',
queryParameters: {'type': 'delivery'})),
];
}
@override
String get key => primaryKey;
@override
String get label => jSON['label'].toString().translation;
@override
String get primaryKeyField => 'key';
@override
selectDelegate(groupValue) {
return SingleOptionSelectableDelegate(groupValue, options);
}
@override
final List<MiscAttributeOption> options;
}