Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Vetrivel-VP/fd55c2496eefd4164bea4643cfe44e90 to your computer and use it in GitHub Desktop.
Save Vetrivel-VP/fd55c2496eefd4164bea4643cfe44e90 to your computer and use it in GitHub Desktop.
Sanity Codes
Support Codes
import {
FaCamera,
FaFire,
FaHome,
FaPaintBrush,
FaVideo,
} from "react-icons/fa";
import { v4 as uuidv4 } from "uuid";
export const subMenu = [
{ id: uuidv4(), name: "My media", slug: "my-media" },
{ id: uuidv4(), name: "Upload", slug: "upload" },
{ id: uuidv4(), name: "Statistics", slug: "statistics" },
{ id: uuidv4(), name: "Collections", slug: "collections" },
{ id: uuidv4(), name: "Following", slug: "following" },
{ id: uuidv4(), name: "Messages", slug: "messages" },
];
export const mainMenu = [
{ id: uuidv4(), name: "My media", slug: "my-media" },
{ id: uuidv4(), name: "Upload", slug: "upload" },
{ id: uuidv4(), name: "Statistics", slug: "statistics" },
{ id: uuidv4(), name: "Collections", slug: "collections" },
{ id: uuidv4(), name: "Profile", slug: "profile" },
];
export const filerMenu = [
{ id: uuidv4(), to: "/", label: "Home", icon: FaHome },
{ id: uuidv4(), to: "/search/photos", label: "Photos", icon: FaCamera },
{
id: uuidv4(),
to: "/search/illustration",
label: "Illustration",
icon: FaPaintBrush,
},
{ id: uuidv4(), to: "/search/videos", label: "videos", icon: FaVideo },
{ id: uuidv4(), to: "/search/gifs", label: "Gifs", icon: FaFire },
];
export const categoriesList = [
{ id: uuidv4(), name: "nature" },
{ id: uuidv4(), name: "photos" },
{ id: uuidv4(), name: "illustration" },
{ id: uuidv4(), name: "musics" },
{ id: uuidv4(), name: "videos" },
{ id: uuidv4(), name: "gifs" },
{ id: uuidv4(), name: "anime" },
{ id: uuidv4(), name: "background" },
{ id: uuidv4(), name: "sky" },
{ id: uuidv4(), name: "money" },
{ id: uuidv4(), name: "water" },
{ id: uuidv4(), name: "cat" },
{ id: uuidv4(), name: "baby" },
{ id: uuidv4(), name: "dog" },
{ id: uuidv4(), name: "food" },
{ id: uuidv4(), name: "car" },
{ id: uuidv4(), name: "flower" },
{ id: uuidv4(), name: "artifacts" },
{ id: uuidv4(), name: "4k Wallpaper" },
{ id: uuidv4(), name: "wallpaper" },
];
export const fetchQuery = `*[_type == 'post'] | order(_createAt desc){
_id,
title,
categories,
otherMedia{
asset -> {
url
}
},
mainImage {
asset -> {
url
}
},
keywords,
description,
_createdAt,
users->{
_id,
displayName,
photoURL
},
collections[]->{
_id,
displayName,
photoURL
},
comments[]->{
_id,
comment,
_createdAt,
users->{
_id,
displayName,
photoURL
}
}
}`;
export const fetchDetailQuery = (feedID) => {
const query = `*[_type == 'post' && _id == '${feedID}']{
_id,
title,
categories,
otherMedia{
asset -> {
url
}
},
mainImage {
asset -> {
url
}
},
keywords,
description,
_createdAt,
users->{
_id,
displayName,
photoURL
},
collections[]->{
_id,
displayName,
photoURL
},
comments[]->{
_id,
comment,
_createdAt,
users->{
_id,
displayName,
photoURL
}
}
}`;
return query;
};
export const searchQuery = (searchTerm) => {
const query = `*[_type == 'post' && title match '${searchTerm}*'
|| categories match '${searchTerm}*'
|| keywords match '${searchTerm}*']{
_id,
title,
categories,
otherMedia{
asset -> {
url
}
},
mainImage {
asset -> {
url
}
},
keywords,
description,
_createdAt,
users->{
_id,
displayName,
photoURL
},
collections[]->{
_id,
displayName,
photoURL
},
comments[]->{
_id,
comment,
_createdAt,
users->{
_id,
displayName,
photoURL
}
}
}`;
return query;
};
Sanity Client Upload Codes
import { createClient } from "@sanity/client";
import imageUrlBuilder from "@sanity/image-url";
import { fetchDetailQuery, fetchQuery, searchQuery } from "./utils/supports";
import { v4 as uuidv4 } from "uuid";
const client = createClient({
projectId: "lwrnrbkc",
dataset: "production",
// useCdn: true,
apiVersion: "2023-04-02",
token: process.env.REACT_APP_SANITY_TOKEN,
});
const builder = imageUrlBuilder(client);
export const urlFor = (source) => builder.image(source);
export const createNewUser = async (data) => {
const doc = {
_id: data.uid,
_type: "users",
uid: data.uid,
displayName: data.displayName,
email: data.email,
phoneNumber: data.phoneNumber,
photoURL: data.photoURL,
};
await client.createIfNotExists(doc).then((res) => {
return res;
});
};
export const uploadAsset = async (asset) => {
let data;
if (
["image/jpeg", "image/jpg", "image/png", "image/gif"].includes(asset.type)
) {
console.log("Image:", asset.type);
data = await client.assets.upload("image", asset, {
contentType: asset.type,
filename: asset.name,
});
return data;
} else {
console.log("Files:", asset.type);
data = await client.assets.upload("file", asset, {
contentType: asset.type,
filename: asset.name,
});
return data;
}
};
export const deleteUploadedAsset = async (id) => {
let data = await client.delete(id);
return data;
};
export const savePost = async (doc) => {
await client.create(doc).then((res) => {
console.log(res);
return res;
});
};
export const fetchFeeds = () => {
let data = client.fetch(fetchQuery);
return data;
};
export const deleteFeed = async (id) => {
let data = await client.delete(id);
return data;
};
export const addToCollection = async (id, uid) => {
await client
.patch(id)
.setIfMissing({ collections: [] })
.insert("after", "collections[-1]", [
{ _key: uuidv4(), _type: "reference", _ref: uid },
])
.commit();
};
export const addToComments = async (id, uid, comment) => {
const doc = {
_type: "comments",
comment,
users: {
_type: "reference",
_ref: uid,
},
};
await client.create(doc).then((com) => {
client
.patch(id)
.setIfMissing({ comments: [] })
.insert("after", "comments[-1]", [
{
_key: uuidv4(),
_type: "reference",
_ref: com._id,
},
])
.commit()
.then((res) => {
console.log(res);
});
});
};
export const fetchFeedDetail = async (feedID) => {
let query = fetchDetailQuery(feedID);
if (query) {
let data = await client.fetch(query);
return data;
}
};
export const fetchSearchQuery = async (searchTerm) => {
let query = searchQuery(searchTerm);
if (query) {
let data = await client.fetch(query);
return data;
}
};
// export const deleteComment = async (id, uid, commentId) => {
// await client
// .patch(id)
// .set({
// comments: {
// _type: "array",
// remove: {
// _ref: uid,
// },
// },
// })
// .commit()
// .then(() => {});
// };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment