Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
feat(examples): improve next-rtk-example
Browse files Browse the repository at this point in the history
  • Loading branch information
fostyfost committed Nov 11, 2021
1 parent 9a45d08 commit cb68599
Show file tree
Hide file tree
Showing 28 changed files with 87 additions and 107 deletions.
38 changes: 34 additions & 4 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions examples/example-next-rtk/eggs/active-post/contracts/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Immutable } from 'immer'

import type { ACTIVE_POST_SLICE } from '@/eggs/active-post/slice'

export enum ActivePostLoadingState {
Expand All @@ -18,8 +16,8 @@ export interface ActivePost {
body: string
}

export type ActivePostState = Immutable<{
export interface ActivePostState {
activePost?: ActivePost
error?: string
loadingState: ActivePostLoadingState
}>
}
5 changes: 2 additions & 3 deletions examples/example-next-rtk/eggs/active-post/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { Immutable } from 'immer'
import { createSelector } from 'reselect'
import { createSelector } from '@reduxjs/toolkit'

import type { ActivePost, ActivePostAwareState } from '@/eggs/active-post/contracts/state'
import { ActivePostLoadingState } from '@/eggs/active-post/contracts/state'
import { ACTIVE_POST_SLICE } from '@/eggs/active-post/slice'

export const activePostSelector = (state: ActivePostAwareState): Immutable<ActivePost> | undefined => {
export const activePostSelector = (state: ActivePostAwareState): ActivePost | undefined => {
return state[ACTIVE_POST_SLICE].activePost
}

Expand Down
6 changes: 2 additions & 4 deletions examples/example-next-rtk/eggs/aviasales/contracts/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Immutable } from 'immer'

import type { AviasalesLoadingState } from '@/eggs/aviasales/contracts/loading-state'
import type { Sort } from '@/eggs/aviasales/contracts/sort'
import type { Ticket } from '@/eggs/aviasales/contracts/ticket'
Expand All @@ -10,14 +8,14 @@ export interface AviasalesAwareState {
[AVIASALES_SLICE]: AviasalesState
}

export type AviasalesState = Immutable<{
export interface AviasalesState {
searchId?: string
tickets: TicketsMap
ticketsSegments: TicketsSegmentsMap
loadingState: AviasalesLoadingState
currentSort: Sort
stops: number[]
}>
}

export interface TicketsMap {
[id: string]: Ticket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* @jest-environment node
*/

import { combineReducers } from '@reduxjs/toolkit'
import { nanoid } from 'nanoid'
import { combineReducers, nanoid } from '@reduxjs/toolkit'
import { expectSaga } from 'redux-saga-test-plan'
import * as matchers from 'redux-saga-test-plan/matchers'
import { dynamic } from 'redux-saga-test-plan/providers'
Expand Down
18 changes: 7 additions & 11 deletions examples/example-next-rtk/eggs/aviasales/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Immutable } from 'immer'
import { createSelector } from 'redux-views'

import { AVAILABLE_STOPS, MAX_TICKETS_LENGTH_TO_SHOW } from '@/eggs/aviasales/constants'
Expand All @@ -13,21 +12,21 @@ export const searchIdSelector = (state: AviasalesAwareState): string | undefined
return state[AVIASALES_SLICE].searchId
}

export const ticketsSelector = (state: AviasalesAwareState): Immutable<TicketsMap> => {
export const ticketsSelector = (state: AviasalesAwareState): TicketsMap => {
return state[AVIASALES_SLICE].tickets
}

export const currentSortSelector = (state: AviasalesAwareState): Sort => {
return state[AVIASALES_SLICE].currentSort
}

export const stopsSelector = (state: AviasalesAwareState): Immutable<number[]> => {
export const stopsSelector = (state: AviasalesAwareState): number[] => {
return state[AVIASALES_SLICE].stops
}

export const ticketsArraySelector = createSelector(
[ticketsSelector, stopsSelector, currentSortSelector],
(tickets: Immutable<TicketsMap>, stops, currentSort: Sort): Immutable<Ticket>[] => {
(tickets: TicketsMap, stops, currentSort: Sort): Ticket[] => {
let filteredTickets = Object.values(tickets).filter(
ticket => stops.includes(ticket.stops[0]) && stops.includes(ticket.stops[1]),
)
Expand All @@ -42,18 +41,15 @@ export const ticketsArraySelector = createSelector(
},
)

export const ticketsIdsSelector = createSelector([ticketsArraySelector], (tickets: Immutable<Ticket>[]): string[] => {
export const ticketsIdsSelector = createSelector([ticketsArraySelector], (tickets: Ticket[]): string[] => {
return tickets.map(ticket => ticket.id)
})

export const getTicketByIdSelector = (state: AviasalesAwareState, id: string): Immutable<Ticket> | undefined => {
export const getTicketByIdSelector = (state: AviasalesAwareState, id: string): Ticket | undefined => {
return state[AVIASALES_SLICE].tickets[id]
}

export const getTicketSegmentByIdSelector = (
state: AviasalesAwareState,
id: string,
): Immutable<TicketSegment> | undefined => {
export const getTicketSegmentByIdSelector = (state: AviasalesAwareState, id: string): TicketSegment | undefined => {
return state[AVIASALES_SLICE].ticketsSegments[id]
}

Expand All @@ -73,6 +69,6 @@ export const isAllTicketLoadedSelector = createSelector(

const availableStopsAsString = AVAILABLE_STOPS.join(',')

export const isAllStopsSelectedSelector = createSelector([stopsSelector], (stops: Immutable<number[]>): boolean => {
export const isAllStopsSelectedSelector = createSelector([stopsSelector], (stops: number[]): boolean => {
return [...stops].sort().join(',') === availableStopsAsString
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nanoid } from 'nanoid'
import { nanoid } from '@reduxjs/toolkit'

import { normalizeTicketsResponse } from '@/eggs/aviasales/utils/normalize-tickets-response'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nanoid } from 'nanoid'
import { nanoid } from '@reduxjs/toolkit'

import { MAX_STOPS, SEGMENTS_LENGTH } from '@/eggs/aviasales/constants'
import type { RawTicket } from '@/eggs/aviasales/contracts/api-response'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Immutable } from 'immer'

import type { CHUCK_NORRIS_SLICE } from '@/eggs/chuck-norris/slice'

export enum ChuckNorrisLoadingState {
Expand All @@ -12,8 +10,8 @@ export interface ChuckNorrisAwareState {
[CHUCK_NORRIS_SLICE]: ChuckNorrisState
}

export type ChuckNorrisState = Immutable<{
export interface ChuckNorrisState {
joke?: string
error?: string
loadingState: ChuckNorrisLoadingState
}>
}
2 changes: 1 addition & 1 deletion examples/example-next-rtk/eggs/chuck-norris/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSelector } from 'reselect'
import { createSelector } from '@reduxjs/toolkit'

import type { ChuckNorrisAwareState } from '@/eggs/chuck-norris/contracts/state'
import { ChuckNorrisLoadingState } from '@/eggs/chuck-norris/contracts/state'
Expand Down
6 changes: 2 additions & 4 deletions examples/example-next-rtk/eggs/clock/contracts/state.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { Immutable } from 'immer'

import type { CLOCK_SLICE } from '@/eggs/clock/slice'

export interface ClockAwareState {
[CLOCK_SLICE]: ClockState
}

export type ClockState = Immutable<{
export interface ClockState {
lastUpdate: number
}>
}
6 changes: 2 additions & 4 deletions examples/example-next-rtk/eggs/count/contracts/state.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { Immutable } from 'immer'

import type { COUNT_SLICE } from '@/eggs/count/slice'

export interface CountAwareState {
[COUNT_SLICE]: CountState
}

export type CountState = Immutable<{
export interface CountState {
count: number
}>
}
6 changes: 2 additions & 4 deletions examples/example-next-rtk/eggs/dog/contracts/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Immutable } from 'immer'

import type { DOG_SLICE } from '@/eggs/dog/slice'

export enum DogLoadingState {
Expand All @@ -12,8 +10,8 @@ export interface DogAwareState {
[DOG_SLICE]: DogState
}

export type DogState = Immutable<{
export interface DogState {
dog?: string
error?: string
loadingState: DogLoadingState
}>
}
2 changes: 1 addition & 1 deletion examples/example-next-rtk/eggs/dog/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSelector } from 'reselect'
import { createSelector } from '@reduxjs/toolkit'

import type { DogAwareState } from '@/eggs/dog/contracts/state'
import { DogLoadingState } from '@/eggs/dog/contracts/state'
Expand Down
6 changes: 2 additions & 4 deletions examples/example-next-rtk/eggs/fox/contracts/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Immutable } from 'immer'

import type { FOX_SLICE } from '@/eggs/fox/slice'

export enum FoxLoadingState {
Expand All @@ -12,8 +10,8 @@ export interface FoxAwareState {
[FOX_SLICE]: FoxState
}

export type FoxState = Immutable<{
export interface FoxState {
fox?: string
error?: string
loadingState: FoxLoadingState
}>
}
2 changes: 1 addition & 1 deletion examples/example-next-rtk/eggs/fox/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSelector } from 'reselect'
import { createSelector } from '@reduxjs/toolkit'

import type { FoxAwareState } from '@/eggs/fox/contracts/state'
import { FoxLoadingState } from '@/eggs/fox/contracts/state'
Expand Down
6 changes: 2 additions & 4 deletions examples/example-next-rtk/eggs/picsum/contracts/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Immutable } from 'immer'

import type { Picture } from '@/eggs/picsum/contracts/picture'
import type { PICSUM_SLICE } from '@/eggs/picsum/slice'

Expand All @@ -13,8 +11,8 @@ export interface PicsumAwareState {
[PICSUM_SLICE]: PicsumState
}

export type PicsumState = Immutable<{
export interface PicsumState {
pics?: Picture[]
error?: string
loadingState: PicsumLoadingState
}>
}
5 changes: 2 additions & 3 deletions examples/example-next-rtk/eggs/picsum/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { Immutable } from 'immer'
import { createSelector } from 'reselect'
import { createSelector } from '@reduxjs/toolkit'

import type { Picture } from '@/eggs/picsum/contracts/picture'
import type { PicsumAwareState } from '@/eggs/picsum/contracts/state'
import { PicsumLoadingState } from '@/eggs/picsum/contracts/state'
import { PICSUM_SLICE } from '@/eggs/picsum/slice'

export const picsSelector = (state: PicsumAwareState): Immutable<Picture[]> | undefined => {
export const picsSelector = (state: PicsumAwareState): Picture[] | undefined => {
return state[PICSUM_SLICE].pics
}

Expand Down
6 changes: 2 additions & 4 deletions examples/example-next-rtk/eggs/posts/contracts/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Immutable } from 'immer'

import type { POSTS_SLICE } from '@/eggs/posts/slice'

export enum PostsLoadingState {
Expand All @@ -17,8 +15,8 @@ export interface PostsItem {
title: string
}

export type PostsState = Immutable<{
export interface PostsState {
posts: PostsItem[]
error?: string
loadingState: PostsLoadingState
}>
}
5 changes: 2 additions & 3 deletions examples/example-next-rtk/eggs/posts/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { Immutable } from 'immer'
import { createSelector } from 'reselect'
import { createSelector } from '@reduxjs/toolkit'

import type { PostsAwareState, PostsItem } from '@/eggs/posts/contracts/state'
import { PostsLoadingState } from '@/eggs/posts/contracts/state'
import { POSTS_SLICE } from '@/eggs/posts/slice'

export const postsSelector = (state: PostsAwareState): Immutable<PostsItem[]> => {
export const postsSelector = (state: PostsAwareState): PostsItem[] => {
return state[POSTS_SLICE].posts
}

Expand Down
6 changes: 2 additions & 4 deletions examples/example-next-rtk/eggs/users/contracts/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Immutable } from 'immer'

import type { User } from '@/eggs/users/contracts/user'
import type { USERS_SLICE } from '@/eggs/users/slice'

Expand All @@ -13,8 +11,8 @@ export interface UsersAwareState {
[USERS_SLICE]: UsersState
}

export type UsersState = Immutable<{
export interface UsersState {
users: User[]
error?: string
loadingState: UsersLoadingState
}>
}
5 changes: 2 additions & 3 deletions examples/example-next-rtk/eggs/users/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { Immutable } from 'immer'
import { createSelector } from 'reselect'
import { createSelector } from '@reduxjs/toolkit'

import type { UsersAwareState } from '@/eggs/users/contracts/state'
import { UsersLoadingState } from '@/eggs/users/contracts/state'
import type { User } from '@/eggs/users/contracts/user'
import { USERS_SLICE } from '@/eggs/users/slice'

export const usersSelector = (state: UsersAwareState): Immutable<User[]> => {
export const usersSelector = (state: UsersAwareState): User[] => {
return state[USERS_SLICE].users
}

Expand Down
Loading

0 comments on commit cb68599

Please sign in to comment.