Skip to content

Instantly share code, notes, and snippets.

@allpwrfulroot
allpwrfulroot / geolocation.js
Last active July 31, 2018 09:02
Funky error, async issue or geolocation bug? watchPositionAsync callback EXPLODES (counter goes to hundreds) if getCurrentPosition called?
import React, { Component } from 'react'
import {
ActivityIndicator,
View,
Text,
Alert
} from 'react-native'
import { Permissions, Location, Notifications, Constants } from 'expo'
import { graphql, gql, compose } from 'react-apollo'
import update from 'immutability-helper'
@allpwrfulroot
allpwrfulroot / GetAllTripsQuery.js
Created April 27, 2017 01:50
Problem solve grahpql
const GetAllTripsQuery = gql`
query GetAllTrips($tripargs: TripWhereArgs) {
viewer {
id
user {
id
domicile
abodes
trips(where: $tripargs, orderBy: { field: start, direction: DESC }) {
pageInfo {
var workdays = {
sunday: false,
monday: true,
tuesday: true,
wednesday: true,
thursday: true,
friday: true,
saturday: false
}
@allpwrfulroot
allpwrfulroot / ImgUpload.js
Created June 8, 2017 01:51
Excerpt from screen with image selection, resize, and upload to grahpql
...
async uploadImageAsync(uri) {
let formData = new FormData();
formData.append("query", `
mutation CreateFile($input: CreateFileInput!) {
createFile(input: $input) {
changedFile {
id
name
@allpwrfulroot
allpwrfulroot / next.config.js
Last active December 11, 2019 16:50
Gists for Medium article
const frontmatter = require('remark-frontmatter')
const withMDX = require('@next/mdx')({
extension: /\.mdx?$/,
options: {
remarkPlugins: [frontmatter],
},
})
module.exports = withMDX({
pageExtensions: ['js', 'jsx', 'mdx'],
@allpwrfulroot
allpwrfulroot / projects-list.json
Last active December 12, 2019 00:44
Demo snippet of JSON listing project page metadata
[
{
"name": "nextjs-workshop",
"size": 299,
"title": "nextjs-workshop",
"description": "public workshop for getting started with NextJS",
"createdAt": "2019-10-27",
"type": "github",
"tags": ["React", "NextJS"]
},
@allpwrfulroot
allpwrfulroot / get-meta.js
Last active December 12, 2019 14:24
Demo utility function for directory introspection and data collection
const fs = require('fs')
const { promisify } = require('util')
const YAML = require('yaml')
async function getMDXInfo(content) {
try {
const files = await promisify(fs.readdir)(`./pages/${content}`)
const mdx = await files.filter(f => f.includes('.mdx'))
let list = await Promise.all(
mdx.map(async p => {
@allpwrfulroot
allpwrfulroot / index.js
Created December 11, 2019 19:23
Projects index page listing all project .mdx info
function Projects() {
const [projects, setProjects] = useState([])
const [error, setError] = useState()
useEffect(() => {
async function fetchProjects() {
try {
const result = await fetch(`/api/get-projects`)
const json = await result.json()
setProjects(json.projects)
} catch (err) {
@allpwrfulroot
allpwrfulroot / get-projects.js
Last active December 12, 2019 14:24
API route that invokes the getMDXInfo utility for the 'projects' directory
import { getMDXInfo } from 'utils/get-meta'
export default async (req, res) => {
if (req.method !== 'GET') {
return (res.statusCode = 401)
}
res.setHeader('Content-Type', 'application/json')
try {
const { list } = await getMDXInfo('projects')
res.status(200).json({ projects: list })
@allpwrfulroot
allpwrfulroot / index.js
Last active December 12, 2019 00:50
Moving the data fetch from render (useEffect) to SSR (getInitialProps)
Projects.getInitialProps = async ({ req }) => {
// Prevent unnecessary refetching
if (process.browser) {
const { projects } = window.__NEXT_DATA__.props.pageProps
if (projects && projects.length > 0) {
return { projects }
}
}
try {
// Have the correct API url