|
| 1 | +// src/lib/AuthorCrud.ts |
| 2 | +import { |
| 3 | + CallbackAction, |
| 4 | + CallbackStateProcessor, |
| 5 | + CallbackStateProvider, |
| 6 | + CrudDefinition, |
| 7 | + DateField, |
| 8 | + DateRangeFilter, |
| 9 | + Delete, |
| 10 | + Edit, |
| 11 | + List, |
| 12 | + New, |
| 13 | + PaginatedResults, |
| 14 | + TextareaField, |
| 15 | + TextField, |
| 16 | + TextFilter, |
| 17 | + type RequestParameters, |
| 18 | + UrlAction, |
| 19 | + View |
| 20 | +} from '$lib'; |
| 21 | + |
| 22 | +import { faker } from '@faker-js/faker'; |
| 23 | + |
| 24 | +import Pen from 'carbon-icons-svelte/lib/Pen.svelte'; |
| 25 | +import TrashCan from 'carbon-icons-svelte/lib/TrashCan.svelte'; |
| 26 | +import ViewIcon from 'carbon-icons-svelte/lib/View.svelte'; |
| 27 | +import { type Author, getStorage } from './internal/authorsInternal'; |
| 28 | + |
| 29 | +const fields = [ |
| 30 | + new TextField('first_name', 'First name'), |
| 31 | + new TextField('last_name', 'Last name'), |
| 32 | + new TextareaField('Bio', 'Biography') |
| 33 | +]; |
| 34 | + |
| 35 | +const IdField = new TextField('id', 'ID'); |
| 36 | + |
| 37 | +const itemsPerPage = 10; |
| 38 | + |
| 39 | +function randomWait(maxMilliseconds: number) { |
| 40 | + return new Promise((resolve: (...args: unknown[]) => unknown) => |
| 41 | + setTimeout(resolve, Math.random() * maxMilliseconds) |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +export const authorCrud = new CrudDefinition<Author>({ |
| 46 | + name: 'authors', |
| 47 | + label: { singular: 'Author', plural: 'Authors' }, |
| 48 | + minStateLoadingTimeMs: 400, |
| 49 | + |
| 50 | + operations: [ |
| 51 | + new List( |
| 52 | + [IdField, ...fields], |
| 53 | + [ |
| 54 | + new UrlAction('View', '/admin/authors/view', ViewIcon), |
| 55 | + new UrlAction('Edit', '/admin/authors/edit', Pen), |
| 56 | + new UrlAction('Delete', '/admin/authors/delete', TrashCan) |
| 57 | + ], |
| 58 | + { |
| 59 | + globalActions: [ |
| 60 | + new CallbackAction( |
| 61 | + 'Reset memory data', |
| 62 | + TrashCan, |
| 63 | + () => { |
| 64 | + window.localStorage.removeItem('authors'); |
| 65 | + window.location.reload(); |
| 66 | + }, |
| 67 | + { buttonKind: 'ghost' } |
| 68 | + ), |
| 69 | + new UrlAction('New', '/admin/authors/new', Pen) |
| 70 | + ], |
| 71 | + pagination: { |
| 72 | + enabled: true, |
| 73 | + itemsPerPage: itemsPerPage |
| 74 | + }, |
| 75 | + filters: [ |
| 76 | + new TextFilter('first_name', 'First name contains'), |
| 77 | + new TextFilter('last_name', 'Last name contains'), |
| 78 | + new TextFilter('bio', 'Biography contains') |
| 79 | + ] |
| 80 | + } |
| 81 | + ), |
| 82 | + new View([IdField, ...fields]), |
| 83 | + new New(fields), |
| 84 | + new Edit(fields), |
| 85 | + new Delete(fields, new UrlAction('List', '/admin/authors/list')) |
| 86 | + ], |
| 87 | + |
| 88 | + stateProcessor: new CallbackStateProcessor(function ( |
| 89 | + data, |
| 90 | + operation, |
| 91 | + requestParameters: RequestParameters = {} |
| 92 | + ) { |
| 93 | + if (operation.name === 'delete') { |
| 94 | + const id = (requestParameters.id || '').toString(); |
| 95 | + getStorage().remove(id); |
| 96 | + |
| 97 | + return Promise.resolve(); |
| 98 | + } |
| 99 | + |
| 100 | + if (operation.name === 'edit' || operation.name === 'new') { |
| 101 | + const id = |
| 102 | + operation.name === 'edit' ? (requestParameters.id || '').toString() : faker.string.uuid(); |
| 103 | + const entity = data as Author; |
| 104 | + entity.id = id; |
| 105 | + |
| 106 | + if (operation.name === 'new') { |
| 107 | + getStorage().add(entity); |
| 108 | + } else { |
| 109 | + getStorage().update(entity); |
| 110 | + } |
| 111 | + |
| 112 | + return Promise.resolve(); |
| 113 | + } |
| 114 | + |
| 115 | + console.error( |
| 116 | + 'StateProcessor error: Unsupported Authors Crud action "' + operation.name + '".' |
| 117 | + ); |
| 118 | + |
| 119 | + return Promise.resolve(); |
| 120 | + }), |
| 121 | + |
| 122 | + stateProvider: new CallbackStateProvider<Author>(async function ( |
| 123 | + operation, |
| 124 | + requestParameters: RequestParameters = {} |
| 125 | + ) { |
| 126 | + console.info('Authors provider called', { operation: operation.name, requestParameters }); |
| 127 | + |
| 128 | + if (operation.name === 'list') { |
| 129 | + const page = parseInt((requestParameters.page || '1').toString()); |
| 130 | + if (isNaN(page)) { |
| 131 | + throw new Error(`Invalid "page" value: expected a number, got "${page}".`); |
| 132 | + } |
| 133 | + |
| 134 | + let entities = getStorage().all(); |
| 135 | + const filters = requestParameters.filters; |
| 136 | + if (filters) { |
| 137 | + entities = entities.filter((entity) => { |
| 138 | + if ( |
| 139 | + filters.first_name && |
| 140 | + !entity.first_name.match(new RegExp(filters.first_name.toString(), 'gi')) |
| 141 | + ) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + if ( |
| 145 | + filters.last_name && |
| 146 | + !entity.last_name.match(new RegExp(filters.last_name.toString(), 'gi')) |
| 147 | + ) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + if (filters.bio && !entity.bio.match(new RegExp(filters.bio.toString(), 'gi'))) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + return true; |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + const listEntities = entities.slice(itemsPerPage * (page - 1), itemsPerPage * page); |
| 159 | + |
| 160 | + await randomWait(500); |
| 161 | + |
| 162 | + return new PaginatedResults( |
| 163 | + page, |
| 164 | + Math.ceil(entities.length / itemsPerPage), |
| 165 | + entities.length, |
| 166 | + listEntities |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + if (operation.name === 'edit' || operation.name === 'view') { |
| 171 | + return Promise.resolve(getStorage().get((requestParameters?.id || '').toString())); |
| 172 | + } |
| 173 | + |
| 174 | + if (operation.name === 'entity_view') { |
| 175 | + return Promise.resolve(getStorage().get((requestParameters?.field_value || '').toString())); |
| 176 | + } |
| 177 | + |
| 178 | + if (operation.name === 'entity_list') { |
| 179 | + return Promise.resolve(getStorage().all()); |
| 180 | + } |
| 181 | + |
| 182 | + console.error('StateProvider error: Unsupported Authors Crud action "' + operation.name + '".'); |
| 183 | + |
| 184 | + return Promise.resolve(null); |
| 185 | + }) |
| 186 | +}); |
0 commit comments