Skip to content

Instantly share code, notes, and snippets.

@glendaviesnz
glendaviesnz / add-validation.jsx
Last active June 4, 2018 03:04
Add validation component to validated field
// where the form is declared a simple string is used to indicated validation type
<input name="firstName" validation="required" />
// an object map is used in the form library to map this string name to a validation method and message
export const validationTypes = {
required: { validate: validateRequired, message: 'Field is required' },
email: { validate: validateEmail, message: 'Valid email address required' }
}
@glendaviesnz
glendaviesnz / component-state.js
Last active December 14, 2018 01:41
Object literal declaration of component state
const componentStates = {
name: 'userApproval',
states: {
[actionTypes.loadUnapprovedUsers]: {
[ComponentStates.Idle]: { to: ComponentStates.Processing, action: PassthroughAction }
},
[actionTypes.loadUnapprovedUsersSuccess]: {
[ComponentStates.Processing]: { to: ComponentStates.Idle, action: PassthroughAction }
},
[actionTypes.loadUnapprovedUsersError]: {
@glendaviesnz
glendaviesnz / rxjs-chunker.ts
Created July 7, 2018 00:42
RxJs document chunker and uploader
export class ChunkingService {
public chunkSize = 1024 * 1024;
private _chunkQueue$ = new Subject();
private _maxConnections = 3;
constructor(
private _apiService: apiService,
private _store: Store<any>) {
Guard.notNothing(_apiService, '_apiService');
Guard.notNothing(_store, '_store');
@glendaviesnz
glendaviesnz / rxjs-chunker.ts
Created July 7, 2018 00:45
RxJs document chunker and uploader. This class takes a document and breaks it into 1Mb chunks and uploads those chunks sequentially to the server.
export class ChunkingService {
public chunkSize = 1024 * 1024;
private _chunkQueue$ = new Subject();
private _maxConnections = 3;
constructor(
private _apiService: apiService,
private _store: Store<any>) {
Guard.notNothing(_apiService, '_apiService');
Guard.notNothing(_store, '_store');
@glendaviesnz
glendaviesnz / component-state.builder.ts
Last active December 14, 2018 02:58
NgRX State Machine - fluent component state builder
// Instead of this ....
const componentStates = {
name: 'userApproval',
states: {
[actionTypes.loadUnapprovedUsers]: {
[ComponentStates.Idle]: { to: ComponentStates.Processing, action: PassthroughAction }
},
[actionTypes.loadUnapprovedUsersSuccess]: {
[ComponentStates.Processing]: { to: ComponentStates.Idle, action: PassthroughAction }
},
@glendaviesnz
glendaviesnz / component-state.service.ts
Last active December 14, 2018 02:58
NgRX State Machine - component state service
// the component uses an injected componentStateService to register its state transitions
this._componentStateService.addComponentStates(componentStates);
// the component state service is reasonably simple and just maintains a lookup object of all the registered
// state transitions, and also has methods for cleaning these up when components are destroyed
@Injectable()
export class ComponentStateService {
public componentStates: any = {};
chunkQueue$
.pipe(mergeMap((data) => data, null, maxConnections))
.subscribe();
export function httpUpload(file) {
return Observable.create((observer) => {
var config = {
onUploadProgress: (progressEvent) => {
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
observer.next({ progress: percentCompleted });
}
};
axios.post(`${appConfig.apiUrl}/upload`, file, config)
.then((response) => {
@glendaviesnz
glendaviesnz / gallery-block-transform-filter.php
Last active May 12, 2021 01:30
Gutenberg Gallery Block Refactor - Transform Back to Unordered List
@glendaviesnz
glendaviesnz / gallery-v2-deprecations.js
Created May 21, 2021 03:54
Gutenberg Gallery Block Refactor Deprecations