Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom URL generator #46

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState } from 'react'
import { WindowSize } from '@reach/window-size'
import { ThemeProvider } from 'styled-components'
// import { get } from './api/config'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment necessary?

import { BrowserRouter, Routes, Route } from 'react-router-dom'
import { FullScreen, useFullScreenHandle } from 'react-full-screen'
import GlobalStyle, { theme } from './components/ui/GlobalStyle'
Expand All @@ -9,25 +10,26 @@ import LifelineCreation from './pages/lifelineCreation'
import EnterFullScreen from './components/buttons/EnterFullscreen'
import ExitFullScreen from './components/buttons/ExitFullscreen'
import Home from './pages/Home'

function App() {
const [showFullscreenButton, setFullscreenButton] = useState(false)
/* Sets the lifeline modules upon load and every defaultLanguage change */
const handle = useFullScreenHandle()

return (
<ThemeProvider theme={theme}>
<FullScreen
handle={handle}
onChange={() => setFullscreenButton(!showFullscreenButton)}
>
{' '}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also the space, i just may not be familiar with the syntax though

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets added by the linter sometimes but it should be removed

<BrowserRouter>
<Routes>
<Route path="/settings" element={<LanguageCustomization />} />
<Route path="/lifelines" element={<LifelineCreation />} />
<Route path="/" element={<Home />} />
<Route path="/langForm/*" element={<LanguageCustomization />} />
<Route path="/moduleForm/*" element={<LifelineCreation />} />
<Route path="/settings/*" element={<LanguageCustomization />} />
<Route path="/lifelines/*" element={<LifelineCreation />} />
<Route path="/*" element={<Home />} />
</Routes>
</BrowserRouter>

{showFullscreenButton ? (
<EnterFullScreen handle={handle.enter} />
) : (
Expand Down
28 changes: 26 additions & 2 deletions client/src/components/settings/LanguageCustomizationForm.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import { useState } from 'react'
import { LANGUAGE_LOCAL_STORAGE_KEY } from '../../utils/constants'
import { useState, useEffect } from 'react'
import {
COMPRESSED_KEY,
LANGUAGE_LOCAL_STORAGE_KEY,
LIFELINES_LOCAL_STORAGE_KEY,
} from '../../utils/constants'
import { compressToEncodedURIComponent } from 'lz-string'
import { useNavigate } from 'react-router-dom'
import { UpdateURL } from '../../routing/UpdateURL'
import { UpdateSettings } from '../../routing/UpdateSettings'

const LanguageCustomization = () => {
const navigate = useNavigate()
const [selectedLanguage, setSelectedLanguage] = useState<string>('')

useEffect(() => {
UpdateURL(navigate, selectedLanguage, null)
UpdateSettings(selectedLanguage, null)
}, [navigate, selectedLanguage])

const formSubmit = (e: any) => {
e.preventDefault()
if (selectedLanguage !== localStorage.getItem(LANGUAGE_LOCAL_STORAGE_KEY)) {
let json = {
language: selectedLanguage,
lifelines: localStorage.getItem(LIFELINES_LOCAL_STORAGE_KEY),
}
const settings_json = JSON.stringify(json)
let compressed = compressToEncodedURIComponent(settings_json)
navigate(`${compressed}`)
localStorage.setItem(COMPRESSED_KEY, compressed)
}
localStorage.setItem(LANGUAGE_LOCAL_STORAGE_KEY, selectedLanguage)
}

Expand Down
24 changes: 22 additions & 2 deletions client/src/components/settings/LifelineCreationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import {
URL,
ERROR_MSG,
LIFELINES_LOCAL_STORAGE_KEY,
LANGUAGE_LOCAL_STORAGE_KEY,
COMPRESSED_KEY,
} from '../../utils/constants'
import { compressToEncodedURIComponent } from 'lz-string'
import { useNavigate } from 'react-router-dom'
import { ModuleResInterface } from '../../interfaces'
import { getData } from '../../utils/utils'
import DraggableLifelines from '../DraggableLifelines'

import { UpdateURL } from '../../routing/UpdateURL'
import { UpdateSettings } from '../../routing/UpdateSettings'
const LifelineCreationForm = () => {
/* Lifeline module properties */
const flavor = 'Lifeline'
Expand All @@ -22,6 +27,7 @@ const LifelineCreationForm = () => {
const [lifelineModules, setLifelineModules] = useState<ModuleResInterface[]>(
[],
)
const navigate = useNavigate()

useEffect(() => {
getData(
Expand All @@ -32,7 +38,9 @@ const LifelineCreationForm = () => {
setModules,
setLifelineModules,
)
}, [])
UpdateURL(navigate, null, lifelineModules)
UpdateSettings(null, lifelineModules)
}, [navigate, lifelineModules])

/* clearProperties
*
Expand All @@ -52,6 +60,18 @@ const LifelineCreationForm = () => {
*/
const formSubmit = (e: any) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend against using the any type since it is both bad practice and the linter will complain about this in the future

e.preventDefault()
/*const language: string | null = localStorage.getItem(
LANGUAGE_LOCAL_STORAGE_KEY,
)*/
let json = {
language: localStorage.getItem(LANGUAGE_LOCAL_STORAGE_KEY),
lifelines: lifelineModules,
}
const settings_json = JSON.stringify(json)
let compressed = compressToEncodedURIComponent(settings_json)
navigate(`${compressed}`)
localStorage.setItem(COMPRESSED_KEY, compressed)

const llModule: ModuleResInterface = {
labels: [title] /* stored as array in API response */,
flavor,
Expand Down
11 changes: 8 additions & 3 deletions client/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { ERROR_MSG, URL, NUM_LIFELINES_DISPLAYED } from '../utils/constants'
import { getData } from '../utils/utils'
import Lifelines from '../components/lifelines/Lifelines'
import { ModuleResInterface, NewsInterface } from '../interfaces/index'

import { UpdateURL } from '../routing/UpdateURL'
import { UpdateSettings } from '../routing/UpdateSettings'
import { useNavigate } from 'react-router-dom'
export default function Home() {
const [defaultLanguage, setDefaultLanguage] = useState<string>('eng')
const [modules, setModules] = useState<ModuleResInterface[]>([])
Expand All @@ -15,7 +17,8 @@ export default function Home() {
)
const [newsfeedModules, setNewsfeedModules] = useState<NewsInterface[]>([])
const [errorFlag, setErrorFlag] = useState<boolean>(false)

const navigate = useNavigate()
//localStorage.getItem(LANGUAGE_LOCAL_STORAGE_KEY),localStorage.getItem(LIFELINES_LOCAL_STORAGE_KEY)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment necessary?

useEffect(() => {
getData(
URL,
Expand All @@ -26,7 +29,9 @@ export default function Home() {
setLifelineModules,
setNewsfeedModules,
)
}, [defaultLanguage])
UpdateURL(navigate, defaultLanguage, lifelineModules)
UpdateSettings(defaultLanguage, lifelineModules)
}, [navigate, defaultLanguage, lifelineModules])

return (
<>
Expand Down
13 changes: 13 additions & 0 deletions client/src/routing/UpdateSettings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { decompressFromEncodedURIComponent } from 'lz-string'
import { COMPRESSED_KEY } from '../utils/constants'
export const UpdateSettings = (selectLanguage, lifelineModules) => {
let decompressed = JSON.parse(
decompressFromEncodedURIComponent(localStorage.getItem(COMPRESSED_KEY)),
)
if (decompressed.language !== selectLanguage) {
selectLanguage = decompressed.language
}
if (lifelineModules != null && decompressed.lifelines !== lifelineModules) {
lifelineModules = decompressed.lifelines
}
}
19 changes: 19 additions & 0 deletions client/src/routing/UpdateURL.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
COMPRESSED_KEY,
LANGUAGE_LOCAL_STORAGE_KEY,
LIFELINES_LOCAL_STORAGE_KEY,
} from '../utils/constants'
export const UpdateURL = (navigate, language, lifelines) => {
if (
language &&
localStorage.getItem(LANGUAGE_LOCAL_STORAGE_KEY) !== language
) {
navigate(`${localStorage.getItem(COMPRESSED_KEY)}`)
}
if (
lifelines &&
localStorage.getItem(LIFELINES_LOCAL_STORAGE_KEY) !== lifelines
) {
navigate(`${localStorage.getItem(COMPRESSED_KEY)}`)
}
}
1 change: 1 addition & 0 deletions client/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export const NUM_LIFELINES_DISPLAYED = 3
export const LIFELINES_LOCAL_STORAGE_KEY: string = 'lifelines'
export const ERROR_MSG: string = 'Error retrieving module data from API...'
export const URL: string = 'https://api.climateclock.world/v1/clock'
export const COMPRESSED_KEY: string = ''
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-react": "^7.28.0",
"lint-staged": "^12.3.3",
"lz-string": "^1.4.4",
"mrm": "2",
"prettier": "^2.5.1",
"react": "^17.0.2",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,11 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

lz-string@^1.4.4:
version "1.4.4"
resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26"
integrity sha1-wNjq82BZ9wV5bh40SBHPTEmNOiY=

make-dir@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
Expand Down