Last active
May 29, 2018 13:28
-
-
Save vojtatranta/8e53010872726a3a4ae1e5174845ec21 to your computer and use it in GitHub Desktop.
This file contains 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 createCheckoutApiModule = ({ | |
getRequestOptions, | |
api // NOTE: metody get, put, post, delete ... | |
}) => { | |
get(url) { // NOTE: asi si budeš vrace promise | |
return new Promise((resolve, reject) => { | |
api.get({ | |
...getRequestOptions(), | |
url, | |
}, (err, result) => { | |
if (err) { | |
reject(err) | |
return | |
} | |
resolve(result) | |
}) | |
}) | |
}, | |
post(url, data) { // NOTE: asi si budeš vrace promise | |
return new Promise((resolve, reject) => { | |
api.post({ | |
...getRequestOptions(), | |
url, | |
data, | |
}, (err, result) => { | |
if (err) { | |
reject(err) | |
return | |
} | |
resolve(result) | |
}) | |
}) | |
}, | |
del(url) { // NOTE: asi si budeš vrace promise | |
return new Promise((resolve, reject) => { | |
api.del({ | |
...getRequestOptions(), | |
url, | |
}, (err, result) => { | |
if (err) { | |
reject(err) | |
return | |
} | |
resolve(result) | |
}) | |
}) | |
}, | |
put(url, data) { // NOTE: asi si budeš vrace promise | |
return new Promise((resolve, reject) => { | |
api.put({ | |
...getRequestOptions(), | |
url, | |
data, | |
}, data, (err, result) => { | |
if (err) { | |
reject(err) | |
return | |
} | |
resolve(result) | |
}) | |
}) | |
}, | |
} | |
// Použití | |
class KomponentNekdeVApce extends React.Component { | |
static contextTypes = { | |
api: React.PropTypes.object.isRequired, | |
desktopApiRequestOptionsProvider: React.PropTypes.object.isRequired, | |
} | |
static childContextTypes = { | |
checkoutApiModule: : React.PropTypes.object.isRequired, | |
} | |
getChildContext() { | |
checkoutApiModule: createCheckoutApiModule({ | |
api: { | |
get: ({ url, headers }) => request.get({ url, headers }), // NOTE: používám request, kterej máme v apce, nevim, jak to uděla třeba s fetch API, vim, že marketing používá Axios, ten má tady tyhle všechny metody, ten se konfiguruje obdobně: https://github.com/axios/axios | |
post: ({ url, headers, data }) => request.post({ url, headers, data }), | |
put: ({ url, headers, data }) => request.put({ url, headers, data }), | |
del: ({ url, headers }) => request.del({ url, headers, data }), | |
// šlo by to i takle: | |
get: (options) => { // NOTE: že si to prostě naimpolementuješ třeba s globálním fetchem() | |
return fetch(options.url).then((result) => result.json()) | |
}, | |
// nebo takle | |
post: (options) => { // NOTE: že si to prostě naimpolementuješ třeba s globálním fetchem() | |
return this.context.httpRequestFactory.post(otpions.url, options.data) | |
}, | |
} | |
getRequestOptions: () => this.context.desktopApiRequestOptionsProvider.getOptions(), // NOTE: tady se pak dá zavolat cokoliv z našeho kontextu, marketing si tam taky může dát co chce | |
}) | |
} | |
render() { | |
// ten getChildContext pak pošle kontextovej klíč checkoutApiModule dolů k CheckoutComponent, pokud si ho tahá z kontextu | |
return <CheckoutComponent /> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment