Last active
November 21, 2018 21:08
-
-
Save joaofnds/24ca56a87df754ea1ea6852f9e0a3b2e to your computer and use it in GitHub Desktop.
A simple example of how to show dates in user locale
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const SERVER_URL = "https://donamaid.herokuapp.com"; | |
| const fetchServer = async (path, opts = {}) => | |
| (await fetch(`${SERVER_URL}/${path}`, opts)).json(); | |
| const createServerAPI = token => ({ | |
| get: (path, opts = {}) => { | |
| return fetchServer(path, { | |
| method: "GET", | |
| headers: { | |
| Authorization: `Bearer ${token}` | |
| }, | |
| ...opts | |
| }); | |
| } | |
| }); | |
| (async () => { | |
| const { data: { token, account: { id } } } = await fetchServer( | |
| "authentication", | |
| { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ | |
| email: "USER_EMAIL", | |
| password: "USER_PASSWORD" | |
| }) | |
| } | |
| ); | |
| const api = createServerAPI(token); | |
| try { | |
| const { data: { attributes: { language, time_zone } } } = await api.get( | |
| `accounts/${id}` | |
| ); | |
| console.log(new Date().toLocaleString(language, { timeZone: time_zone })); | |
| } catch (e) { | |
| console.log(e); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment