You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.7 KiB
133 lines
4.7 KiB
import delve from 'dlv';
|
|
import {ImageLoader, ImageLoaderProps} from "next/image";
|
|
import {isEmpty, isEqual} from "radash";
|
|
|
|
import {environment} from "../environments/environment";
|
|
|
|
export type ImageFormatType = "default" | "thumbnail" | "medium" | "small";
|
|
|
|
export interface PriceRange {
|
|
min: number;
|
|
max: number;
|
|
}
|
|
|
|
export type SortType = 'pas' | 'pde' | 'alp';
|
|
|
|
export interface QueryParam {
|
|
param: string;
|
|
value: PriceRange | string | number[];
|
|
}
|
|
|
|
export const buildQueriesListFromQueryParams = (query): QueryParam[] => {
|
|
const queryMap = ['sort', 'cat', 'page', 'facets', 'filters', 'search'];
|
|
|
|
return queryMap.map((param: string) => {
|
|
if (!isEmpty(query[param])) {
|
|
let value = !isEmpty(query[param]) ? query[param] : null;
|
|
|
|
if (param === 'filters') {
|
|
value = query[param].split(',').map((value: string) => parseInt(value, 10));
|
|
}
|
|
|
|
if (param === 'facets') {
|
|
const values = query[param].split(':');
|
|
value = {min: parseInt(values[0], 10), max: parseInt(values[1], 10)};
|
|
}
|
|
|
|
return ({param, value});
|
|
} else {
|
|
return null;
|
|
}
|
|
}).filter((val) => !isEmpty(val));
|
|
}
|
|
|
|
export const findQueryParamsValue = <T>(queryList: Array<{ param: string, value: T }>, params: string): T | null => {
|
|
const paramGET = queryList.find((val: { param: string, value: T }) => val.param === params);
|
|
return !isEmpty(paramGET) ? paramGET.value : null;
|
|
}
|
|
|
|
export const getShopSortList = (): Array<{ value: SortType, label: string }> => {
|
|
return [
|
|
{value: "pas", label: 'Prix croissant'},
|
|
{value: "pde", label: 'Prix décroissant'},
|
|
{value: "alp", label: 'Ordre alphabetique'},
|
|
];
|
|
}
|
|
|
|
export const buildShopParamsGET = (sort: SortType = 'pas',
|
|
page: number = null,
|
|
range: PriceRange = {
|
|
min: 0,
|
|
max: 0
|
|
},
|
|
cat: string = null,
|
|
filters: number[] = [],
|
|
search: string) => {
|
|
let url = `?sort=${sort}`;
|
|
|
|
if (!isEmpty(range) && !isEqual(range, {min: 0, max: 0})) {
|
|
url += `&facets=${range.min}:${range.max}`;
|
|
}
|
|
if (!isEmpty(filters)) {
|
|
url += `&filters=${filters.join(',')}`;
|
|
}
|
|
if (!isEmpty(cat)) {
|
|
url += `&cat=${cat}`;
|
|
}
|
|
if (!isEmpty(page)) {
|
|
url += `&page=${page}`;
|
|
}
|
|
if (!isEmpty(search)) {
|
|
url += `&search=${search}`;
|
|
}
|
|
|
|
return url;
|
|
}
|
|
|
|
export const contentfulImageLoader: ImageLoader = ({src, width}: ImageLoaderProps) => {
|
|
return `${src}?w=${width}`
|
|
}
|
|
|
|
export const getCategoryUrl = (item): string => {
|
|
const categorySlug = !delve(item, 'attributes', null) ? delve(item, 'category.data.attributes.slug', '') : delve(item, 'attributes.category.data.attributes.slug', '');
|
|
return '/blog/' + categorySlug;
|
|
}
|
|
|
|
export const getPostUrl = (item): string => {
|
|
const categorySlug = !delve(item, 'attributes', null) ? delve(item, 'category.data.attributes.slug', '') : delve(item, 'attributes.category.data.attributes.slug', '');
|
|
const postSlug = !delve(item, 'attributes', null) ? delve(item, 'slug', '') : delve(item, 'attributes.slug', '');
|
|
return '/blog/' + categorySlug + '/' + postSlug;
|
|
}
|
|
|
|
export const getStrapiImage = (item, format: ImageFormatType = 'default') => {
|
|
const image = !delve(item, 'attributes', null) ? delve(item, 'image.data.attributes', {}) : delve(item, 'attributes.image.data.attributes', {});
|
|
switch (format) {
|
|
case "default":
|
|
return environment.strapiUrl + delve(image, "url", "/images/default.png");
|
|
case "medium":
|
|
return environment.strapiUrl + delve(image, "formats.medium.url", "/images/default.png");
|
|
case "small":
|
|
return environment.strapiUrl + delve(image, "formats.small.url", "/images/default.png");
|
|
case "thumbnail":
|
|
return environment.strapiUrl + delve(image, "formats.thumbnail.url", "/images/default.png");
|
|
default:
|
|
return environment.strapiUrl + delve(image, "url", "/images/default.png");
|
|
}
|
|
}
|
|
|
|
export const getStrapiImageSize = (item, format: ImageFormatType = 'default'): [number, number] => {
|
|
const image = delve(item, 'attributes.image.data.attributes', {});
|
|
switch (format) {
|
|
case "default":
|
|
return [delve(image, "width", 100), delve(image, "height", 100)];
|
|
case "medium":
|
|
return [delve(image, "formats.medium.width", 100), delve(image, "formats.medium.height", 100)];
|
|
case "small":
|
|
return [delve(image, "formats.small.width", 100), delve(image, "formats.small.height", 100)];
|
|
case "thumbnail":
|
|
return [delve(image, "formats.thumbnail.width", 100), delve(image, "formats.thumbnail.height", 100)];
|
|
default:
|
|
return [delve(image, "width", 100), delve(image, "height", 100)];
|
|
}
|
|
}
|