Skip to content

Commit b834f7b

Browse files
committed
feat: optional hydration
BREAKING CHANGE: iGetPage no longer hydratable
1 parent 48ab3a4 commit b834f7b

File tree

5 files changed

+40
-33
lines changed

5 files changed

+40
-33
lines changed

packages/common-types/src/fetch.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ export interface iPagination<O = tOrderBy> {
3131
*/
3232
first?: number;
3333
/**
34-
* JSON string
35-
* aplica a cualquiera columna
34+
* Ordenar segun columna o propiedad
3635
* ej: ["id", "asc" ], order ascendente de id
3736
* ej: ["id", "desc" ], order descendente de id
3837
*/
@@ -45,6 +44,5 @@ export interface iPagination<O = tOrderBy> {
4544
export type tHydrate<T> = (c: T, e?: unknown) => void;
4645

4746
export type iGetPage<T, C extends string | number = string> = (
48-
hydrate: tHydrate<iPage<T, C> | null>,
4947
params?: iPagination
5048
) => Promise<iPage<T, C>>;

packages/components-vue/src/components/form/InputCountriesAPI.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
:el="LoaderContentFetch"
66
:wrap="!states && !!countryValue"
77
:theme="theme"
8-
:promise="unHydrate(getCountryStates)"
8+
:promise="getCountryStates"
99
:payload="[countryValue]"
1010
unwrap
1111
>
1212
<LoaderContentFetch
1313
v-slot="citiesReq"
1414
:theme="theme"
15-
:promise="!!model[1] && unHydrate(getStateCities)"
15+
:promise="!!model[1] && getStateCities"
1616
:payload="[countryValue, model[1]]"
1717
:no-loader="statesReq.loading"
1818
:fallback="[]"
@@ -34,7 +34,6 @@
3434
3535
import type { iUseThemeProps } from "../../types/props";
3636
import type { iState } from "../../types/countries";
37-
import useFetch from "../../composables/fetch";
3837
import useCountries from "../../composables/countries";
3938
4039
interface iFormInputCountriesApi extends iUseThemeProps {
@@ -52,7 +51,6 @@
5251
5352
const props = defineProps<iFormInputCountriesApi>();
5453
55-
const { unHydrate } = useFetch();
5654
const { defaultCountry, getCountryStates, getStateCities } = useCountries();
5755
5856
const countryValue = computed(() => props.model[0] || defaultCountry || "");

packages/components-vue/src/components/form/Simple.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
v-slot="{ content }"
66
:theme="theme"
77
:label="t('form_loading_countries')"
8-
:promise="(withLocationInput || withPhoneInput) && getCountriesAndStates"
8+
:promise="(withLocationInput || !!withPhoneInput) && getCountriesAndStates"
99
class="flx --flxColumn --flx-start-stretch --gap-10 --maxWidth-full"
1010
:el="noForm ? 'fieldset' : 'form'"
1111
:fallback="{ countries: [], states: [] }"

packages/components-vue/src/components/loader/ContentFetch.vue

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</BaseErrorBoundary>
2222
</template>
2323

24-
<script setup lang="ts" generic="T, P extends unknown[] = unknown[]">
24+
<script setup lang="ts" generic="T, P extends any[] = any[]">
2525
import {
2626
ref,
2727
watch,
@@ -39,7 +39,9 @@
3939
4040
import type { iUseThemeProps } from "../../types/props";
4141
42-
export interface iLoaderContentFetchProps<Ti, Pi extends unknown[]> extends iUseThemeProps {
42+
type tDataPromise<Ti, Pi extends any[]> = (...args: Pi) => Promise<Ti>;
43+
44+
interface iLoaderContentFetchProps<Ti, Pi extends any[]> extends iUseThemeProps {
4345
/**
4446
* Loader label
4547
*/
@@ -48,10 +50,11 @@
4850
* Hide loader
4951
*/
5052
noLoader?: boolean;
51-
promise?: false | ((hydrate: tHydrate<Ti>, ...args: Pi) => Promise<Ti>);
52-
url?: false | string;
5353
fallback?: Ti;
5454
unwrap?: boolean;
55+
url?: false | string;
56+
promise?: false | tDataPromise<Ti, Pi>;
57+
hydratablePromise?: false | ((hydrate: tHydrate<Ti>) => tDataPromise<Ti, Pi>);
5558
payload?: Pi;
5659
/**
5760
* Component or tag to render
@@ -80,6 +83,11 @@
8083
const fetchedContent = ref<T>();
8184
const firstLoad = ref(false);
8285
const hydrated = ref(false);
86+
/**
87+
* Hydrate content
88+
*
89+
* Useful with firebase client approach
90+
*/
8391
const hydrate: tHydrate<T> = (newContent, newErrors) => {
8492
// prevent hydration
8593
if (fetchedContent.value && !hydrated.value) return (hydrated.value = true);
@@ -91,18 +99,21 @@
9199
const content = computed(() => fetchedContent.value ?? props.fallback);
92100
93101
async function refresh() {
94-
if (props.promise || props.url) {
95-
try {
102+
try {
103+
if (props.promise || props.hydratablePromise || props.url) {
96104
loading.value = true;
97105
98-
// use fallback while the real content loaads
106+
// use fallback while the real content loads
99107
if (props.fallback) firstLoad.value = true;
100108
101-
if (props.promise) {
102-
fetchedContent.value = await props.promise(
103-
hydrate,
104-
...(<P>(props.payload || []))
105-
);
109+
if (props.promise || props.hydratablePromise) {
110+
const payload = <P>(props.payload || []);
111+
112+
if (props.promise) {
113+
fetchedContent.value = await props.promise(...payload);
114+
} else if (props.hydratablePromise) {
115+
fetchedContent.value = await props.hydratablePromise(hydrate)(...payload);
116+
}
106117
} else if (props.url) {
107118
const response = await (await fetch(props.url)).json();
108119
const data = "data" in response ? response.data : response;
@@ -113,15 +124,17 @@
113124
114125
// success, clear errors
115126
errors.value = undefined;
116-
} catch (err) {
117-
console.error(err);
118-
fetchedContent.value = undefined;
119-
errors.value = err;
127+
} else {
128+
throw new Error("No data source url or promise provided");
120129
}
121-
122-
firstLoad.value = true;
123-
loading.value = false;
130+
} catch (err) {
131+
console.error(err);
132+
fetchedContent.value = undefined;
133+
errors.value = err;
124134
}
135+
136+
firstLoad.value = true;
137+
loading.value = false;
125138
}
126139
127140
// lifecycle

packages/components-vue/src/components/pagination/Content.vue

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
3737
import type { iUseThemeProps } from "../../types/props";
3838
39-
export interface iPaginationContentProps<Ti, Ci extends string | number = string>
39+
export interface iPCProps<Ti, Ci extends string | number = string>
4040
extends iPagination,
4141
iUseThemeProps {
4242
/**
@@ -63,10 +63,8 @@
6363
}
6464
6565
/**
66-
* Menu de paginacion [PROGRESS]
66+
* Menu de paginacion
6767
* Redirecciona a la misma ruta + el query de pagina
68-
* TODO: Add conditional items per page selector
69-
* Not sure what this was supposed to mean
7068
*
7169
* @component
7270
* @example
@@ -75,7 +73,7 @@
7573
7674
defineOptions({ name: "PaginationContent", inheritAttrs: false });
7775
78-
const props = defineProps<iPaginationContentProps<T, C>>();
76+
const props = defineProps<iPCProps<T, C>>();
7977
8078
const xamuOptions = inject<iPluginOptions>("xamu");
8179
const router = getCurrentInstance()?.appContext.config.globalProperties.$router;
@@ -111,7 +109,7 @@
111109
112110
return newPagination;
113111
});
114-
const pagination = computed<iPagination | undefined>({
112+
const pagination = computed<iPagination>({
115113
get() {
116114
if (props.withRoute) return routePagination.value;
117115

0 commit comments

Comments
 (0)