1.0.259 • Published 1 year ago

shulga-app-core v1.0.259

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

Not realease version.

Class: EventHandling

Contain a set of ready-made handlers for your events in the DOM

import { EventHandling } from 'shulga-app-core'

// #onScrollBottom
const enabled = true

const Component = () => {
    return <ScrollLayout onScroll={EventHandling.onScrollBottom((e) => callback(e), enabled)} />
}

// #toScrollBottom
const GoBottomButton = () => {
    return (
        <Button onClick={() => EventHandling.toScrollBottom(scrollRef)}>
            <Icon as={ArrowDownOutlined} />
        </Button>
    )
}

Class: RequiredEnv

What problem are we solving?

When starting the application, we may have N keys in proccess.env and some of them can be required. The RequiredEnv class takes env keys to the input and checks the library using runtypes (the contract is being built automatically) the presence of keys in proccess.env and compares with the type of Runtypes.String. Check is performed when initializing the class.

import { RequiredEnv } from "shulga-app-core";

// Example#1

process.env = {
  REACT_APP_API: "https://anydomain.com", // required
  REACT_APP_CI_TOKEN: "hash",
};

const env1 = new RequiredEnv(["REACT_APP_API"] as const, { checkOnInitializeClass: true }); // it's ok, no error

const values1 = env1.getVariables(); // Record<https://anydomain.com, string | undefined>

// Example#2

process.env = {
  REACT_APP_API: undefined, // but required ???
  REACT_APP_CI_TOKEN: "hash",
};

const env2 = new RequiredEnv(["https://anydomain.com"] as const, { checkOnInitializeClass: true }); // oops, throw exception

Class: UrlSerializer

What problem are we solving?

When developing a large application, we have a large set of routes and we all store them differently, and if we change the domain or the path in the route, we need to change from one to all routes. I'd like to have route inheritance and IDE hints for quality DX. This class provides inheritance from route, link and query parameters.

const serializer = new UrlSerializer(root)
const users = serializer.path('users').build()

Users

const user = users.extend().param('userId').build()

User

const posts = user.extend().path('posts').build()

Posts

const post = posts
                .extend()
                .param('postId')
                .build()
const usersLink = users
                    .link({})
const userLink = user
                    .link({ ':userId': '10' })
const postsLink = posts
                    .link({ ':userId': '10' })
const postLink = post
                    .link({ ':userId': '10' as const, ':postId': '20' as const })

Postlink

const postLink = post.link({ ':userId': '10' })

Postlink

const extendedLink = postLink
    .extend()
    .path('likes')
    .param('likeId')
    .path('user')
    .build()
    .link({ ':likeId': '30' })

Extended


Hooks: useCollectionPaging

What problem are we solving?

It happens that we do not have server-side pagination and we need to implement it on the front.

This pagination supports page listing, navigation through increment or decrement of page number or hard setting of page number. There is support for switching pagination pages via function calls nextPagingPage | prevPagingPage.

useCollectionPaging also returns other useful properties for your web applications.

import { useCollectionPaging } from 'shulga-app-core/hooks'

const pagingProps = useCollectionPaging({
    startsWith: 1,
    pageSize: 15,
    paginationSize: 6,
    collection: [1, 2, 3, 4, 5, 6]
    onMount: true
});

pagingProps.collection // > array of page items
pagingProps.isFirstPage // > boolean (first page of pages list)
pagingProps.isLastPage // > boolean (last page of pages list)
pagingProps.isFirstPagingPage // > boolean (first page of pagination list)
pagingProps.isLastPagingPage // > boolean (last page of pagination list)
pagingProps.nextPage() // > go page + 1
pagingProps.prevPage() // > go page - 1
pagingProps.updatePage(10) // > set page 10
pagingProps.nextPagingPage() // > go next page of pagination list
pagingProps.prevPagingPage() // > go prev page of pagination list
pagingProps.page // > current page
pagingProps.pages // > visible pages of pagination list

Hooks: useCollectionSort

What problem are we solving?

Sometimes we have to sort data at the front and the default sort is either not enough or we have to write a lot of handler functions for [].sort().

This sort narrows the types of processed values to boolean | number | string and implements the shift order ASC > DESC > default > ASC > DESC > default and so on.

The order update is implemented by calling the update property without passing a parameter. But if we want to change the property by which we sort, specify field in the update parameters.

Through the property handler handler: (item) => primitive we can customize our property value in [].sort().

The field value supports JSONPath autocompletion.

You can work with primitive arrays without specifying field

Null | undefiend values will be moved to the end of the array in any sort order.

The order is determined by the orders property. Available 'ASC' | 'DESC' | 'default', you can specify any combination of values.

Performance lags slightly behind the default [].sort() method up to 1 million objects in the sorted array

import { useCollectionSort } from 'shulga-app-core/hooks'

const arrayOfObjects = useCollectionSort({
    collection: todos,
    order: 'ASC',
    orders: ['ASC', 'DESC', 'default'],
    field: 'id',
})

const arrayOfPrimitievs = useCollectionSort({
    collection: [1, 2, 3, 4, 5, 6, 7],
    order: 'ASC',
    orders: ['ASC', 'DESC', 'default'],
})

arrayOfObjects.cleanup // > reset to initial state
arrayOfObjects.collection // > sorted collection
arrayOfObjects.field // > current field
arrayOfObjects.order // > current order
arrayOfObjects.orders // > current orders
arrayOfObjects.update // > update fn

Hooks: usePaging

What problem are we solving?

usePaging is for server pagination.

This pagination supports page listing, navigation through increment or decrement of page number or hard setting of page number. There is support for switching pagination pages via function calls nextPagingPage | prevPagingPage.

usePaging also returns other useful properties for your web applications.

import { usePaging } from 'shulga-app-core/hooks'

const pagingProps = usePaging({
    startsWith: 1,
    pageSize: 6,
    paginationSize: 5,
    itemsCount: 10,
})

pagingProps.isFirstPage // > boolean (first page of pages list)
pagingProps.isLastPage // > boolean (last page of pages list)
pagingProps.isFirstPagingPage // > boolean (first page of pagination list)
pagingProps.isLastPagingPage // > boolean (last page of pagination list)
pagingProps.nextPage() // > go page + 1
pagingProps.prevPage() // > go page - 1
pagingProps.updatePage(10) // > set page 10
pagingProps.nextPagingPage() // > go next page of pagination list
pagingProps.prevPagingPage() // > go prev page of pagination list
pagingProps.page // > current page
pagingProps.pages // > visible pages of pagination list

// Ugly example for usage
const getTodos = React.useCallback((page: number) => {
    axios
        .get(`/users?page=${pagingProps.page}`)
        .then((response) => {})
        .catch((err) => {})
}, [])

React.useEffect(() => {
    getTodos(pagingProps.page)
}, [pagingProps.page])
1.0.259

1 year ago

1.0.258

1 year ago

1.0.257

1 year ago

1.0.256

1 year ago

1.0.255

1 year ago

1.0.254

1 year ago

1.0.253

1 year ago

1.0.252

1 year ago

1.0.251

1 year ago

1.0.250

1 year ago

1.0.249

1 year ago

1.0.248

1 year ago

1.0.247

1 year ago

1.0.246

1 year ago

1.0.245

1 year ago

1.0.244

1 year ago

1.0.243

1 year ago

1.0.242

1 year ago

1.0.241

1 year ago

1.0.240

1 year ago

1.0.239

1 year ago

1.0.238

1 year ago

1.0.237

1 year ago

1.0.236

1 year ago

1.0.235

1 year ago

1.0.234

1 year ago

1.0.233

1 year ago

1.0.232

1 year ago

1.0.231

1 year ago

1.0.230

1 year ago

1.0.229

1 year ago

1.0.228

1 year ago

1.0.227

1 year ago

1.0.226

1 year ago

1.0.225

1 year ago

1.0.224

1 year ago

1.0.223

1 year ago

1.0.222

1 year ago

1.0.221

1 year ago

1.0.220

1 year ago

1.0.219

1 year ago

1.0.218

1 year ago

1.0.217

1 year ago

1.0.216

1 year ago

1.0.214

1 year ago

1.0.213

1 year ago

1.0.212

1 year ago

1.0.211

1 year ago

1.0.210

1 year ago

1.0.209

1 year ago

1.0.208

1 year ago

1.0.207

1 year ago

1.0.206

1 year ago

1.0.205

1 year ago

1.0.204

1 year ago

1.0.203

1 year ago

1.0.202

1 year ago

1.0.201

1 year ago

1.0.200

1 year ago

1.0.199

1 year ago

1.0.198

1 year ago

1.0.197

1 year ago

1.0.196

1 year ago

1.0.195

1 year ago

1.0.194

1 year ago

1.0.193

1 year ago

1.0.192

1 year ago

1.0.191

1 year ago

1.0.190

1 year ago

1.0.189

1 year ago

1.0.188

1 year ago

1.0.187

1 year ago

1.0.186

1 year ago

1.0.185

1 year ago

1.0.184

1 year ago

1.0.183

1 year ago

1.0.182

1 year ago

1.0.181

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago