-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert project over to loadable data
- Loading branch information
1 parent
088ba12
commit 45c3f1d
Showing
14 changed files
with
267 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,62 @@ | ||
export type LoadableData<T, Id> = | ||
| { type: 'loading' | 'not-found'; id: Id; error?: undefined } | ||
| { type: 'found'; id: Id; value: T; error?: undefined } | ||
| { type: 'error'; id: Id; error: Error }; | ||
import { ApiError } from '$lib/network/types'; | ||
import type { EntityId, EntityAdapter, EntityState } from '@reduxjs/toolkit'; | ||
|
||
export type Loadable<T> = | ||
| { type: 'loading' | 'not-found' } | ||
| { type: 'found'; value: T } | ||
| { type: 'error'; error: Error }; | ||
|
||
export type LoadableData<T, Id> = Loadable<T> & { id: Id }; | ||
|
||
export function errorToLoadable<T, Id>(error: unknown, id: Id): LoadableData<T, Id> { | ||
if (error instanceof Error) { | ||
if (error instanceof ApiError && error.response.status === 404) { | ||
return { type: 'not-found', id }; | ||
} | ||
|
||
return { type: 'error', id, error }; | ||
} | ||
|
||
return { type: 'error', id, error: new Error(String(error)) }; | ||
} | ||
|
||
export function loadableUpsert<T, Id extends EntityId>( | ||
adapter: EntityAdapter<LoadableData<T, Id>, Id> | ||
) { | ||
return ( | ||
state: EntityState<LoadableData<T, Id>, Id>, | ||
action: { payload: LoadableData<T, Id> } | ||
) => { | ||
loadableUpsertMany(adapter)(state, { payload: [action.payload] }); | ||
}; | ||
} | ||
|
||
export function loadableUpsertMany<T, Id extends EntityId>( | ||
adapter: EntityAdapter<LoadableData<T, Id>, Id> | ||
) { | ||
return ( | ||
state: EntityState<LoadableData<T, Id>, Id>, | ||
action: { payload: LoadableData<T, Id>[] } | ||
) => { | ||
const values = action.payload.map((payload) => { | ||
const value = state.entities[payload.id]; | ||
if (value === undefined) { | ||
return payload; | ||
} | ||
|
||
if (!(value.type === 'found' && payload.type === 'found')) { | ||
return payload; | ||
} | ||
|
||
const newValue: LoadableData<T, Id> = { | ||
type: 'found', | ||
id: payload.id, | ||
value: { ...value, ...payload.value } | ||
}; | ||
|
||
return newValue; | ||
}); | ||
|
||
adapter.setMany(state, values); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.