@frontic/nuxt 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.cjs +5 -0
- package/dist/module.d.mts +48 -0
- package/dist/module.d.ts +48 -0
- package/dist/module.json +12 -0
- package/dist/module.mjs +120 -0
- package/dist/runtime/composables/index.d.ts +0 -0
- package/dist/runtime/composables/index.js +2 -0
- package/dist/runtime/composables/listings.d.ts +0 -0
- package/dist/runtime/composables/listings.js +45 -0
- package/dist/runtime/composables/useFronticListing.d.ts +0 -0
- package/dist/runtime/composables/useFronticListing.js +208 -0
- package/dist/runtime/types/index.d.ts +0 -0
- package/dist/runtime/types/index.js +0 -0
- package/dist/runtime/types/listing.d.ts +0 -0
- package/dist/runtime/types/listing.js +0 -0
- package/dist/runtime/utils/filter-helpers.d.ts +0 -0
- package/dist/runtime/utils/filter-helpers.js +46 -0
- package/dist/runtime/utils/index.d.ts +0 -0
- package/dist/runtime/utils/index.js +2 -0
- package/dist/runtime/utils/state-helpers.d.ts +0 -0
- package/dist/runtime/utils/state-helpers.js +88 -0
- package/dist/types.d.mts +7 -0
- package/dist/types.d.ts +7 -0
- package/package.json +63 -0
package/dist/module.cjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { NuxtModule } from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Available Frontic composables that can be enabled/disabled
|
|
5
|
+
*/
|
|
6
|
+
type FronticComposable = 'listing' | 'page' | 'block' | 'context';
|
|
7
|
+
/**
|
|
8
|
+
* Frontic composables configuration
|
|
9
|
+
* - true: Enable all composables (default)
|
|
10
|
+
* - false or []: Disable all composables
|
|
11
|
+
* - string[]: Enable only specified composables
|
|
12
|
+
*/
|
|
13
|
+
type FronticComposablesConfig = boolean | FronticComposable[];
|
|
14
|
+
/**
|
|
15
|
+
* Frontic module configuration options
|
|
16
|
+
*/
|
|
17
|
+
interface FronticOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Prefix for all the imported components
|
|
20
|
+
* @default "Ui"
|
|
21
|
+
*/
|
|
22
|
+
prefix: string;
|
|
23
|
+
/**
|
|
24
|
+
* Directory that the components live in.
|
|
25
|
+
* @default "@/components/ui"
|
|
26
|
+
*/
|
|
27
|
+
componentDir: string;
|
|
28
|
+
/**
|
|
29
|
+
* Configure which Frontic composables to enable
|
|
30
|
+
* - true: Enable all composables (default)
|
|
31
|
+
* - false or []: Disable all composables
|
|
32
|
+
* - string[]: Enable only specified composables
|
|
33
|
+
*
|
|
34
|
+
* Available: 'listing', 'page', 'block', 'context'
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* composables: true // Enable all
|
|
38
|
+
* composables: false // Disable all
|
|
39
|
+
* composables: ['listing', 'page'] // Enable specific ones
|
|
40
|
+
*
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
composables?: FronticComposablesConfig;
|
|
44
|
+
}
|
|
45
|
+
declare const module: NuxtModule<FronticOptions>;
|
|
46
|
+
|
|
47
|
+
export { module as default };
|
|
48
|
+
export type { FronticComposable, FronticComposablesConfig, FronticOptions };
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { NuxtModule } from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Available Frontic composables that can be enabled/disabled
|
|
5
|
+
*/
|
|
6
|
+
type FronticComposable = 'listing' | 'page' | 'block' | 'context';
|
|
7
|
+
/**
|
|
8
|
+
* Frontic composables configuration
|
|
9
|
+
* - true: Enable all composables (default)
|
|
10
|
+
* - false or []: Disable all composables
|
|
11
|
+
* - string[]: Enable only specified composables
|
|
12
|
+
*/
|
|
13
|
+
type FronticComposablesConfig = boolean | FronticComposable[];
|
|
14
|
+
/**
|
|
15
|
+
* Frontic module configuration options
|
|
16
|
+
*/
|
|
17
|
+
interface FronticOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Prefix for all the imported components
|
|
20
|
+
* @default "Ui"
|
|
21
|
+
*/
|
|
22
|
+
prefix: string;
|
|
23
|
+
/**
|
|
24
|
+
* Directory that the components live in.
|
|
25
|
+
* @default "@/components/ui"
|
|
26
|
+
*/
|
|
27
|
+
componentDir: string;
|
|
28
|
+
/**
|
|
29
|
+
* Configure which Frontic composables to enable
|
|
30
|
+
* - true: Enable all composables (default)
|
|
31
|
+
* - false or []: Disable all composables
|
|
32
|
+
* - string[]: Enable only specified composables
|
|
33
|
+
*
|
|
34
|
+
* Available: 'listing', 'page', 'block', 'context'
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* composables: true // Enable all
|
|
38
|
+
* composables: false // Disable all
|
|
39
|
+
* composables: ['listing', 'page'] // Enable specific ones
|
|
40
|
+
*
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
composables?: FronticComposablesConfig;
|
|
44
|
+
}
|
|
45
|
+
declare const module: NuxtModule<FronticOptions>;
|
|
46
|
+
|
|
47
|
+
export { module as default };
|
|
48
|
+
export type { FronticComposable, FronticComposablesConfig, FronticOptions };
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { existsSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { defineNuxtModule, createResolver, addImportsDir, addComponentsDir, addComponentExports } from '@nuxt/kit';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
// -- Unbuild CommonJS Shims --
|
|
9
|
+
import __cjs_url__ from 'url';
|
|
10
|
+
import __cjs_path__ from 'path';
|
|
11
|
+
import __cjs_mod__ from 'module';
|
|
12
|
+
const __filename = __cjs_url__.fileURLToPath(import.meta.url);
|
|
13
|
+
const __dirname = __cjs_path__.dirname(__filename);
|
|
14
|
+
const require = __cjs_mod__.createRequire(import.meta.url);
|
|
15
|
+
const _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
const IMPLEMENTED_COMPOSABLES = ["listing"];
|
|
17
|
+
function isComposableEnabled(config, name) {
|
|
18
|
+
if (config === true)
|
|
19
|
+
return true;
|
|
20
|
+
if (config === false)
|
|
21
|
+
return false;
|
|
22
|
+
if (Array.isArray(config))
|
|
23
|
+
return config.includes(name);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
const module = defineNuxtModule({
|
|
27
|
+
defaults: {
|
|
28
|
+
prefix: "Ui",
|
|
29
|
+
componentDir: "@/components/ui",
|
|
30
|
+
composables: true
|
|
31
|
+
},
|
|
32
|
+
meta: {
|
|
33
|
+
name: "frontic",
|
|
34
|
+
configKey: "frontic",
|
|
35
|
+
version: "0.0.1",
|
|
36
|
+
compatibility: {
|
|
37
|
+
nuxt: ">=3.17.0"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
async setup(options, nuxt) {
|
|
41
|
+
const { componentDir, prefix, composables: composablesConfig = true } = options;
|
|
42
|
+
const COMPONENT_DIR_PATH = componentDir;
|
|
43
|
+
const ROOT_DIR_PATH = nuxt.options.rootDir;
|
|
44
|
+
const { resolve, resolvePath } = createResolver(ROOT_DIR_PATH);
|
|
45
|
+
const { resolve: resolveModule } = createResolver(_dirname);
|
|
46
|
+
const runtimeDir = resolveModule("./runtime");
|
|
47
|
+
const fronticClientDir = join(ROOT_DIR_PATH, ".frontstack");
|
|
48
|
+
const fronticClientPath = join(fronticClientDir, "generated-client.ts");
|
|
49
|
+
const hasFronticClient = existsSync(fronticClientPath);
|
|
50
|
+
const listingEnabled = isComposableEnabled(composablesConfig, "listing");
|
|
51
|
+
const pageEnabled = isComposableEnabled(composablesConfig, "page");
|
|
52
|
+
const blockEnabled = isComposableEnabled(composablesConfig, "block");
|
|
53
|
+
const contextEnabled = isComposableEnabled(composablesConfig, "context");
|
|
54
|
+
const anyImplementedEnabled = IMPLEMENTED_COMPOSABLES.some((c) => isComposableEnabled(composablesConfig, c));
|
|
55
|
+
if (anyImplementedEnabled && !hasFronticClient) {
|
|
56
|
+
const enabledList = IMPLEMENTED_COMPOSABLES.filter((c) => isComposableEnabled(composablesConfig, c)).map((c) => `useFrontic${c.charAt(0).toUpperCase() + c.slice(1)}`);
|
|
57
|
+
console.warn(
|
|
58
|
+
`
|
|
59
|
+
[@frontic/nuxt] Frontic client not found at .frontstack/generated-client.ts
|
|
60
|
+
The following composables require the Frontic client: ${enabledList.join(", ")}
|
|
61
|
+
Run \`npx @frontic/cli generate\` to generate the client, or disable composables:
|
|
62
|
+
frontic: { composables: false }
|
|
63
|
+
`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
nuxt.hook("prepare:types", ({ tsConfig }) => {
|
|
67
|
+
tsConfig.compilerOptions ||= {};
|
|
68
|
+
tsConfig.compilerOptions.paths ||= {};
|
|
69
|
+
tsConfig.compilerOptions.paths["@frontstack/*"] = [".frontstack/*"];
|
|
70
|
+
});
|
|
71
|
+
if (anyImplementedEnabled) {
|
|
72
|
+
addImportsDir(resolve(runtimeDir, "composables"));
|
|
73
|
+
addImportsDir(resolve(runtimeDir, "utils"));
|
|
74
|
+
}
|
|
75
|
+
nuxt.options.runtimeConfig.public.frontic = {
|
|
76
|
+
hasFronticClient,
|
|
77
|
+
composables: {
|
|
78
|
+
listing: listingEnabled,
|
|
79
|
+
page: pageEnabled,
|
|
80
|
+
block: blockEnabled,
|
|
81
|
+
context: contextEnabled
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const componentsPath = await resolvePath(COMPONENT_DIR_PATH);
|
|
85
|
+
addComponentsDir(
|
|
86
|
+
{
|
|
87
|
+
path: componentsPath,
|
|
88
|
+
extensions: [],
|
|
89
|
+
ignore: ["**/*"]
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
prepend: true
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
try {
|
|
96
|
+
await Promise.all(
|
|
97
|
+
readdirSync(componentsPath).map(async (dir) => {
|
|
98
|
+
try {
|
|
99
|
+
const filePath = await resolvePath(join(COMPONENT_DIR_PATH, dir, "index"), {
|
|
100
|
+
extensions: [".ts", ".js"]
|
|
101
|
+
});
|
|
102
|
+
addComponentExports({
|
|
103
|
+
prefix,
|
|
104
|
+
filePath: resolve(filePath),
|
|
105
|
+
priority: 1
|
|
106
|
+
});
|
|
107
|
+
} catch (err) {
|
|
108
|
+
if (err instanceof Error)
|
|
109
|
+
console.warn("Module error: ", err.message);
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
if (err instanceof Error)
|
|
115
|
+
console.warn(err.message);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
export { module as default };
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
function unwrapRef(ref) {
|
|
2
|
+
return typeof ref === "object" && ref !== null && "value" in ref ? ref.value : ref;
|
|
3
|
+
}
|
|
4
|
+
export const LISTING_QUERY_KEYS = {
|
|
5
|
+
root: ["listings"],
|
|
6
|
+
listing: (listingName) => [...LISTING_QUERY_KEYS.root, listingName],
|
|
7
|
+
detail: (listingName, parameters, query) => [
|
|
8
|
+
...LISTING_QUERY_KEYS.listing(listingName),
|
|
9
|
+
JSON.stringify(parameters),
|
|
10
|
+
JSON.stringify(query)
|
|
11
|
+
]
|
|
12
|
+
};
|
|
13
|
+
export function fetchListingQuery(config) {
|
|
14
|
+
return {
|
|
15
|
+
key: () => {
|
|
16
|
+
const params = unwrapRef(config.parameters);
|
|
17
|
+
const query = unwrapRef(config.query);
|
|
18
|
+
const page = unwrapRef(config.page);
|
|
19
|
+
const queryWithPage = page ? { ...query, page } : query;
|
|
20
|
+
return LISTING_QUERY_KEYS.detail(config.listingName, params, queryWithPage);
|
|
21
|
+
},
|
|
22
|
+
query: async () => {
|
|
23
|
+
const params = unwrapRef(config.parameters);
|
|
24
|
+
const query = unwrapRef(config.query);
|
|
25
|
+
const contextKey = unwrapRef(config.contextKey);
|
|
26
|
+
const page = unwrapRef(config.page);
|
|
27
|
+
const queryWithPage = page ? { ...query, page } : query;
|
|
28
|
+
try {
|
|
29
|
+
const { default: client } = await import("@frontstack/generated-client");
|
|
30
|
+
return await client.listing(config.listingName, params, {
|
|
31
|
+
query: queryWithPage,
|
|
32
|
+
contextKey
|
|
33
|
+
});
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error(`[@frontic/nuxt] Failed to fetch listing "${config.listingName}". Make sure the Frontic client is generated.`);
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
staleTime: config.staleTime ?? 1e3 * 60 * 5
|
|
40
|
+
// 5 minutes default
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export function createCacheKey(keyArray) {
|
|
44
|
+
return keyArray.map((part) => typeof part === "object" ? JSON.stringify(part) : String(part)).join("-");
|
|
45
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { useQuery, useMutation, useQueryCache } from "@pinia/colada";
|
|
2
|
+
import { useDebounceFn } from "@vueuse/core";
|
|
3
|
+
import { makeLogicalFilter, manageFilter, removeFilter } from "../utils/filter-helpers.js";
|
|
4
|
+
import { makeFilterState, processAvailableFilters, processAvailableSorts, applyFilterConfig, countActiveFilters, parseSortString, isSortAllowed } from "../utils/state-helpers.js";
|
|
5
|
+
import { fetchListingQuery } from "./listings.js";
|
|
6
|
+
export function useFronticListing(config) {
|
|
7
|
+
const queryCache = useQueryCache();
|
|
8
|
+
const {
|
|
9
|
+
name: listingName,
|
|
10
|
+
params = {},
|
|
11
|
+
cacheKey = listingName,
|
|
12
|
+
contextKey,
|
|
13
|
+
searchTermThreshold = 2,
|
|
14
|
+
orFilterKeys = [],
|
|
15
|
+
filters,
|
|
16
|
+
sorts,
|
|
17
|
+
enableSearch = true,
|
|
18
|
+
enableFilters = true,
|
|
19
|
+
enableSort = true,
|
|
20
|
+
staleTime = 1e3 * 60 * 5
|
|
21
|
+
// 5 minutes
|
|
22
|
+
} = config;
|
|
23
|
+
const filterConfig = filters || {};
|
|
24
|
+
const sortConfig = sorts || {};
|
|
25
|
+
const searchTerm = useState(`${cacheKey}-searchTerm`, () => "");
|
|
26
|
+
const _orFilterList = useState(`${cacheKey}-orFilterList`, () => []);
|
|
27
|
+
const _andFilterList = useState(`${cacheKey}-andFilterList`, () => []);
|
|
28
|
+
const _sortQuery = useState(`${cacheKey}-sortQuery`, () => void 0);
|
|
29
|
+
const listingState = useState(`${cacheKey}-listingState`, () => ({
|
|
30
|
+
sort: void 0,
|
|
31
|
+
search: void 0,
|
|
32
|
+
filter: void 0
|
|
33
|
+
}));
|
|
34
|
+
const apiQuery = computed(() => ({
|
|
35
|
+
sort: enableSort ? _sortQuery.value : void 0,
|
|
36
|
+
search: enableSearch && searchTerm.value && searchTerm.value.trim().length > searchTermThreshold ? searchTerm.value : void 0,
|
|
37
|
+
filter: enableFilters ? [...makeLogicalFilter("or", _orFilterList.value), ...makeLogicalFilter("and", _andFilterList.value)] : void 0
|
|
38
|
+
}));
|
|
39
|
+
const { data, status, refresh } = useQuery(
|
|
40
|
+
fetchListingQuery({
|
|
41
|
+
listingName,
|
|
42
|
+
parameters: params,
|
|
43
|
+
query: apiQuery,
|
|
44
|
+
contextKey,
|
|
45
|
+
staleTime
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
const { mutateAsync: loadMore } = useMutation({
|
|
49
|
+
mutation: async () => {
|
|
50
|
+
const nextPage = data.value?.page;
|
|
51
|
+
if (!nextPage?.next) return void 0;
|
|
52
|
+
const nextPageQuery = fetchListingQuery({
|
|
53
|
+
listingName,
|
|
54
|
+
parameters: params,
|
|
55
|
+
query: apiQuery,
|
|
56
|
+
contextKey,
|
|
57
|
+
page: ref(nextPage.next),
|
|
58
|
+
staleTime
|
|
59
|
+
});
|
|
60
|
+
return await nextPageQuery.query();
|
|
61
|
+
},
|
|
62
|
+
onSuccess: (data2) => {
|
|
63
|
+
const newData = data2;
|
|
64
|
+
const newItems = newData?.items || [];
|
|
65
|
+
if (!newItems.length) return;
|
|
66
|
+
const cacheKeyValue = [cacheKey, JSON.stringify(apiQuery.value)];
|
|
67
|
+
const oldData = queryCache.getQueryData(cacheKeyValue);
|
|
68
|
+
queryCache.setQueryData(cacheKeyValue, {
|
|
69
|
+
...newData,
|
|
70
|
+
items: [...oldData?.items || [], ...newItems]
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
if (enableSearch) {
|
|
75
|
+
watch(searchTerm, (newTerm) => {
|
|
76
|
+
if (newTerm.trim().length > searchTermThreshold || listingState.value.search) {
|
|
77
|
+
triggerSearchFromInput();
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
watch(
|
|
82
|
+
data,
|
|
83
|
+
(newData) => {
|
|
84
|
+
if (newData) {
|
|
85
|
+
updateState(newData);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
{ immediate: true }
|
|
89
|
+
);
|
|
90
|
+
function updateState(response) {
|
|
91
|
+
const rawFilters = response.filter;
|
|
92
|
+
let uiFilters = makeFilterState(rawFilters);
|
|
93
|
+
uiFilters = applyFilterConfig(uiFilters, filterConfig);
|
|
94
|
+
const availableFilters = processAvailableFilters(rawFilters, filterConfig);
|
|
95
|
+
const availableSorts = processAvailableSorts(sortConfig);
|
|
96
|
+
const activeFilterCount = countActiveFilters(uiFilters);
|
|
97
|
+
const pageData = response.page;
|
|
98
|
+
listingState.value = {
|
|
99
|
+
page: pageData?.current,
|
|
100
|
+
sort: _sortQuery.value,
|
|
101
|
+
search: apiQuery.value.search,
|
|
102
|
+
filter: uiFilters,
|
|
103
|
+
availableFilters,
|
|
104
|
+
availableSorts,
|
|
105
|
+
activeFilterCount,
|
|
106
|
+
total: response.total ?? 0
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
async function refreshListing() {
|
|
110
|
+
await refresh();
|
|
111
|
+
if (data.value) {
|
|
112
|
+
updateState(data.value);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const triggerSearchFromInput = useDebounceFn(refreshListing, 200);
|
|
116
|
+
async function paginate(direction) {
|
|
117
|
+
const pageData = data.value?.page;
|
|
118
|
+
const pageNumber = direction === "next" ? pageData?.next : pageData?.prev;
|
|
119
|
+
if (!pageNumber) return false;
|
|
120
|
+
listingState.value.page = pageNumber;
|
|
121
|
+
const pageQuery = fetchListingQuery({
|
|
122
|
+
listingName,
|
|
123
|
+
parameters: params,
|
|
124
|
+
query: apiQuery,
|
|
125
|
+
contextKey,
|
|
126
|
+
page: ref(pageNumber),
|
|
127
|
+
staleTime
|
|
128
|
+
});
|
|
129
|
+
const response = await pageQuery.query();
|
|
130
|
+
if (response) {
|
|
131
|
+
updateState(response);
|
|
132
|
+
}
|
|
133
|
+
return !!response;
|
|
134
|
+
}
|
|
135
|
+
async function sortItems(sortBy) {
|
|
136
|
+
if (!enableSort) return;
|
|
137
|
+
const sortValue = parseSortString(sortBy);
|
|
138
|
+
if (!sortValue) {
|
|
139
|
+
_sortQuery.value = void 0;
|
|
140
|
+
} else {
|
|
141
|
+
if (!isSortAllowed(sortBy, sortConfig)) return;
|
|
142
|
+
_sortQuery.value = sortValue;
|
|
143
|
+
}
|
|
144
|
+
await refreshListing();
|
|
145
|
+
}
|
|
146
|
+
async function resetSort() {
|
|
147
|
+
if (!enableSort) return;
|
|
148
|
+
_sortQuery.value = void 0;
|
|
149
|
+
await refreshListing();
|
|
150
|
+
}
|
|
151
|
+
async function filterItems(filterField, filterOptions) {
|
|
152
|
+
if (!enableFilters) return;
|
|
153
|
+
const shouldUseOr = orFilterKeys.includes(filterField);
|
|
154
|
+
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
155
|
+
targetList.value = manageFilter(targetList.value, filterField, filterOptions);
|
|
156
|
+
await refreshListing();
|
|
157
|
+
}
|
|
158
|
+
async function addFilterOption(filterField, filterOption) {
|
|
159
|
+
if (!enableFilters) return;
|
|
160
|
+
const shouldUseOr = orFilterKeys.includes(filterField);
|
|
161
|
+
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
162
|
+
const existingFilter = targetList.value.find((filter) => filter.field === filterField);
|
|
163
|
+
const currentValues = existingFilter ? Array.isArray(existingFilter.value) ? existingFilter.value : [existingFilter.value] : [];
|
|
164
|
+
targetList.value = manageFilter(targetList.value, filterField, [...currentValues, filterOption]);
|
|
165
|
+
await refreshListing();
|
|
166
|
+
}
|
|
167
|
+
async function removeFilterOption(filterField, filterOption) {
|
|
168
|
+
if (!enableFilters) return;
|
|
169
|
+
const shouldUseOr = orFilterKeys.includes(filterField);
|
|
170
|
+
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
171
|
+
targetList.value = removeFilter(targetList.value, filterField, filterOption);
|
|
172
|
+
await refreshListing();
|
|
173
|
+
}
|
|
174
|
+
async function resetFilter(filterField) {
|
|
175
|
+
if (!enableFilters) return;
|
|
176
|
+
if (!filterField) {
|
|
177
|
+
_orFilterList.value = [];
|
|
178
|
+
_andFilterList.value = [];
|
|
179
|
+
} else {
|
|
180
|
+
const shouldUseOr = orFilterKeys.includes(filterField);
|
|
181
|
+
const targetList = shouldUseOr ? _orFilterList : _andFilterList;
|
|
182
|
+
targetList.value = removeFilter(targetList.value, filterField);
|
|
183
|
+
}
|
|
184
|
+
await refreshListing();
|
|
185
|
+
}
|
|
186
|
+
async function resetSearch() {
|
|
187
|
+
searchTerm.value = "";
|
|
188
|
+
await refreshListing();
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
// State
|
|
192
|
+
listing: data,
|
|
193
|
+
status,
|
|
194
|
+
state: listingState,
|
|
195
|
+
searchTerm,
|
|
196
|
+
// Actions
|
|
197
|
+
loadMore: () => loadMore(),
|
|
198
|
+
refresh: refreshListing,
|
|
199
|
+
paginate,
|
|
200
|
+
sortItems,
|
|
201
|
+
resetSort,
|
|
202
|
+
filterItems,
|
|
203
|
+
addFilter: addFilterOption,
|
|
204
|
+
removeFilter: removeFilterOption,
|
|
205
|
+
resetFilter,
|
|
206
|
+
resetSearch
|
|
207
|
+
};
|
|
208
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export function makeEqualsFilter(field, value) {
|
|
2
|
+
return {
|
|
3
|
+
type: "equals",
|
|
4
|
+
field,
|
|
5
|
+
value
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export function makeLogicalFilter(type, filters) {
|
|
9
|
+
return filters.length > 0 ? [
|
|
10
|
+
{
|
|
11
|
+
type,
|
|
12
|
+
filter: filters
|
|
13
|
+
}
|
|
14
|
+
] : [];
|
|
15
|
+
}
|
|
16
|
+
export function manageFilter(filterList, field, value) {
|
|
17
|
+
const existingIndex = filterList.findIndex((filter) => filter.field === field);
|
|
18
|
+
const newFilter = makeEqualsFilter(field, value);
|
|
19
|
+
if (existingIndex !== -1) {
|
|
20
|
+
const newList = [...filterList];
|
|
21
|
+
newList[existingIndex] = newFilter;
|
|
22
|
+
return newList;
|
|
23
|
+
}
|
|
24
|
+
return [...filterList, newFilter];
|
|
25
|
+
}
|
|
26
|
+
export function removeFilter(filterList, field, option) {
|
|
27
|
+
if (!option) {
|
|
28
|
+
return filterList.filter((filter) => filter.field !== field);
|
|
29
|
+
}
|
|
30
|
+
const existingFilter = filterList.find((filter) => filter.field === field);
|
|
31
|
+
if (!existingFilter) return filterList;
|
|
32
|
+
const currentValues = Array.isArray(existingFilter.value) ? existingFilter.value : [existingFilter.value];
|
|
33
|
+
const newValues = currentValues.filter((value) => value !== option);
|
|
34
|
+
if (newValues.length === 0) {
|
|
35
|
+
return filterList.filter((filter) => filter.field !== field);
|
|
36
|
+
}
|
|
37
|
+
return manageFilter(filterList, field, newValues);
|
|
38
|
+
}
|
|
39
|
+
export function hasFilter(filterList, field) {
|
|
40
|
+
return filterList.some((filter) => filter.field === field);
|
|
41
|
+
}
|
|
42
|
+
export function getFilterValues(filterList, field) {
|
|
43
|
+
const filter = filterList.find((f) => f.field === field);
|
|
44
|
+
if (!filter) return [];
|
|
45
|
+
return Array.isArray(filter.value) ? filter.value : [filter.value];
|
|
46
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { makeEqualsFilter, makeLogicalFilter, manageFilter, removeFilter, hasFilter, getFilterValues } from "./filter-helpers.js";
|
|
2
|
+
export { makeFilterState, processAvailableFilters, processAvailableSorts, applyFilterConfig, countActiveFilters, parseSortString, isSortAllowed } from "./state-helpers.js";
|
|
File without changes
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export function makeFilterState(filters) {
|
|
2
|
+
const result = {};
|
|
3
|
+
if (!filters) return result;
|
|
4
|
+
for (const key in filters) {
|
|
5
|
+
if (Object.hasOwn(filters, key)) {
|
|
6
|
+
const selectedItems = filters[key]?.filter((item) => item.selected) || [];
|
|
7
|
+
if (selectedItems.length > 0) {
|
|
8
|
+
result[key] = selectedItems;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
export function processAvailableFilters(rawFilters, config = {}) {
|
|
15
|
+
if (!rawFilters) return [];
|
|
16
|
+
const { keys = [], mode = "include", labels = {} } = config;
|
|
17
|
+
const availableFilters = [];
|
|
18
|
+
const responseFilterKeys = Object.keys(rawFilters);
|
|
19
|
+
for (const key of responseFilterKeys) {
|
|
20
|
+
const isInKeys = keys.includes(key);
|
|
21
|
+
const shouldInclude = keys.length === 0 || (mode === "include" ? isInKeys : !isInKeys);
|
|
22
|
+
if (!shouldInclude) continue;
|
|
23
|
+
const filterOptions = rawFilters[key] || [];
|
|
24
|
+
if (filterOptions.length === 0) continue;
|
|
25
|
+
const label = labels[key] || key;
|
|
26
|
+
availableFilters.push({
|
|
27
|
+
key,
|
|
28
|
+
label,
|
|
29
|
+
options: filterOptions
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return availableFilters;
|
|
33
|
+
}
|
|
34
|
+
export function processAvailableSorts(config = {}) {
|
|
35
|
+
const { keys = [], mode = "include", labels = {} } = config;
|
|
36
|
+
return Object.entries(labels).filter(([value]) => {
|
|
37
|
+
if (keys.length === 0) return true;
|
|
38
|
+
const isInKeys = keys.includes(value);
|
|
39
|
+
return mode === "include" ? isInKeys : !isInKeys;
|
|
40
|
+
}).map(
|
|
41
|
+
([value, label]) => ({
|
|
42
|
+
key: value,
|
|
43
|
+
label,
|
|
44
|
+
value
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
export function applyFilterConfig(filters, config = {}) {
|
|
49
|
+
const { keys = [], mode = "include", labels = {} } = config;
|
|
50
|
+
let result = filters;
|
|
51
|
+
if (keys.length > 0) {
|
|
52
|
+
result = Object.keys(filters).reduce(
|
|
53
|
+
(acc, key) => {
|
|
54
|
+
const isInKeys = keys.includes(key);
|
|
55
|
+
const shouldInclude = mode === "include" ? isInKeys : !isInKeys;
|
|
56
|
+
if (shouldInclude && filters[key]) {
|
|
57
|
+
acc[key] = filters[key];
|
|
58
|
+
}
|
|
59
|
+
return acc;
|
|
60
|
+
},
|
|
61
|
+
{}
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
if (Object.keys(labels).length > 0) {
|
|
65
|
+
const renamedFilters = {};
|
|
66
|
+
Object.entries(result).forEach(([key, value]) => {
|
|
67
|
+
const newKey = labels[key] || key;
|
|
68
|
+
renamedFilters[newKey] = value;
|
|
69
|
+
});
|
|
70
|
+
result = renamedFilters;
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
export function countActiveFilters(filters) {
|
|
75
|
+
if (!filters) return 0;
|
|
76
|
+
return Object.keys(filters).length;
|
|
77
|
+
}
|
|
78
|
+
export function parseSortString(sortString) {
|
|
79
|
+
if (sortString === "default") return void 0;
|
|
80
|
+
const [field, order] = sortString.split(":");
|
|
81
|
+
return { field, order };
|
|
82
|
+
}
|
|
83
|
+
export function isSortAllowed(sortString, config = {}) {
|
|
84
|
+
const { keys = [], mode = "include" } = config;
|
|
85
|
+
if (keys.length === 0) return true;
|
|
86
|
+
const isInKeys = keys.includes(sortString);
|
|
87
|
+
return mode === "include" ? isInKeys : !isInKeys;
|
|
88
|
+
}
|
package/dist/types.d.mts
ADDED
package/dist/types.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@frontic/nuxt",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Nuxt module for auto-importing UI components - inspired by shadcn-vue",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Frontic",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/frontic/frontic-nuxt.git",
|
|
10
|
+
"directory": "packages/nuxt"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/frontic/frontic-nuxt#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/frontic/frontic-nuxt/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"nuxt",
|
|
18
|
+
"nuxt-module",
|
|
19
|
+
"frontic",
|
|
20
|
+
"frontstack",
|
|
21
|
+
"vue",
|
|
22
|
+
"components"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"import": "./dist/module.mjs",
|
|
28
|
+
"require": "./dist/module.cjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"main": "./dist/module.cjs",
|
|
32
|
+
"types": "./dist/types.d.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"prepack": "nuxt-module-build build",
|
|
38
|
+
"dev": "nuxi dev playground",
|
|
39
|
+
"dev:build": "nuxi build playground",
|
|
40
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
41
|
+
"build": "nuxt-module-build build",
|
|
42
|
+
"typecheck": "vue-tsc --noEmit",
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"test:watch": "vitest watch",
|
|
45
|
+
"clean": "rm -rf dist .nuxt"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@nuxt/kit": "^3.17.0",
|
|
49
|
+
"@pinia/colada": "^0.14.0",
|
|
50
|
+
"@vueuse/core": "^12.0.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@nuxt/module-builder": "^0.8.0",
|
|
54
|
+
"@nuxt/schema": "^3.17.0",
|
|
55
|
+
"nuxt": "^3.17.0",
|
|
56
|
+
"vitest": "^2.x",
|
|
57
|
+
"vue": "^3.x",
|
|
58
|
+
"vue-tsc": "^2.x"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"nuxt": ">=3.17.0"
|
|
62
|
+
}
|
|
63
|
+
}
|