slate/node_common/managers/slate.js

23 lines
549 B
JavaScript
Raw Normal View History

2020-10-06 01:33:37 +03:00
import * as Data from "~/node_common/data";
import * as Strings from "~/common/strings";
import * as Numbers from "~/common/numbers";
2020-10-06 01:33:37 +03:00
export const getRandomSlateElementURL = async ({ id, fallback = "" }) => {
if (Strings.isEmpty(id)) {
return fallback;
2020-10-06 01:33:37 +03:00
}
const query = await Data.getSlateById({ id });
if (!query || query.error) {
return fallback;
2020-10-06 01:33:37 +03:00
}
if (!query.data.objects.length) {
return fallback;
}
2020-10-06 02:44:05 +03:00
const index = Numbers.getRandomInt(0, query.data.objects.length - 1);
return query.data.objects[index].url;
2020-10-06 01:33:37 +03:00
};