From 2de110a15737abb060215f5c5405c91f23b34ba3 Mon Sep 17 00:00:00 2001 From: CamiloGarciaLaRotta Date: Sun, 24 Mar 2019 21:53:05 -0400 Subject: [PATCH 1/2] fix: make Chrome extension functional again - uses staging Auth - requires all fields for job posting --- src/App.re | 43 +++++++++++++++++++++++++------------------ src/Constants.re | 2 +- src/JobApp.re | 9 ++++++--- src/Services.re | 11 +++++++---- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/App.re b/src/App.re index 59abae8..6b54ede 100644 --- a/src/App.re +++ b/src/App.re @@ -1,6 +1,7 @@ type state = { id: option(string), token: option(string), + loading: bool, }; type action = @@ -15,10 +16,10 @@ let make = _children => { reducer: (action, _state) => switch (action) { | DidMount(maybeId, maybeJwt) => - ReasonReact.Update({id: maybeId, token: maybeJwt}) + ReasonReact.Update({id: maybeId, token: maybeJwt, loading: false}) | Login(maybeId, maybeJwt) => ReasonReact.UpdateWithSideEffects( - {id: maybeId, token: maybeJwt}, + {id: maybeId, token: maybeJwt, loading: false}, ( _self => { SyncStorage.refreshId(maybeId); @@ -28,11 +29,11 @@ let make = _children => { ) | Logout => ReasonReact.UpdateWithSideEffects( - {id: None, token: None}, + {id: None, token: None, loading: false}, (_self => SyncStorage.clear()), ) }, - initialState: () => {id: None, token: None}, + initialState: () => {id: None, token: None, loading: true}, didMount: self => { let handleExpiredToken = () => Logout |> self.send; let handleRetrievedId = (maybeId, maybeJwt) => @@ -82,21 +83,27 @@ let make = _children => { render: self =>
( - switch (self.state.token) { - | None => - - Login(maybeId, maybeToken) |> self.send + self.state.loading ? +
: +
+ ( + switch (self.state.token) { + | None => + + Login(maybeId, maybeToken) |> self.send + ) + /> + | Some(token) => + self.send(Logout)) + id=(Belt.Option.getWithDefault(self.state.id, "")) + jwt=token + /> + } ) - /> - | Some(token) => - self.send(Logout)) - id=(Belt.Option.getWithDefault(self.state.id, "")) - jwt=token - /> - } +
)
, }; diff --git a/src/Constants.re b/src/Constants.re index 840524a..cdd14d9 100644 --- a/src/Constants.re +++ b/src/Constants.re @@ -10,7 +10,7 @@ let linkedinCDNURL = "https://static.licdn.com"; let linkedinCompanyNameLowerCase = "linkedin"; -let authUrl = "https://jobhub-authentication.herokuapp.com"; +let authUrl = " https://jobhub-authentication-staging.herokuapp.com"; let jobAppUrl = "https://scrum-gang-job-applications.herokuapp.com"; diff --git a/src/JobApp.re b/src/JobApp.re index ee7926c..3c0c193 100644 --- a/src/JobApp.re +++ b/src/JobApp.re @@ -202,7 +202,7 @@ let make = (~signOutHandler, ~id, ~jwt, _children) => { name="postedDate" placeholder="" value=self.state.postedDate - required=(Js.Boolean.to_js_boolean(false)) + required=(Js.Boolean.to_js_boolean(true)) />
@@ -211,6 +211,7 @@ let make = (~signOutHandler, ~id, ~jwt, _children) => { _type="date" name="deadline" value=self.state.deadline + required=(Js.Boolean.to_js_boolean(true)) onChange=(ev => ev |> Utilities.valueFromEvent |> changeDeadline) />
@@ -219,7 +220,8 @@ let make = (~signOutHandler, ~id, ~jwt, _children) => { valueFromEvent(ev) |> changeStatusValue) /> ("Applied" |> str) @@ -228,7 +230,7 @@ let make = (~signOutHandler, ~id, ~jwt, _children) => { valueFromEvent(ev) |> changeStatusValue) /> ("To apply" |> str) @@ -237,6 +239,7 @@ let make = (~signOutHandler, ~id, ~jwt, _children) => { valueFromEvent(ev) |> changeStatusValue) /> @@ -230,7 +230,7 @@ let make = (~signOutHandler, ~id, ~jwt, _children) => { valueFromEvent(ev) |> changeStatusValue) /> ("To apply" |> str) diff --git a/src/Services.re b/src/Services.re index 1e96752..77a21ce 100644 --- a/src/Services.re +++ b/src/Services.re @@ -1,5 +1,7 @@ type userId = {id: string}; +type postingExistsResponse = {status: bool}; + type authResponse = { iat: int, exp: int, @@ -36,6 +38,8 @@ module Decode = { token: json |> field("token", string), id: json |> field("user", userId), }; + let postingExistsResponse = json : postingExistsResponse => + Json.Decode.{status: json |> field("status", bool)}; let selfResponse = json : selfResponse => Json.Decode.{ id: json |> field("_id", string), @@ -96,7 +100,47 @@ let authenticate = (~email, ~password, ~callback, ~failure, _self) => { |> ignore; }; -let submitApplication = +let checkAlreadyApplied = (~url, ~jwt, ~callback, ~failure) => { + let headers = + Fetch.HeadersInit.make({ + "Content-Type": "application/json", + "Authorization": "Bearer " ++ jwt, + }); + let jobAppURL = Constants.jobAppUrl ++ "/applications/exists?url=" ++ url; + Js.log("HERE " ++ jwt); + Js.log("HERE " ++ url); + Js.Promise.( + Fetch.fetchWithInit( + jobAppURL, + Fetch.RequestInit.make(~method_=Get, ~headers, ~mode=Fetch.CORS, ()), + ) + |> then_(response => Fetch.Response.json(response)) + |> then_(json => + json + |> Decode.postingExistsResponse + |> ( + res => + res.status ? + { + failure("Already applied to this URL"); + resolve(); + } : + { + callback(); + resolve(); + } + ) + ) + |> catch(err => { + Js.log(err); + failure("Connection Error"); + resolve(); + }) + ) + |> ignore; +}; + +let submit = ( ~company, ~position, @@ -147,19 +191,55 @@ let submitApplication = callback(); resolve(); | _ => - failure(); + failure("Connection Error"); resolve(); }; }) |> catch(err => { Js.log(err); - failure(); + failure("Connection Error"); resolve(); }) ) |> ignore; }; +let submitApplication = + ( + ~company, + ~position, + ~url, + ~resume, + ~date_posted, + ~deadline, + ~status, + ~id, + ~jwt, + ~callback, + ~failure, + ) => + /** TODO here before anything check if already applied */ + checkAlreadyApplied( + ~url, + ~jwt, + ~callback= + _ => + submit( + ~company, + ~position, + ~url, + ~resume, + ~date_posted, + ~deadline, + ~status, + ~id, + ~jwt, + ~callback, + ~failure, + ), + ~failure, + ); + let getResumeRevisions = (~id, ~jwt, ~callback, ~failure) => { let headers = Fetch.HeadersInit.make({