2020-07-01 19:08:08 +00:00
|
|
|
import { DeepPartial } from 'fishery';
|
|
|
|
import { Product } from './product';
|
|
|
|
import { AdapterTypes, ModelRegistry } from '../framework/model-registry';
|
|
|
|
import { ModelFactory } from '../framework/model-factory';
|
|
|
|
import { APIAdapter } from '../framework/api-adapter';
|
|
|
|
|
|
|
|
export class SimpleProduct extends Product {
|
|
|
|
public constructor( partial: DeepPartial<SimpleProduct> = {} ) {
|
|
|
|
super( partial );
|
|
|
|
Object.assign( this, partial );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the simple product factory and adapters.
|
|
|
|
*
|
|
|
|
* @param {ModelRegistry} registry The registry to hold the model reference.
|
|
|
|
*/
|
2020-07-01 20:58:12 +00:00
|
|
|
export function registerSimpleProduct( registry: ModelRegistry ): void {
|
2020-07-01 19:08:08 +00:00
|
|
|
if ( null !== registry.getFactory( SimpleProduct ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const factory = ModelFactory.define<SimpleProduct, any, ModelFactory<SimpleProduct>>(
|
|
|
|
( { params } ) => {
|
|
|
|
return new SimpleProduct( params );
|
|
|
|
},
|
|
|
|
);
|
|
|
|
registry.registerFactory( SimpleProduct, factory );
|
|
|
|
|
|
|
|
const apiAdapter = new APIAdapter<SimpleProduct>(
|
|
|
|
'/wc/v3/products',
|
|
|
|
( model ) => {
|
|
|
|
return {
|
|
|
|
type: 'simple',
|
|
|
|
name: model.Name,
|
|
|
|
regular_price: model.RegularPrice,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
registry.registerAdapter( SimpleProduct, AdapterTypes.API, apiAdapter );
|
|
|
|
}
|